Library loading at runtime

Asked

Viewed 378 times

4

Is there any contraindication in loading libraries .jar at runtime?

I found this code that does this:

URLClassLoader sysloader = (URLClassLoader) ClassLoader.getSystemClassLoader();
Class sysclass = URLClassLoader.class;

try {
      Method method = sysclass.getDeclaredMethod("addURL", new Class[]{URL.class});
      method.setAccessible(true);
      method.invoke(sysloader, new Object[]{URL_CARREGAR});
} catch (Throwable t) {
      t.printStackTrace();
      throw new IOException("Error, could not add URL to system classloader");
}

This code works. But that’s the best way?

I want to load at runtime so that new Jars can be placed in a folder and load new features to my system. As if they were add-on modules.

2 answers

3

This code is good, as it is versatile, simple, useful and flexible. You can make it a little better this way:

public static void adicionarAoClasspath(String caminho) throws IOException {
   adicionarAoClasspath(new File(caminho).toURI().toURL());
}

public static void adicionarAoClasspath(URL url) throws IOException {
    URLClassLoader sysloader = (URLClassLoader) ClassLoader.getSystemClassLoader();

    try {
        Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
        method.setAccessible(true);
        method.invoke(sysloader, url);
    } catch (Throwable t) {
        t.printStackTrace();
        throw new IOException("Error, could not add URL to system classloader", t);
    }
}

The difference is in the string of exceptions, in the use of varargs where possible and in the use of class literal without needing an intermediate variable for this. In addition, it parameterizes what in its original code was the URL_CARREGAR.

  • Thank you for your reply... And you this certain code this way is much better... today mine is already very similar to this, as it loads from a list of files..

2


It is not contraindicated to load jars at runtime, on the contrary. Developing applications that work in this way is desirable. Because then we can change a part of the software without needing to recompile it completely and without even stopping the execution of it.

Your thinking is correct.

The use of this type of approach can be observed in plugins. Plugins add Features to a software at runtime. Sometimes it is necessary to restart the software so that it can load the plugin, but even so the beauty of the solution is lost.

  • Thank you for the answer.. It was exactly what I imagined and exactly the answer I needed. Can you tell me if it might be a problem if I carry the same jar twice? I’m trying to make sure that doesn’t happen, but I have this doubt

  • No problem. JVM will look at Urls in the order they appear in the classpath. That is, it will use the first URL found in classpath. This link explains how the classes are found: http://docs.oracle.com/javase/7/docs/technotes/tools/findingclasses.html. See the part: How the Java Launcher Finds JAR-class-path Classes

Browser other questions tagged

You are not signed in. Login or sign up in order to post.