The moose likes Beginning Java and the fly likes jar file classpath Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "jar file classpath" Watch "jar file classpath" New topic

jar file classpath

Jon Parise
Ranch Hand

Joined: Jul 03, 2007
Posts: 81
Hey everyone,

I rarely make JAR files, but I currently need one. Anyways, I have forgotten a lot of things about their creation. The one thing I did figure out is that I need a manifest file with a classpath in it.

I need to point to some jar files that are located in C:/Program Files/OpenOffice.org 2.3/program/classes/

When I add this to my jar file's class path it doesn't seem to work. I am assuming it has something to do with the spaces. I tried putting "" around the whole thing as you would on the DOS prompt, but still no luck.

Every time I get a NoClassDefFoundError at runtime on the JAR.

Could someone refresh my memory and show me how to add C:/Program Files/OpenOffice.org 2.3/program/classes/ to my JAR file's CLASSPATH?

And before you ask I can't copy the JARs I need else where, they only run out of that particular folder, which is the source of many problems.


K. Tsang

Joined: Sep 13, 2007
Posts: 2383

Hi Jon, if you are using JDK6 and if your class files are located in C:/Program Files/OpenOffice.org 2.3/program/classes/
then try this:
jar cvfe <jarfilename>.jar <mainclass> -C "C:/Program Files/OpenOffice.org 2.3/program/classes/" .

If not create manifest file with the following lines:
Main-Class: <classname>
Class-Path: "C:/Program Files/OpenOffice.org 2.3/program/classes/"

then do
jar cvfm <jarfilename>.jar <manifestfilename> -C "C:/Program Files/OpenOffice.org 2.3/program/classes/"

The only thing I'm not sure of is if you need to specify individual jar files in the manifest's class path since you can't use .../*.jar while leaving as is just adds all files in that dir to your jar file.

Jar command description

Usage: jar {ctxui}[vfm0Me] [jar-file] [manifest-file] [entry-point] [-C dir] files ...
-c create new archive
-t list table of contents for archive
-x extract named (or all) files from archive
-u update existing archive
-v generate verbose output on standard output
-f specify archive file name
-m include manifest information from specified manifest file
-e specify application entry point for stand-alone application
bundled into an executable jar file
-0 store only; use no ZIP compression
-M do not create a manifest file for the entries
-i generate index information for the specified jar files
-C change to the specified directory and include the following file
If any file is a directory then it is processed recursively.
The manifest file name, the archive file name and the entry point name are
specified in the same order as the 'm', 'f' and 'e' flags.

Example 1: to archive two class files into an archive called classes.jar:
jar cvf classes.jar Foo.class Bar.class
Example 2: use an existing manifest file 'mymanifest' and archive all the
files in the foo/ directory into 'classes.jar':
jar cvfm classes.jar mymanifest -C foo/ .

Hopes this help

Jon Parise
Ranch Hand

Joined: Jul 03, 2007
Posts: 81
I'd like to use a manifest file for this since I am redistributing this JAR.

Currently, I have a JAR file that netbeans created. The only problem is that Netbeans wants to copy the files locally as I mentioned in another thread. I have found about a dozen other posts with peopel having the same netbeans issue.

Anyways, what I am now trying is this:

1) Extract the jar file with WinRAR/WinZip
2) Fix the incorrect classpath netbeans provided
3) zip the folder back into a zip file
4) Change the extension to .jar

This works, except it still can't find my jar files..

What I need to know is if within the manifest file I should put C:\Program Files or "C:\Program Files" or something else. Right now neither seems to work.

Here is the manifest(and yes it ends in a newline)

What makes this incredibly annoying is that it runs just fine in Netbeans, referencing the JARs like it should. However when it makes the JAR it sets the classpath incorrectly...
Consider Paul's rocket mass heater.
subject: jar file classpath