Dynamically loading classes from a jar at runtime

Posted on 2010-09-23
Medium Priority
Last Modified: 2013-11-24

I want to write an application in Java which is able to load JARs from a directory during runtime, add all classes inside to the classpath, load a spring context from the jar and instantiate the main class of it (it implements an Interface). If I add the JAR to the classpath before execution I am able to find the class which implements the interface and load it. I want to do this at runtime and call its functions using reflection (the list of methods will be listed by calling a function that is also noted in the interface). I think I need multiple class loaders and instantiate somehow. Does anyone know ho I can accomplish this?

Question by:westerlike
  • 3
  • 3
LVL 26

Expert Comment

ID: 33751231
yes as you said you need toi write custom classloaders, take a look http://www.javaworld.com/javaworld/jw-10-1996/jw-10-indepth.html

Author Comment

ID: 33751289
Thanks, still have some questions though: If I load the jar with a custom loader and call the loadClass of the classloader, will it also load all dependencies of that class (those are also bundled in the jar file)? In my case I have a spring.xml in the jar as well which describes the beans inside, they use several libraries whcih are also bundled inside the jar file. The main entry point class implements and interface that I know and can find it from the context file once the jar is loaded.

How would I approach loading that bean context and instantiating the main class from the jar using the classloader?
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 33751333
<<If I load the jar with a custom loader and call the loadClass of the classloader, will it also load all dependencies of that class (those are also bundled in the jar file)?>>

<<instantiating the main class from the jar using the classloader?>>
once the class is loaded, you can instantiate it way you normally do.

Author Comment

ID: 33751378
and would the libraries in the jar file interfere with the main classloader versioning/dupe classes? Also would they be able to communicate? I am trying to load the class and have the main class loader call functions using reflection. The jars can be replaced during runtime and the main app would then remove the custom loader and instantiate a new one.
LVL 40

Accepted Solution

Gurvinder Pal Singh earned 2000 total points
ID: 33751400
the system classes will not be overriden. duplicate classes are not loaded again. Please read carefully the first link i had posted

to know how to reload a class again, check this


Author Closing Comment

ID: 33844742
Thanks. I managed to create a dynamic loader.

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Don’ts and Dos are two important end products of software testing basics that a tester needs to regard. This article attempts to explain the principles of both.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month9 days, 11 hours left to enroll

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question