Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to change Java Classpath Context

 
Dinkar Chaturvedi
Ranch Hand
Posts: 42
Android Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dinkar Chaturvedi
Ranch Hand
Posts: 42
Android Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic