|
Java™ by example!
|
|
|
How do I load a class from a local JAR and create an instance?
For this example, let's create a JAR file called testjar.jar containing only one class in a package esus.testpackage. Create a directory structure:
TestClass.java:
Compile and create a JAR file:
Let's not add this JAR file to the classpath, but dynamically use it to load a class and execute a method on it. For this purpose, use the URLClassLoader which takes a set of URLs pointing to JARs or directories. Main.java:
outputs:
Further Information
Author of answer: Joris Van den Bogaert
Comments to this answer are only viewable by members. Login or become a member!
|
|
|
|
|