Search code examples
classloaderjava-7self-contained

Java 7: using URLClassLoader not working anymore


my problem is, that my code which was working on Java6 does not work any more. Since my app needs to load jar's at runtime (plugins), i wrote myselt a simple class deriving from URLClassLoader like this

public class MyClassLoader extends java.net.URLClassLoader {

/** Creates a new instance of URLClassLoader */
public MyClassLoader(java.net.URL url) 
{
    super(new java.net.URL[]{url},ClassLoader.getSystemClassLoader());
}

public void addURL(java.net.URL url)
{
    super.addURL(url);
}}

So if i want load a jar, i simply call addURL(pathToJar) and load that class via

Class.forName(myClass, true, myClassLoader)

This worked like a charm running on Java6. Now i decided to make a self contained java app in Java7. When I start the app, the jar's also get loaded at runtime, but if there's a class inside which derives from a class that is inside the classpath (not in the plugin jar) i get a ClassCastException.

So i guess something has changed in Java7. At the moment I'm using Java7_u13 on OSX. Can any one give me a hint on what I should do, to get the old behaviour back? Searching the net didn't get me any help yet.

Many thanks in advance.

Greetings, -chris-


Solution

  • Meanwhile i found the solution to my problem. I just used the 'wrong' classloader as the parent. Everything now works fine if i replace

    super(new java.net.URL[]{url},ClassLoader.getSystemClassLoader());
    

    with

    super(new java.net.URL[]{url},MyClassLoader.class.getClassLoader());
    

    Greetings, -chris-