Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java ClassLoader - Adding dynamically loaded jars to the system class loader

Is there any way to update the system class loader at runtime? After I've dynamically loaded a jar file, is there anything I can do to add the classes/packages loaded from this jar into my system class loader?

The reason I'm trying to do this is that while I've had some success through just passing around my newly created ClassLoader in my own code, I'm having trouble with a third party library (apache-WSIF) that doesn't seem to be working with the passed in ClassLoader.

like image 635
Fozefy Avatar asked Jul 07 '11 21:07

Fozefy


People also ask

Which ClassLoader loads JAR files from JDK?

The Bootstrap class loader loads the basic runtime classes provided by the JVM, plus any classes from JAR files present in the system extensions directory. It is parent to the System class loader. To add JAR files to the system extensions, directory, see Using the Java Optional Package Mechanism.

What is Java dynamic loading?

Dynamic loading is a technique for programmatically invoking the functions of a class loader at run time. Dynamic class loading is done when the name of the class is not known at compile time.


1 Answers

I've been able to achieve what I was attempting to do using the following:

Thread.currentThread().setContextClassLoader(myClassLoader);

As discussed in the top answer here: How do you change the CLASSPATH within Java?

Basically, before calling into the WSIF library all I need to do is make sure I've set my custom classLoader as the contextClassLoader on the current thread.

like image 97
Fozefy Avatar answered Sep 22 '22 22:09

Fozefy