0

I have the following scenario. I'm working with a server which has a lib folder which contains all of its JARs, but things get kind of complicated when having multiple applications for the server, as the lib directory gets kind of crazy.

What I'd like to do is put all of my application's JARs in a different location and instruct the classloader to load them when my application is loaded. How can I do this? How will I need to rewrite my application to facilitate this?

Naftuli Kay
  • 87,710
  • 93
  • 269
  • 411
  • 2
    Isn't this what the CLASSPATH environment variable is for? – CanSpice Aug 17 '11 at 18:47
  • What kind of server is it? A JEE app server like Tomcat or Jetty? If not, what constitutes an application running on this server, and how does the server run it? – JB Nizet Aug 17 '11 at 18:48
  • This server bootstraps itself and loads JARs in its own way. Ie: the classpath is only set to a single JAR which then loads everything else. Plus, I have a _lot_ of JARs that I need to include and I don't want a CLASSPATH variable a thousand miles long. – Naftuli Kay Aug 17 '11 at 18:49
  • It's Wowza Media Server, not a JEE server. It's kind of complicated, but there's a hook which fires when it's starting up which is where I'd do my logic. My main bootstrap JAR would live in the lib folder, but then load everything else from elsewhere. – Naftuli Kay Aug 17 '11 at 18:50
  • As Peter Lawrey says, Java 6 supports directories on the classpath which will load all jars in that directory. But if you can't do that or if you have to load the jars at runtime, then you will need to do as gigadot explains and write your own custom class loader. – Jason Wheeler Aug 17 '11 at 20:40

2 Answers2

4

As far as I know, it is not possible to load the jar files or set the CLASSPATH at runtime unless you have written your own class loader. Tomcat is a good illustration in which the tomcat's class loader loads the jar files dynamically in multi-level.

http://tomcat.apache.org/tomcat-6.0-doc/class-loader-howto.html

There is a common class loader which allow the jar files to be shared accross all web application. However, if you want to addthe jar files which are only loaded for the web application then you should add it under webroot, e.g. WEB-INF/lib. The libraries in this folder are not shared to all applications. For example, this means if two web applications are using different versions of the same library, they will not be mixed up.

Since I am using maven to manage the project, I do not need to worry about where to put my ja dependencies. Maven automatically bundles the libraries into war file for me.

So if you are not using servlet container, e.g. tomcat, jetty, etc. , then you will need to write a class loader. If that is necessary, then have a look into tomcat but it may not be easy.

gigadot
  • 8,879
  • 7
  • 35
  • 51
  • Just want to chime in: I've written a custom classloader for a small app-container I wrote myself, and while it's not trivial, it's not that difficult. However, in the OP's case I'm not sure he can rewrite his container (Wowza media server) to use a custom, hierarchical classloader. – Alistair A. Israel Aug 17 '11 at 21:04
  • I amnot sure if it will work for Wowza media server but there are a few tips to try which works for wrapping the existing class loader of a current thread assumming the class loader is an instance of a URLClassLoader http://stackoverflow.com/questions/252893/how-do-you-change-the-classpath-within-java – gigadot Aug 19 '11 at 07:23
2

You can add directories to the class path in Java 6.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130