I need to add a jar at run time from path provided in the config file. Let us call it run-time.jar
.
My original jar is original.jar
. I am importing classes from run-time.jar
directly in my original.jar
. At compilation time I have a sample version of run-time.jar
to help me through compilation issues. I am building a thin jar with out dependencies.
I am planning to use URLClassLoader
to load classes at run time. My sample code in original.jar
is
// Importing class from run-time.jar
import run.time.Test
class Original {
public static void main(String[] args) {
/*Code to load classes from jar file*/
Test newTest = new Test();
newTest.runTests();
}
}
Will this approach work or is there any suggestion to do it better. Any help is appreciated. Thanks!!
This approach is sometimes used to load JDBC drivers at runtime, you can get some inspiration from this thread: Loading JDBC Driver at Runtime
I suppose to find out whether this works for your jar, just try it out. But I think your classloader hack should work for Java version < 9.
However from a code quality point of view, this is not a very clean solution as I am sure you are aware of. Have you considered looking into OSGi?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.