Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Creating a JAR file to run from command line - Class not found

800467Sep 30 2009 — edited Sep 30 2009
Hi all

I have created a JAR file by using the following command line instructions in DOS:

jar cmf C:\Java\manifest.txt test.jar C:\Java\classes

The C:\Java\classes location contains my package structure e.g. com\level1\level2\level3\ which contains all the .class files that I am interested in including the class that I want to run which contains the main method: HelloWorld

I have specified in the manifest file the following:

Manifest-Version: 1.0
Created-By: 1.6.0 (Sun Microsystems Inc.)
Main-Class: com.level1.level2.level3.HelloWorld

Now when I try and run the jar file from the command line using the following command:

java -jar test.jar HelloWorld

I get the following exception:

Caused by: java.lang.ClassNotFoundException: HelloWorld
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
Could not find the main class: HelloWorld. Program will exit.

I cannot understand why this is happening. I looked inside the JAR file - should there be a package structure in there for the HelloWorld class? Currently there is not one but I thought the creation of the JAR file using the command above this would be done automatically?

Thanks for any suggestions...
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 28 2009
Added on Sep 30 2009
14 comments
686 views