Solved

How do I dynamically load/unload Component (jar file) in my java application?

Posted on 2007-12-06
15
6,495 Views
Last Modified: 2013-12-17
My Application demands some components (jar files) to be loaded/unloaded dynamically on request. For example, I have module1.jar and module2.jar etc and on demand my application should be able to load/unload  module1.jar or module2.jar files or both. Could you guys please help me with any java code example of load/unload jar files dynamically (i.e. without restarting the server). Again, please I need sample code for doing this.

Thanks in advance for your help.

Open in new window

0
Comment
Question by:shilpa_rajith
  • 6
  • 5
  • 2
  • +1
15 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 20419706
Loading modules is in the offing for the coming version of Java, but for now, you can load classes dynamically by calling Class.forName or using an explicit classloader. You can keep your classes in jars and they can be either in or out of the classpath. If the latter, load them with a JarURLConnection
0
 
LVL 9

Expert Comment

by:ysnky
ID: 20419717
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20419721
0
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
LVL 86

Expert Comment

by:CEHJ
ID: 20419742
Sorry - i meant URLClassLoader not JarURLConnection
0
 

Author Comment

by:shilpa_rajith
ID: 20419926
Thanks a lot for you quick response and sorry for not very descriptive on the question. First, the jar file can be of the same module but diff. versions. i.e. I want to unload Module1.jar version 1 and load latest of the same module say Module1.jar version 2. Second, How to unload a jar file?
Google search returned "Apache Felix OSGi" framework. I never used this. Did you guys ever used this framework? Please provide some code example as It will help me implement it fast.

Thanks a lot in advance.
0
 
LVL 9

Expert Comment

by:ysnky
ID: 20422334
0
 
LVL 92

Expert Comment

by:objects
ID: 20424012
to unload a jar you need to delete the classloader used to load (thus do not use the system classloader)

http://site.red-illusions.dk/2006/11/04/simple-dynamic-loadingunloading-of-code/
0
 

Author Comment

by:shilpa_rajith
ID: 20540108
Hi Guys,

Sorry for getting back on this late.

What I want here is, Instead of placing a JAR file in lib folder of a webapp, the app should be able to load it dynamically and entire application should be able to refer to it. I want to achieve the same effect of having a JAR file in lib folder of a webapp but dynamically.

Thanks a lot in advance.
0
 

Author Comment

by:shilpa_rajith
ID: 20774842

Thanks a lot to all experts for sending in your responses. I am closing this as I couldn't find an appropriate answer to my question. Probably I will post the question again more clearly may be with example.
Thanks all again.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20775981
>>I am closing this as I couldn't find an appropriate answer to my question.

All the answers look pretty relevant to me ;-)
0
 

Author Comment

by:shilpa_rajith
ID: 20812214

Dear CEHJ,

I am not sure how to achieve the requirement  i posted on 12.28.2007. i.e get the effect of placing a jar file in lib folder.
e.g. i want to load a jar file in a method say loadJarFile(String jarfilename) and refer to the classes in it in another method (in diff. package) say useClassOfJarFile() method without referring to the URLClassloader object created in loadJarFile() method. Other way of saying is, instead of placing a jar file in lib folder of a webapp, I want to load it dynamically and refer to its classes anywhere in the app. how to achieve this?

Thanks,
Rajith
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 300 total points
ID: 20813074
You can use reflection to call the methods of any classes that are loaded dynamically, so it's perfectly possible. What's not possible is to declare classes in the code that are not loadable at the entry point (or before) of your application
0
 

Author Comment

by:shilpa_rajith
ID: 20899639

Thanks a lot for the responses. Sorry, I didn't find proper solution to my request. I would like to close/delete this question and would consider putting new one. Thanks again.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20913276
shilpa_rajith, you should realize how this site works. You have had a lot of help from experts on this question. The fact that you don't like the answers is not really relevant. The answer 'it can't be done' IS AN ANSWER
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
tried to place tif image in 6.5 2 26
ServiceCenter IR Query Expressions 1 40
jsp error 6 25
Designing forms 3 17
The article will include the best Data Recovery Tools along with their Features, Capabilities, and their Download Links. Hope you’ll enjoy it and will choose the one as required by you.
Healthcare organizations in the United States must adhere to the guidance of both the HIPAA (Health Insurance Portability and Accountability Act) and HITECH (Health Information Technology for Economic and Clinical Health Act) for securing and protec…
An overview on how to enroll an hourly employee into the employee database and how to give them access into the clock in terminal.
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.

856 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