Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Does classpath work when executing jars?  RSS feed

 
B Hayes
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have no problems using the classpath when executing classes (java -cp).
However, using java -jar -cp doesn't seem to work. If there are any external jars that my main jar requires, I need to put them in the \lib\ext dir for them to be found. I've tried every conceivable pathing string.
On the net I found this... is this correct? I find it hard to believe.
Classpath problem with -jar switch
It's blank, it doesn't compile, even though I've checked and tools.jar is definitely on the classpath. What's wrong?
When an application is run from a jar the classpath is ignored. Even specifying
java -jar -cp %classpath% ejbstudio.jar
does not correct the problem. This is a documented bug in java.exe and probably will not be fixed in JDK1.4 so an alternative that works is to specify
java -cp ejbstudio.jar;%classpath% ejbstudio.app
This is tedious to type (and throws up a console window, handy for debugging but not pretty in production) so set up a Windows shortcut or a script as follows (adapted to wherever you put the JAR).
[JAVA_HOME]\bin\javaw -cp /jarpath/EjbStudio.jar;%classpath% ejbstudio.app
The requisite classes cannot be added to the EjbStudio jar. This would be a breach of the JDK licence, and would not solve the problem of getting your own classes onto the classpath.
The classpath in the application jar specifies a classpath of ./toolsfolder/tools.jar. This means that Unix users can put a symlink to [JAVA_HOME]/lib in the folder in which they keep the application jar, and this will get tools.jar onto the application classpath, allowing them to .
Windows users cannot create symlinks (they're like shortcuts only they work) so they will have to actually create a toolsfolder folder in the application jar folder and copy tools.jar into it in order to take advantage of this.
Note that this does not address the issue of getting your own classes onto the classpath; for that you must avoid the -jar switch as described above.
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your manifest file of the executable .jar file, you have to specify the class path. the entry looks like this:
Class-Path: someExternalJar.jar anotherJar.jar ./a/directory/here
note that the locations are specified relative to the location of the jar file, not from wherever the applciation is being run. Also note that the entries are separated by spaces, not ; or :. Finally, note the hypenation and capitalization of "Class-Path" (whatever happended to standards?)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!