24

I was recently asked in an interview about the order in which classloaders are called when a class is loaded.

Unfortunately I've never had the need to write my own classloader so at the time was unfamiliar with the intricacies of classloading.

This got me wondering, what reasons are their to write your own classloader.

So that's my question: What scenarios have people faced which required the need to writing their own classloaders?

Brett Hannah
  • 4,307
  • 4
  • 30
  • 33
  • In very simple words, default class-loaders which come with JRE can load classes from classpath (file systems), URLs (sockets e.g. Applet class loader, RMI class loader) etc. If these are not enough or does not load your classes properly (e.g. different versions of same class need to be loaded) you need to write your own custom class loaders. – Bimalesh Jha Jul 20 '13 at 17:29

12 Answers12

19

I'm currently working on an extremely large application that is highly modularized, i.e. it consists of literally hundreds of JAR files. This meant that the classpath string became huge, causing all sorts of problems in all sorts of places, because of various development tools's inability to deal with a 5KB classpath string. This was solved by writing a custom classloader that reads its classpath from a file.

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
11

Typical reason is that your application is hosting other applications which are using same libraries in different versions in the same runtime (e.g. Tomcat). So you have to make sure that your classloader can provide different versions of the same class for each of these applications.

EDIT:

To clarify this a little bit (see confusion in comments): When said "your classloader" I ment "an implementation of java.lang.ClassLoader" not an instance of such a class. Actually it's your classloaders in both meanings: the Tomcat people implemented different ClassLoader-classes and have even more instances at runtime...for details see the corresponding docs.

Kai Huppmann
  • 10,705
  • 6
  • 47
  • 78
  • actually you would have to load different versions of the same class with different instances of your classloader. One classloader cannot have two versions of the same class at the same time... – pgras Feb 25 '09 at 13:59
  • @pgras: I think that's what kai1968 meant... "your classloader" == "the classloaders used in your application", at least that's how I read it. – Mr. Shiny and New 安宇 Feb 25 '09 at 14:55
7

Boredom and the desire to torment my co-workers when they had to maintain my code. :)

dhable
  • 2,879
  • 26
  • 35
3

Some places actually store classes in a database (well there used to be places, not sure if there are anymore) and use a class loader to get the classes from the database at run time.

TofuBeer
  • 60,850
  • 18
  • 118
  • 163
  • 3
    My company did this so that a developer could write a plugin and upload it into the application with no downtime. We mostly switched to using scripting languages (Groovy, JRuby) for that functionality, but it was fun while it lasted. – Brian Deterling Feb 25 '09 at 22:39
2

I had to implement a ClassLoader once when I wanted to load up classes in .jar files from within a .jar file (this was a few years ago, I'm sure there are tools now which can do that for you). i.e., you would put your dependency .jar files into one .jar file.

But that's the only time, in my experience writing a custom ClassLoader is a pretty rare thing.

1

You can do software releases without the downtime, kinda of situations in 24x7 running systems.
You can write your own class loader call that from JMX and replace the class file at run time.

Rohit Sachan
  • 1,178
  • 1
  • 8
  • 16
1

Have a look at this question.

Community
  • 1
  • 1
kgiannakakis
  • 103,016
  • 27
  • 158
  • 194
1

I came across an article which talks about why (very briefly) OSGI uses a custom Classloader.

Parag
  • 12,093
  • 16
  • 57
  • 75
0

I have seen a good post here. It talks about different class loaders, class loader hierarchy and custom class loader.

Darshan Lila
  • 5,772
  • 2
  • 24
  • 34
Sanju Thomas
  • 181
  • 2
  • 10
0

We had an application framework that had applications based on it which were 'statically tied'. This meant that you needed to have a jvm instance per application you wanted to run, which was not only bad for memory use, but meant that you could not have a super application from which to launch the various apps, or any easy (non-interprocess) communication between the running apps.

Since the whole thing was run from webstart (i.e with a bunch of jars as the classpath), the solution to preventing the system classloader from finding the classes was to offset the packages. For expample if you had a class a.b.X in an application foo then it would be in the jar file as foo/a/b/X.class.

mike g
  • 1,791
  • 1
  • 18
  • 25
0

At my last job we implemented a server that could have "plug-in logical query definitions". (The client could call queries by name, the server looked up the registered query for that name and ran it).

The query definitions were code and/or metadata contained in a jar.

The jar was uploaded to the server though our console application.

When uploaded (and later when the server restarted), our framework would create a class loader for the jar to load it into the running server.

Scott Stanchfield
  • 29,742
  • 9
  • 47
  • 65
0

I have do that one time. We have to use a API provide by a third party vendor, and this API use a strange version of hibernate3.jar. So, we had to load this specific jar with a custom classloader in order to avoir "serial version UID" exception.

Antoine Claval
  • 4,923
  • 7
  • 40
  • 68