8

I need to make use of additional jars during the execution of the program. I manually add the jars to the relative ./lib folder and then execute the program. How should I define the classpath in the manifest file?

Suraj
  • 83
  • 1
  • 4
  • I got a solution! I will include variables like: Class-Path: lib/externaljar1.jar lib/externaljar2.jar etc upto 5 or 10 values. Then while including jars during runtime, ill just rename it as externaljar1, externaljar2 etc and copy it to the lib folder. ;-) – Suraj Jan 21 '11 at 11:07
  • I thank every1 who responded to my query. Thanks a lot! – Suraj Jan 21 '11 at 11:07

2 Answers2

12

You can't use regular expressions or other wildcards in the Class-Path attribute of your manifest.

There is only one supported wildcard in Java, and that only works on if specified on the commandline on a "manual" java invocation (i.e. not using -jar): using directoryname/*.

Community
  • 1
  • 1
Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • Is there any other approach other than using the -cp command or the classloader function? – Suraj Jan 21 '11 at 09:18
  • @Suraj: asking about other approaches without telling us why those approaches are no good for you is ... rather pointless. And personally, I find it rather rude as well. – Joachim Sauer Jan 21 '11 at 09:19
  • Sorry about that! Actually I am programming the code for a user friendly approach & deploying the same in an executable jar. It is to have a generic database where the user can choose his database. Thereby he needs to specify the connectivity jar needed & put that in the program accessible folder. I thought if I could use regular-expression in manifest classpath like lib/* OR lib/*.jar so that when program executes it finds all the jars. – Suraj Jan 21 '11 at 09:31
  • If you only need to load selected classes (such as the JDBC driver) from that jar, then you can easily load it via a separate [`URLClassLoader`](http://download.oracle.com/javase/6/docs/api/java/net/URLClassLoader.html). You don't need it on your "main" classpath for that. – Joachim Sauer Jan 21 '11 at 09:33
  • 6
    To be clear - the "one wildcard supported" is *not* supported in the `Class-Path` attribute of a manifest, only in the `-cp` option on the command line. – Tom Anderson Oct 31 '13 at 16:44
  • None wildcard is supported in manifest file, just as the above comment said !!! You should update your answer to correct this ! – Eric Jan 04 '15 at 11:04
  • @EricWang: indeed, I clarified my answer. – Joachim Sauer Jan 05 '15 at 14:48
2

I'm not all too sure about what you want exactly, but you can add jars during runtime:

  • list (jar) files using File.list() on the directory containing the jars
  • do a regex on the filenames you retrieve
  • use an URLClassLoader to load the jar

I don't know exactly how to register (if necessary) the URLClassLoaders to the main classloader. But that's the way I think I would go.

extraneon
  • 23,575
  • 2
  • 47
  • 51
  • 2
    You can't modify the bootstrap classloader within the spec (there are ugly hacks that can do it, using `addURL()`). The spec-conforming solution would be to put just the boostrapping class into its own jar, use that to find all the other jars (including the "real" main jar) and load all of those in one classloader. Then call the real main method from the real main class. – Joachim Sauer Jan 21 '11 at 08:34