6

I've tested on three windows machines, and two linux VPSes, on different versions of Java, both on the OpenJDK & Oracle JDK. It functioned perfectly, and then all of a sudden, it only works in my IDE, though I haven't changed any relevant code, and I can't imagine what can cause this.

Prevalent code in system:

Class<?> cls = (session == null ? secjlcl : session.getJLCL()).loadClass(name);
Logger.log(JavaLoader.class.isAssignableFrom(cls) + " - " + cls + " - " + cls.getSuperclass().getName());
if (JavaLoader.class.isAssignableFrom(cls)) {

And my ClassLoader:

public class JavaLoaderClassLoader extends URLClassLoader {
public JavaLoaderClassLoader(URL[] url, ClassLoader parent) {
    super(url);
}

private HashMap<String, Class<?>> javaLoaders = new HashMap<String, Class<?>>();

public String addClass(byte[] data) throws LinkageError {
    Class<?> cls = defineClass(null, data, 0, data.length);
    javaLoaders.put(cls.getName(), cls);
    return cls.getName();
}

public Class<?> loadClass(String name, boolean resolve) {
    if (javaLoaders.containsKey(name)) return javaLoaders.get(name);
    try {
        Class<?> see = super.loadClass(name, resolve);
        if (see != null) return see;
    }catch (ClassNotFoundException e) {
        Logger.logError(e);
    }
    return null;
}

public void finalize() throws Throwable {
    super.finalize();
    javaLoaders = null;
}
}

One note, I expect many classloaders to load a different file in the same name/package, so I use separate classloaders to keep them separate, however in testing, that was NOT tested.

Now, this had worked flawlessly in the past, and I have zero clue why it stopped. I would assume I broke something, but the code still works in my IDE?

4
  • 2
    You have multiple class loaders. What does System.out.println(cls.getClassLoader()+" "+JavaLoader.class.getClassLoader()); print - two different things, or the same things twice?
    – user253751
    Apr 26, 2015 at 11:10
  • @immibis On non-IDE environments, my loaded one is my custom ClassLoader, and one is the standard URLClassLoader. However, in my IDE, it uses AppClassLoader from sun.misc.Launcher. That seems to show the problem, I could use the parent classloader for mine as that, OR load my JavaLoader class with my classloader. I'll investigate. Apr 26, 2015 at 18:31
  • Are you running a JAR that has other JARs inside it? E.g., in Eclipse you export a runnable JAR and select "Packages required libraries into generated JAR"? If so, then look into Eclipse's JAR-in-JAR classloader which is setting its own default StreamHandlerFactory. The other issue might be order in which classes are searched.
    – Dan
    Jul 3, 2015 at 4:47
  • are you running in some managed environment (i.e Servlet Container, or some EJB Container perhaps)?
    – diginoise
    Oct 15, 2015 at 12:03

1 Answer 1

1

This appears to be your bug:

public JavaLoaderClassLoader(URL[] url, ClassLoader parent) {
    super(url);
}

You aren't installing parent as the parent class loader through the super constructor.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.