aspose file tools*
The moose likes Java in General and the fly likes How to change Java Classpath Context Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to change Java Classpath Context" Watch "How to change Java Classpath Context" New topic
Author

How to change Java Classpath Context

Dinkar Chaturvedi
Ranch Hand

Joined: Mar 27, 2007
Posts: 42

Hi,
I am developing a java application which will have two modules. The main module will remain constant but the plugin module changes over time. Everytime the plugin module changes, its reflected in the version. The main module connects to a server and checks for changes using current and server version and downloads the latest version of the plugin in case its not latest before it actually executes it.
Each of the modules are supposed to be as jar files. I run the main module using java -jar mainmodule.jar command.
How do i set the classpath to the recently downloaded plugin module which is again a jar file?
Any help on this will be greatly appreciated.

Regards
Dinkar
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

You create a new ClassLoader to load the jar. You can use the standard URLClassLoader class. When you need to reload the plugin classes, you discard the URLClassLoader and make a new one with a new class path.


[Jess in Action][AskingGoodQuestions]
Dinkar Chaturvedi
Ranch Hand

Joined: Mar 27, 2007
Posts: 42

Hey thanks for the reply. I looked thru the JDK api and couldnt figure out much as I havent worked on this class loader apis and stuff. Can you point me to some sample java code where i can find a skeleton of the way to do what i am trying to do?
if you could provide any such help, it'll be of great help.

Regards
Dinkar
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

It's actually pretty simple:

where FooImpl is a class in the jar that implements some interface Foo, and Foo is loaded as part of the main application.

To reload the jar, get rid of the object in "foo", just repeat these same three lines -- but make sure you get rid of the old "foo" and any other objects that came from the first ClassLoader or some weird and confusing stuff can happen!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to change Java Classpath Context