3

I want to look for extension jars in a predefined directory and examine their manifest. Those which match should be loaded, those which do not - should be unloaded.

Now, I can do it a primitive way - mandate that each extension pass the name of its bootstrapper on the command line and then for each jar found in the directory try and load any of the given bootstrapper types. Something like this:

java -jar myapp.jar --pluginDir=c:/a/b/c --exts="classA;classB;classC"

Then myapp would iterate over all the files inside the pluginDir and try to load each of classA, classB or classC. Once a classX is successfully loaded, it is removed from the list. The search stops when either all the extensions are found and loaded or there are no more jars in the plugin dir.

A slightly more javaish approach seems to be writing something in the manifest of extension jars and check the manifest of each found jar.

Can anyone show a working code snippet of this approach or of anything better, if there is?

EDIT

A quote from the postgre JDBC driver README file (see https://stackoverflow.com/a/12480373/80002):

INSTALLING THE DRIVER

To install the driver, the postgresql.jar file has to be in the classpath.

ie: under LINUX/SOLARIS (the example here is my linux box):

    export CLASSPATH=.:/usr/local/pgsql/share/java/postgresql.jar 
Community
  • 1
  • 1
mark
  • 59,016
  • 79
  • 296
  • 580
  • Apparently, my question is almost the same as http://stackoverflow.com/questions/11447699/java-spi-serviceloader-adding-multiple-jars-to-app-classpath, only I did not know about the ServiceLoader and left an option for other solutions. – mark Sep 18 '12 at 15:47

1 Answers1

1

There is ServideLoader class for this purpose. JDBC drivers are built on this principle. Hope, it helped.

Jiri Kremser
  • 12,471
  • 7
  • 45
  • 72
  • Please, give an example code snippet. Also note, that the extension jars are not in the class path as it appears in the manifest of the application jar. – mark Sep 18 '12 at 15:45
  • It is quite complex, but if you've read the JavaDoc and try Google. You may find examples like http://solitarygeek.com/java/a-simple-pluggable-java-application or http://weblogs.java.net/blog/timboudreau/archive/2008/08/simple_dependen.html – Jiri Kremser Sep 18 '12 at 15:52
  • I wish you could give an example. Still +1. – mark Sep 18 '12 at 16:09
  • Ok 1 real world example: here is the plugin http://jdbc.postgresql.org/download/postgresql-jdbc-9.1-902.src.tar.gz notice the META-INF/services content and here is the "plugin loader" http://www.docjar.com/html/api/java/sql/DriverManager.java.html line 502 – Jiri Kremser Sep 18 '12 at 16:27
  • Nope. They specifically require the driver to be in the CLASSPATH - please see an update to my question. – mark Sep 18 '12 at 16:44
  • It is not possible to instantiate a class which is not on the CLASSPATH (except compiling it in Java, but it is an edge case). You have to firstly get it to the CLASSPATH. It is not difficult see http://stackoverflow.com/questions/252893/how-do-you-change-the-classpath-within-java – Jiri Kremser Sep 18 '12 at 16:54
  • But I do not know which jar is an extension jar and which is not! Does this mean I need to add all the jars in the plugin directory to the class path? – mark Sep 18 '12 at 17:03