-2

How to load a java lib to a running application dynamically?

I would like to add a dynamic lib (a jar file), so it is loaded.

For example, the program works well, however when the user tries to invoke specific functionality which needs logic present in the external lib, an exception is thrown:

Java.lang.NoClassDefFoundError: Could not initialize class...

Yan Khonski
  • 12,225
  • 15
  • 76
  • 114
  • 1
    https://stackoverflow.com/questions/60764/how-should-i-load-jars-dynamically-at-runtime – BackSlash Mar 15 '18 at 14:48
  • 1
    I'm voting to close this question as off-topic because there is no question, and it lacks search efforts – Thomas Ayoub Mar 15 '18 at 15:35
  • Possible duplicate: https://stackoverflow.com/questions/1457863/what-causes-and-what-are-the-differences-between-noclassdeffounderror-and-classn – mustaccio Jul 24 '18 at 16:15

1 Answers1

1

You can use URLClassLoader to load jar file. https://docs.oracle.com/javase/7/docs/api/java/net/URLClassLoader.html

Here is an example how to use this: https://examples.javacodegeeks.com/core-java/net/urlclassloader/java-net-urlclassloader-example/

Note that you must use reflection after you dynamically load jar.