22

Until for adding external jar to classpath in runtime by programmatically everybody used:

URLClassLoader sysloader = (URLClassLoader) ClassLoader.getSystemClassLoader();
Method method = URLClassLoader.class.getDeclaredMethod("addURL", new Class[]{URL.class});
method.invoke(sysloader, new Object[]{file.toURI().toURL()});

Now with java9 we have problem:

Exception in thread "main" java.lang.ClassCastException: java.base/jdk.internal.loader.ClassLoaders$AppClassLoader cannot be cast to java.base/java.net.URLClassLoader

URLClassLoader doesn't work anymore in Java 9. What to do now under jdk9 for adding an external jar to the classpath in runtime programmatically?

6
  • 3
    Though the release note read that the current JavaSE/JDK doesn't provide any such API to augment the classpath at run-time. Yet, the better you could elaborate on what you're ultimately trying to achieve, the more the community could help you.
    – Naman
    Dec 31, 2017 at 12:48
  • 2
    ClassLoader.getSystemClassLoader() is not an URLClassLoader anymore but you can always create a new instance of URLClassLoader if you want. Dec 31, 2017 at 13:06
  • 1
    @ZhekaKozlov is correct, esp. if you can invoking the code in the JAR file via reflection anyway. Dec 31, 2017 at 15:45
  • Actually, with Java 9, you have two problems. Even if the ClassLoader were in fact a URLClassLoader, I doubt that attempt at reflection would be permitted in a modular application.
    – VGR
    Dec 31, 2017 at 16:19
  • I found out what "Gradle" had the problem with UrlClassLoader. Anybody know how they fixed it? Dec 31, 2017 at 18:18

2 Answers 2

11

The JavaSE9 release notes read about the same :

The application class loader is no longer an instance of java.net.URLClassLoader (an implementation detail that was never specified in previous releases).

Code that assumes that ClassLoader::getSytemClassLoader returns a URLClassLoader object will need to be updated.

Note that Java SE and the JDK do not provide an API for applications or libraries to dynamically augment the class path at run-time.

Additionally when an extended classpath is required, one can make use of

Class<?> clazz = Class.forName("nameofclass", true, new URLClassLoader(urlarrayofextrajarsordirs));

as suggested in this thread from Oracle. This comes with caveats:

  • java.util.ServiceLoader uses the thread's ClassLoader context Thread.currentThread().setContextClassLoader(specialloader);

  • java.sql.DriverManager does honors the calling class' ClassLoader, -not- the Thread's ClassLoader. Create Driver directly using Class.forName("drivername", true, new URLClassLoader(urlarrayofextrajarsordirs).newInstance();

  • javax.activation uses the thread's ClassLoader context (important for javax.mail).

4
  • Ok. What in return for the old? Dec 31, 2017 at 18:22
  • @EvgeniyEgorov What exactly are you trying to achieve is the actual question. If you could elaborate on that, would be helpful.
    – Naman
    Dec 31, 2017 at 18:54
  • I tried to find info about it in google search. Unfortunately, I found out no any info or docs. Jan 1, 2018 at 20:39
  • 2
    @EvgeniyEgorov You might want to take a look at this thread as well and be definitive over what your requirement is.
    – Naman
    Jan 2, 2018 at 2:03
3

Naman's answer is not a correct replacement for what you are looking for. The correct way to add a jar to the classpath in Java 9 and above is to use Java Instrumentation's appendToSystemClassLoaderSearch(JarFile jarfile) method.

First you will need to add your Agent class to your MANIFEST.MF

Launcher-Agent-Class: com.yourpackage.Agent

Then add your agent. The example below will allow you to call Agent.addClassPath(File f) to add a Jar to the classpath in both Java 8 & 9+

public class Agent {
    private static Instrumentation inst = null;

    // The JRE will call method before launching your main()
    public static void agentmain(final String a, final Instrumentation inst) {
        Agent.inst = inst;
    }

    public static boolean addClassPath(File f) {
        ClassLoader cl = ClassLoader.getSystemClassLoader();

        try {
            // If Java 9 or higher use Instrumentation
            if (!(cl instanceof URLClassLoader)) {
                inst.appendToSystemClassLoaderSearch(new JarFile(f));
                return;
            }

            // If Java 8 or below fallback to old method
            Method m = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
            m.setAccessible(true);
            m.invoke(cl, (Object)f.toURI().toURL());
        } catch (Throwable e) { e.printStackTrace(); }
    }

}
2
  • 2
    jdk12. agentmain(..) wasn't called. MANIFEST.MF has the string with Launcher-Agent-Class. Jun 27, 2019 at 19:30
  • Is any detailed maven exmple? Dec 9, 2023 at 7:04

Your Answer

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

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