• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to add new jar files to class path dynamically at runtime?

 
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not sure this is the right forum for this question, but here goes...

I need to develop a web app (Spring MVC, if you must know, but the question is not spring-specific), that will allow a user to upload a JAR file that they have developed. The servlet needs to scan the JAR for compatible classes that implement a specific "plugin" interface, and add those classes to it's classPath. Essentially, this should allow the user of the site to upload, enable, and configure plugins.

Is this possible? And if so, how can I add these uploaded JARs to the classpath for the webapp in a way that they won't be deleted next time I deploy my WAR file (ie: I can't store the uploaded JARs in the WEB-INF folder, etc.)?

thanks,
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That really sounds like a job for a separate class loader. That way you could avoid all sorts of ghastly duplicate class name etc problems.

Be warned, doing your own class loader is not for the faint-hearted.

Runtime scanning classes for annotations indicating a plug-in architecture is what the Jersey project does.

Letting users run arbitrary code on your server sounds like a recipe for security disaster to me - you may find yourself originating spam email or having your files deleted.

Bill

 
Philippe Desrosiers
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks William!

Letting users run arbitrary code on your server sounds like a recipe for security disaster to me - you may find yourself originating spam email or having your files deleted.



I totally agree. This is more an admin console scenario, where the user would be running arbitrary code on their own server :-)
 
William Brogden
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, assuming you are going to try this, the essential thing is to define an interface for your "plug-in" which will be the only communication between the servlet side and the plugin.

Not necessarily a formal Interface, an Abstract class which the user will have to extend works also.

That will make it possible for a custom class loader to create instances and route requests to it.

Bill
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Philippe Desrosiers wrote:Not sure this is the right forum for this question, but here goes...

I need to develop a web app (Spring MVC, if you must know, but the question is not spring-specific), that will allow a user to upload a JAR file that they have developed. The servlet needs to scan the JAR for compatible classes that implement a specific "plugin" interface, and add those classes to it's classPath. Essentially, this should allow the user of the site to upload, enable, and configure plugins.

Is this possible? And if so, how can I add these uploaded JARs to the classpath for the webapp in a way that they won't be deleted next time I deploy my WAR file (ie: I can't store the uploaded JARs in the WEB-INF folder, etc.)?

thanks,

 
Philippe Desrosiers
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bnaresh, my man! Thanks so much, this is pretty much exactly what I was looking for!

So that's "part one" of the problem solved. Part two is "scanning" a particular JAR file for any classes that implement a specific interface. I'm sure I've asked that question before, though, somewhere in the forums. Just have to try and find it now....


thanks again!
 
reply
    Bookmark Topic Watch Topic
  • New Topic