14

When trying to solve this problem, I encountered some articles, etc. referring to "isolated" ClassLoaders. I was unable to find a definition for isolated classloader via Google search, so maybe the term is not widely-known jargon, and perhaps has a different meaning in different contexts.

Anyway, Maven's surefire plugin can use an isolated ClassLoader: http://maven.apache.org/plugins/maven-surefire-plugin/examples/class-loading.html

Also one of the answers below references an article explaining how to create an "isolated" ClassLoader.

Neither reference above gives a definition for an isolated ClassLoader; they seem to assume the reader knows what that means or can look it up. However, the second link does include a hint as to what it means by "isolated":

Bootstrapping lets you run your container without polluting the system classpath. This allows you to run your deployed applications with the unpolluted system classpath as its parent. You’ve achieved classloader isolation.

But I'm not quite clear on what is isolated from what and how from this paragraph or the rest of the article. I see that he's loading one version of a class w/o overriding / overwriting another version--maybe one classloader is isolated from another by being different instances w/o one being the parent of the other? I'm not sure.

I especially covet a Google or SO search link that contains a link clearly holding the answer. A direct link to an answer works too. :)

Community
  • 1
  • 1
jyoungdev
  • 2,674
  • 4
  • 26
  • 36
  • Please provide a link or quotation that gives context to your question. (BTW, isn't covetousness a sin? :-) ) – Stephen C Mar 06 '12 at 00:30
  • @Stephen: We'd appreciate it if you left your religious zealotry outside when you come in. – Matti Virkkunen Mar 06 '12 at 00:34
  • 2
    Well, we know from Larry Wall that the three great virtues of a programmer are laziness, impatience, and hubris, and since covetousness is not a virtue, it must be a sin. So there you go. – Tom Anderson Mar 06 '12 at 00:39
  • 5
    @MattiVirkkunen - Can I recommend that you look up what ":-)" means. Perhaps you need to turn down your own anti-religious zealotry setting. – Stephen C Mar 06 '12 at 01:06
  • @StephenC I updated my question with context. – jyoungdev Mar 06 '12 at 16:28

4 Answers4

11

The author is using the term "isolation" basically to mean that the bootstrap classloader for the JVM (the "main" classloader) doesn't have any extra jars/classes in it (just one simple class which in turn sets up the child classloader(s)). The article isn't real obvious as to why this is "isolated" because it only sets up one child classloader. The "isolation" term becomes more obvious when you setup more than one child classloader. These children will be isolated from each other in that they won't share any classes (other than the core JRE classes). Thus, you can do things like have each child use a different version of the same jar.

Raul Santelices
  • 1,030
  • 11
  • 17
jtahlborn
  • 52,909
  • 5
  • 76
  • 118
  • For example: classloader1 = new URLClassLoader(oneUrl, parent); classloader2 = new URLClassLoader(anotherUrl, parent); would create 2 classloaders isolated from ea. other, each one able to have different versions of a given class (and maybe have other uses), correct? – jyoungdev Mar 06 '12 at 19:35
  • 2
    @apollodude217 - yes, that is correct. the key bit, though, is that neither oneUrl nor anotherUrl should reference classes loaded by the parent classloader (hence the example runs the main classloader with no extra jars). – jtahlborn Mar 06 '12 at 21:14
2

Here's how to create an isolated classloader, you'd create one any time you want an unpolluted system classpath, useful for bootstrapping Java programs.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • This was actually one of the articles I read when trying to look up what an isolated classloader is, and it's not clear to me from the context what it is. I see what the author is doing, but I do not know what part of that makes his classloader "isolated". – jyoungdev Mar 06 '12 at 16:29
  • 1
    The link is broken – Michał Klimczak Feb 06 '23 at 10:56
1

From my understanding from the article that you linked to that an isolated class loader is a class loader that is separate to the main system class loader. That is, classes loaded by the isolated class loader won't be available to every class in JRE (only the ones that the isolated class loader loaded itself [and any classes loaded by any child class loaders]).

This could be useful because two of your dependencies might require different versions of the same library to run. That is, your dependencies might expect to see different instantiations of the same class in the library. Thus giving them two separate isolated class loaders will allow them to load the classes they require and not interfere with each other.

Dunes
  • 37,291
  • 7
  • 81
  • 97
0

By default classloaders form a tree with the system classloader at the root. New child classloaders will by default ask their parent first if it can load the class, and if not, load from their own sources.

The goal of an isolated classloader is to isolate yourself from the parent classloaders by checking your own sources FIRST, then your parent SECOND, so reversing the order. This allows you to create a new classloading context where you control the first place to look. You still want to fallback to the parent chain so you can load things like the jdk etc.

You also need to take care that the isolated classloader return instances of interfaces defined in the parent classloader, or else you will run into LinkageErrors or other issues.

Here's an article I wrote in 2006, but it's still fairly relevant (it's incomplete with respect to resource loading and the apis have changed a bit over the years):

http://tech.puredanger.com/2006/11/09/classloader/

Alex Miller
  • 69,183
  • 25
  • 122
  • 167