File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes setting the classpath for java -jar Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "setting the classpath for java -jar" Watch "setting the classpath for java -jar" New topic

setting the classpath for java -jar

Tom Rispoli
Ranch Hand

Joined: Aug 29, 2008
Posts: 349
Hi Every one, I'm trying to execute a jar file using java 1.6 and I keep getting classNotFoundExceptions when I try to use classes in another .jar file. I tried setting the CLASSPATH environment variable and using the -cp and -classpath command line options but they don't seem to help (I read that they are ignored for -jar commands). I also tried using the Class-Path attribute in the file of the jar. I think this is the right way to do it but it still doesn't seem to work. I have myApp.jar which uses files from myUtil.jar. I have the 2 files in the same directory. In the file I have this: Class-Path: myUtil.jar. However it still can't find the classes. I tried putting myUtil.jar in jdk\jre\lib\ext, this worked and the classes were found but I don't like this solution, I want to give my client more flexibity on where they keep the util file. Does any one have any advice on what I'm doing wrong or any tips on how to debug this?

Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
See here:
Execute a program encapsulated in a JAR file. The first argument is the name of a JAR file instead of a startup class name. In order for this option to work, the manifest of the JAR file must contain a line of the form Main-Class: classname. Here, classname identifies the class having the public static void main(String[] args) method that serves as your application's starting point. See the Jar tool reference page and the Jar trail of the Java Tutorial for information about working with Jar files and Jar-file manifests.
When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored.

So, if you try to run jar with java - jar command, the classpath optinions are ignored,
but you must set properly the "Main-Class: classname" option in the manifest file,
and this main class has to have public static void main(String ... ) method.

Tom Rispoli
Ranch Hand

Joined: Aug 29, 2008
Posts: 349
Thanks for the reply, I do have the Main-Class attribute set in the manifest, and its working correctly. When I run java -jar myApp.jar the main class that I specified is executed. The problem occurs when the code in the main class references a class file in myUtil.jar. I get the class not found error. I need to know what to do to get the class loader to find my other .jar file.

Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

Yes, using the manifest is the right way to do it. Just make sure the manifest file has the right name, and make sure it's actually in the jar in the right place. The other gotcha I have read about is something about requiring an end-of-line character at the end of the last line of the manifest.

I don't remember if the "Class-Path" name is case-sensitive, but check that out as a possibility. And also check out the casing of the jar file's name, if you're running this in a file system with case-sensitive file names.

Also, what you put in the manifest requires both jars to be in the same directory. I believe you said that's what you did, and that's probably the best thing to do. Don't mess about giving your client "flexibility" about where to put the jars, that's not how software installs work. Just put the two jars in the same directory and leave it at that.
Tom Rispoli
Ranch Hand

Joined: Aug 29, 2008
Posts: 349
Ok, I think I found out what I was doing wrong in this case. I'm creating the myApp.jar file using ant, and I have index="yes" in the command. I took that out and everything worked fine.

Thanks for the help.
I agree. Here's the link:
subject: setting the classpath for java -jar
It's not a secret anymore!