Dynamically loading classes from a jar at runtime

Posted on 2010-09-23
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
LVL 26

Expert Comment

ID: 33751231
yes as you said you need toi write custom classloaders, take a look

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?
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 40

Expert Comment

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

gurvinder372 earned 500 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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

630 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