jQuery in Action, 3rd edition
The moose likes Java in General and the fly likes jar creation with subdirectories Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "jar creation with subdirectories" Watch "jar creation with subdirectories" New topic

jar creation with subdirectories

K. Tsang

Joined: Sep 13, 2007
Posts: 3132

Hello all, not sure if the captioned topic is considered as intermediate or not but here I go. I have set up my file structure on Windows as something like *.java goes inside /src dir; *.class goes inside /class dir; and the jar file goes inside the /dist dir.

I did the following to put the files into proper location like this:
javac -d class src/MyClass.java
jar -cvfe dist/MyClass.jar MyClass class/MyClass.class

Then when I try running using java -jar dist/MyClass.jar it can't find the main class??

Exception in thread "main" java.lang.NoClassDefFoundError: MyClass
Caused by: java.lang.ClassNotFoundException: MyClass
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)

However, if I don't use subdirectories then everything works fine.
javac -d . src/MyClass.java
jar -cvfe MyClass.jar MyClass MyClass.class
java -jar MyClass.jar

Can anyone please help to clarify what I'm doing wrong or the correct to create jar files with class files inside some directory. Thanks.

K. Tsang JavaRanch SCJP5 SCJD OCPJP7 OCPWCD5 OCPBCD5 OCPWSD5 OCMJEA5 part 1 part 2/3
Jorge Pinho

Joined: May 16, 2003
Posts: 25

Hi K.

java -jar dist/MyClass.jar also needs a classpath...

java -cp dist -jar MyClass.jar

Hope it works, or its something else, lets see...

Jorge Pinho / SCJP 5
Live Help via Skype - lets.talk.about.help
Dont wait for forum answers
Joanne Neal

Joined: Aug 05, 2005
Posts: 3742
Originally posted by Jorge Pinho:
Hi K.

java -jar dist/MyClass.jar also needs a classpath...

java -cp dist -jar MyClass.jar

Hope it works, or its something else, lets see...

No. If you use the -jar option then the classpath and the main class have to be specified in the jar's manifest file.

java command line options
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.

K. Tsang

Joined: Sep 13, 2007
Posts: 3132

Guess what using java -cp wouldn't work. Adding the class-path thing in manifest wouldn't make a difference. It's something to with the -C option.

Suppose I'm at D:\projects\java\test directory and want to run HelloWorld (yes the most simplest of all programs lol).

File structure is like:

first compile
D:\projects\java\test>javac -d cls src/*.java

then create jar
D:\projects\java\test>jar cvfe dist/HelloWorld.jar HelloWorld -C cls .
added manifest
adding: HelloWorld.class(in = 425) (out= 286)(deflated 32%)

then run jar
D:\projects\java\test>java -jar dist/HelloWorld.jar
Hello World

for myself
[ March 04, 2008: Message edited by: K. Tsang ]
I agree. Here's the link: http://aspose.com/file-tools
subject: jar creation with subdirectories
It's not a secret anymore!