File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Executing a jar file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Executing a jar file" Watch "Executing a jar file" New topic
Author

Executing a jar file

Vinod Chamoli
Greenhorn

Joined: Jun 09, 2003
Posts: 21
Hello,
I want to create a jar file which can be run simply by clicking on it.
Here are the steps which I have followed.
I have created a manifest file which has a listing of main class.
Created jar file using -jar option available in java.
Now the problem is when I try to run this jar file, it gives this error:
Exception in thread "main" java.lang.NoClassDefFoundError:
Though all necessary jar files are included in classpath.
Small Note: When I try to run class files, it runs fine.
Pls help me out.
Thanks and Regards,
M.K.A. Monster
Ranch Hand

Joined: May 02, 2002
Posts: 130
I think you have a problem with your class-path.
Arif Shaikh
Greenhorn

Joined: Dec 31, 2002
Posts: 1
YOu have to add one entry for main class in you Manifest.mf file
M.K.A. Monster
Ranch Hand

Joined: May 02, 2002
Posts: 130
I have created a manifest file which has a listing of main class.

He says he has added this thing in the manifest file.
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
How did you create the jar file?
Does the Jar file structure follows the package structure?
Also, did you run the application using:
java -jar [your_jar.jar] [your_application]
instead of
java -classpath [your_jar.jar] [your_application]
Nick


SCJP 1.2, OCP 9i DBA, SCWCD 1.3, SCJP 1.4 (SAI), SCJD 1.4, SCWCD 1.4 (Beta), ICED (IBM 287, IBM 484, IBM 486), SCMAD 1.0 (Beta), SCBCD 1.3, ICSD (IBM 288), ICDBA (IBM 700, IBM 701), SCDJWS, ICSD (IBM 348), OCP 10g DBA (Beta), SCJP 5.0 (Beta), SCJA 1.0 (Beta), MCP(70-270), SCBCD 5.0 (Beta), SCJP 6.0, SCEA for JEE5 (in progress)
Vinod Chamoli
Greenhorn

Joined: Jun 09, 2003
Posts: 21
I have checked my classpath before running also I tried to run this using
java -classpath -jar jarname
but still its not working.
pls help
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
Is that your JAR file contains all necessary classes for your application?
If so, you dont need -classpath.
Nick
J�rg Vater
Greenhorn

Joined: Mar 17, 2004
Posts: 10
Did You put the full qualified name of Your 'main-class' in the manifestfile? You have to do this.
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
More specifically, you need to put the Class that contains the method:
public static void main(String args[])
in the MANIFEST.
Nick
Vinod Chamoli
Greenhorn

Joined: Jun 09, 2003
Posts: 21
I have considered all the above things
timothy zimmerman
Ranch Hand

Joined: Jun 26, 2001
Posts: 149
One suggestion I don't see here is the Class-Path entry in the manifest file.
Have you tried setting your classpath there. Below is an example
Manifest-Version: 1.0
Created-By: Apache Ant 1.5
Main-Class: Main
Class-Path: castor-0.9.5.jar dom4j.jar c3p0.jar commons-collections.ja
r odmg.jar xerces.jar xalan.jar commons-logging.jar hibernate2.jar eh
cache.jar cglib2.jar db2jcc.jar db2jcc_license_cu.jar j2ee.jar Absolu
teLayout.jar
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

First: I guess this is a beginners question. Can someone move it?
Second: If you put a blank after the name of the class which contains a main-method, (or .class) in the manifest, it will fail.
(edited for typo)
[ March 25, 2004: Message edited by: Stefan Wagner ]

http://home.arcor.de/hirnstrom/bewerbung
Vinod Chamoli
Greenhorn

Joined: Jun 09, 2003
Posts: 21
This executable jar file was first created by using Webspher studio, and latter I have created using -jar option.
If this is a begginers question, then pls try solving it.
Beatrice Ng
Greenhorn

Joined: Jun 20, 2003
Posts: 28
Have you tried explicitly specify full path of your "java".exe?
Brian Pipa
Ranch Hand

Joined: Sep 29, 2003
Posts: 299
Post your manifest for us to see!!!


My Java Freeware:<br />MACCC - <a href="http://maccc.pipasoft.com" target="_blank" rel="nofollow">http://maccc.pipasoft.com</a><br />Nator - <a href="http://nator.pipasoft.com" target="_blank" rel="nofollow">http://nator.pipasoft.com</a>
Vinod Chamoli
Greenhorn

Joined: Jun 09, 2003
Posts: 21
Manifest-Version: 1.0
Main-Class: MainClassName
Here is the Manifest file.
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

Your Main-classes name is really 'MainClassName'? (don't really believing).
Try

and post the 'test.tmp' to the forum.
It's essential to use the right starting-directory when creating the jar, using paths and jars.
Don't replace the output with something more readable when posting to the forum. This is polite and appropriate, when first asking your question, but since the answers don't help, we have to get into the details.
Vinod Chamoli
Greenhorn

Joined: Jun 09, 2003
Posts: 21
Here are the details of the temp file.
META-INF/MANIFEST.MF
chatdev/ChatClient$1.class
chatdev/ChatClient$2.class
chatdev/ChatClient$CloseListener.class
chatdev/ChatClient$EnterListener.class
chatdev/ChatClient$SendListener.class
chatdev/ChatClient.class
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

Ok.
I assume your main-class is:
chatdev.ChatClient
am I true?
And you start it from

sucessfully?
Only to be sure:
Your Manifestfile contains:

And the jar is called 'chatdev.jar'?
so you try to run:

?
[ March 30, 2004: Message edited by: Stefan Wagner ]
Vinod Chamoli
Greenhorn

Joined: Jun 09, 2003
Posts: 21
Yeah your are right, but it does not work.
Using java -jar chatdev.jar it does not run. It throws exception.
Mrityunjay Prasad
Greenhorn

Joined: Jan 21, 2004
Posts: 4
It seems your jar is dependent on some external jar. In that case try this:
c:\>java -cp yourFile.jar;dependentFile.jar yourpack.yourMainClass
Vinod Chamoli
Greenhorn

Joined: Jun 09, 2003
Posts: 21
It worked, thanks for your efforts.
But still one thing I dont understand, we have all jars in our classpath still it does not work, Can any one help me in solving this question.
Thanks
Dave Newton
Greenhorn

Joined: Mar 31, 2004
Posts: 1
Sheesh, you guys... this ain't hard.
When you execute java with the "-jar" flag it ignores your environment CLASSPATH variable and the java "-cp" and "-classpath" flags.
Using the Class-Path manifest file entry is one way around this. The class path entries are filesystem based, relative to where the jar file is being executed from.
Another way is to unjar dependent jars and jar them back up into the application jar. Another way is to provide a simple executable (exe, bat, sh, whatever is suitable for any likely target environments) that does it the old-fashioned way. Another way is WebStart.
Lots of solutions. I personally like the "wrapper that calls java with the classname" as that provides a lot more flexibility in terms of setting the classpath, using or integrating the user's existing CLASSPATH, etc.
Dave
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Executing a jar file
 
Similar Threads
How to run a program contained in a jar file
problem with jar
Problems running jar file
Can't get a jar to run with dependencies
how can i find a file in jar