Solved

creating a .jar file and executing it to run the java application

Posted on 2004-10-13
32
222 Views
Last Modified: 2013-11-23
My application uses four classes( for ex A,B,C,D).To run my application, I run the class(A) where I have the main method.I am trying to put them in a jar file and run the application. Can you please tell me how to acheive this?

Thanks,
0
Comment
Question by:g46905
  • 11
  • 6
  • 6
  • +4
32 Comments
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
0
 
LVL 37

Accepted Solution

by:
zzynx earned 125 total points
Comment Utility
Maybe this can help too: http://mindprod.com/jgloss/jar.html
0
 
LVL 2

Expert Comment

by:Breadstick
Comment Utility
Create a file called manifest.mf.  In it, put the following line:
Main-Class: A

Also, be sure to leave a blank line undernieth it.  Then from the command line, cd to the directory containing your class files, and type the following:
jar cfm MyJar.jar manifest.mf *.class

You can use whatever name you want instead of MyJar.jar.

To run the jar file, type:
java -jar MyJar.jar

I hope that helps,
Breadstick
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
Comment Utility
Create Manifest file say Main.mf with following line

Main-Class: A

create Jar file

Java -cvfm Main.mf A.class B.class C.class D.class
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
Comment Utility
should be

Java -cvfm main.jar Main.mf A.class B.class C.class D.class
0
 
LVL 2

Expert Comment

by:Breadstick
Comment Utility
> Java -cvfm main.jar Main.mf A.class B.class C.class D.class

That's incorrect.  "Java" isn't used to create jar files.  jar.exe is what must be used.

To create the jar file:
jar cfm MyJar.jar manifest.mf *.class

To run it:
java -jar MyJar.jar
0
 

Author Comment

by:g46905
Comment Utility
I have followed all the procedures mentioned above.For some reason, It gets class not found eventhough I could see all the classes in the .jar file. Any thoughts about why it is throwing java.lang.NoClassDefFoundError: org/xml/sax/SAXException?  thanks
0
 

Author Comment

by:g46905
Comment Utility
When I try to run the appliation by extracting all the classes(A,B,C,D) in the same location where where my .jar is, it run fine. I am not why this is not running when I try to run it using .jar. Can you please share your thoughts/suggestions ?

Thanks
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
Comment Utility
how u r executing??
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
Comment Utility
set classpath=%classpath%;c:\jdk1.4\jre\lib\rt.jar;c:\yourjar.jar;

java -jar YourJar.jar
0
 

Author Comment

by:g46905
Comment Utility
The application is accessing the main method(class).I am calling another class and that class in not being found.I see all the classes in the .jar file. Please let me know if I am doing something incorrect.

Thanks,
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
Comment Utility
try double clicking that jar file

or javaw -jar jarfile
0
 

Author Comment

by:g46905
Comment Utility
Sudhakar,
              Are you trying to tell me that doing this way, I will be able to see wht classes are in the .jar file?

Thanks
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
Comment Utility
No,

I am saying that to execute. The above are some more options to execute the jar file. If u test like that and find problems still, let me know.

So that I can try.

BTW, may i see ur manifest file??
0
 

Author Comment

by:g46905
Comment Utility
   javaw:  not found is the result when Itry javaw -jar jarfile.

   When I use java -jar jarfile, you can see the result below.

java.lang.NoClassDefFoundError: org/xml/sax/SAXException
        at ReportStats.loadAllProfiles(ReportStats.java:1641)
        at ReportStats.parseArgs(Compiled Code)
        at ReportStats.main(Compiled Code)
org/xml/sax/SAXException


If we see the error below,it look like it is missing the libraries required.There is a file called classpath in the .jar file.This is what I set the path to be in the classpath file.Please let me know if you have any questions.

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" path=""/>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
    <classpathentry kind="lib" path="./bin/crimson.jar"/>
    <classpathentry kind="lib" path="./bin/dom.jar"/>
    <classpathentry kind="lib" path="./bin/jaxp-api.jar"/>
    <classpathentry kind="lib" path="./bin/sax.jar"/>
    <classpathentry kind="lib" path="./bin/xercesImpl.jar"/>
    <classpathentry kind="output" path=""/>
</classpath>





       
0
 
LVL 6

Expert Comment

by:manuel_m
Comment Utility
you can set the library in the classpath of the manifest file.
set all needed libs in the classpath as shown below

Class-Path: bin/sax.jar bin/crimson.jar

the libs have to be in this folder.

another possibility is to extract the libs and put the unjared libraries of dom.jar and sax.jar, etc. into the jar. so you don't have to set a classpath and your jar is executable anywhere.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 6

Expert Comment

by:manuel_m
Comment Utility
<manifest file="MANIFEST.MF">
    <attribute name="Built-By" value="${user.name}"/>
    <attribute name="Class-Path" value="bin/dom.jar bin/sax.jar"/>
</manifest>

not all libs are listed here. you have to complete it.
0
 

Author Comment

by:g46905
Comment Utility
I will try to set the Class-Path: .................;.............; in the manifest file and will let you know about the result.

Could you please tell me what is the use of having classpath file in the .jar folder in we can set the class path in manifest file(Class-Path) ?

Thanks,
0
 

Author Comment

by:g46905
Comment Utility
manuel,
           Is this line <attribute name="Built-By" value="${user.name}"/> necessary ini the manifest file?

Thanks,
0
 
LVL 6

Expert Comment

by:manuel_m
Comment Utility
>Is this line <attribute name="Built-By" value="${user.name}"/> necessary ini the manifest file?
no, only information who creates this file
0
 
LVL 6

Expert Comment

by:manuel_m
Comment Utility
if you set the Class-Path in the manifest file, the application knows where to search for the libraries he needs to run.
i created in my application a manifest file where i set the Class-Path as followed:

in manifest file:

Class-Path: jdom.jar forms.jar looks.jar pdflib.jar

in the folder where my jar of the application is, are also the libs jdom.jar, forms.jar, looks.jar and pdflib.jar. This guarantees that my application runs because it can find the libs.
0
 

Author Comment

by:g46905
Comment Utility
This is what I have in the manifest file.

Manifest-Version: 1.0
Main-Class: ReportStats
Class-Path: bin/sax.jar bin/crimson.jar bin/dom4j-1.5-rc1.jar bin/xercesImpl.jar bin/xml-apis.jar

here is the error being generated while running the .jar file.

Failed to load Main-Class manifest attribute from

Can you please tell me why this is happening?

Thanks

0
 

Author Comment

by:g46905
Comment Utility
This is what I finally see .Can someone please tell me why this is occuring?

java.lang.NoClassDefFoundError: org/w3c/dom/events/EventTarget
        at java.lang.ClassLoader.defineClass0(Native Method)
        at java.lang.ClassLoader.defineClass0(Compiled Code)
        at java.lang.ClassLoader.defineClass(Compiled Code)
        at java.security.SecureClassLoader.defineClass(Compiled Code)
        at java.net.URLClassLoader.defineClass(Compiled Code)
        at java.net.URLClassLoader.access$1(Compiled Code)
        at java.net.URLClassLoader$1.run(Compiled Code)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.AccessController.doPrivileged(Compiled Code)
        at java.net.URLClassLoader.findClass(Compiled Code)
        at java.lang.ClassLoader.loadClass(Compiled Code)
        at sun.misc.Launcher$AppClassLoader.loadClass(Compiled Code)
        at java.lang.ClassLoader.loadClass(Compiled Code)
        at java.lang.ClassLoader.loadClassInternal(Compiled Code)
        at java.lang.ClassLoader.defineClass0(Native Method)
        at java.lang.ClassLoader.defineClass0(Compiled Code)
        at java.lang.ClassLoader.defineClass(Compiled Code)
        at java.security.SecureClassLoader.defineClass(Compiled Code)
        at java.net.URLClassLoader.defineClass(Compiled Code)
        at java.net.URLClassLoader.access$1(Compiled Code)
        at java.net.URLClassLoader$1.run(Compiled Code)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.AccessController.doPrivileged(Compiled Code)
        at java.net.URLClassLoader.findClass(Compiled Code)
        at java.lang.ClassLoader.loadClass(Compiled Code)
        at sun.misc.Launcher$AppClassLoader.loadClass(Compiled Code)
        at java.lang.ClassLoader.loadClass(Compiled Code)
        at java.lang.ClassLoader.loadClassInternal(Compiled Code)
        at java.lang.ClassLoader.defineClass0(Native Method)
        at java.lang.ClassLoader.defineClass0(Compiled Code)
        at java.lang.ClassLoader.defineClass(Compiled Code)
        at java.security.SecureClassLoader.defineClass(Compiled Code)
        at java.net.URLClassLoader.defineClass(Compiled Code)
        at java.net.URLClassLoader.access$1(Compiled Code)
        at java.net.URLClassLoader$1.run(Compiled Code)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.AccessController.doPrivileged(Compiled Code)
        at java.net.URLClassLoader.findClass(Compiled Code)
        at java.lang.ClassLoader.loadClass(Compiled Code)
        at sun.misc.Launcher$AppClassLoader.loadClass(Compiled Code)
        at java.lang.ClassLoader.loadClass(Compiled Code)
        at java.lang.ClassLoader.loadClassInternal(Compiled Code)
        at java.lang.ClassLoader.defineClass0(Native Method)
        at java.lang.ClassLoader.defineClass0(Compiled Code)
        at java.lang.ClassLoader.defineClass(Compiled Code)
        at java.security.SecureClassLoader.defineClass(Compiled Code)
        at java.net.URLClassLoader.defineClass(Compiled Code)
        at java.net.URLClassLoader.access$1(Compiled Code)
        at java.net.URLClassLoader$1.run(Compiled Code)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.AccessController.doPrivileged(Compiled Code)
        at java.net.URLClassLoader.findClass(Compiled Code)
        at java.lang.ClassLoader.loadClass(Compiled Code)
        at sun.misc.Launcher$AppClassLoader.loadClass(Compiled Code)
        at java.lang.ClassLoader.loadClass(Compiled Code)
        at java.lang.ClassLoader.loadClassInternal(Compiled Code)
        at XmlParsing.Upload(XmlParsing.java:110)
        at ReportStats.loadAllProfiles(ReportStats.java:1642)
        at ReportStats.parseArgs(Compiled Code)
        at ReportStats.main(Compiled Code)
org/w3c/dom/events/EventTarget
0
 
LVL 2

Expert Comment

by:Breadstick
Comment Utility
Try using this in the manifest file:
Main-Class: org.xml.sax.ReportStats
0
 
LVL 2

Expert Comment

by:Breadstick
Comment Utility
The bin directory isn't in your jar file is it? It should be outside the jar file.  The jar file should be in the same directory that the bin directory is in.
0
 
LVL 6

Expert Comment

by:manuel_m
Comment Utility
do you have a package ?
where is your main class?

if you have a package like "com.yourpackage.something" and your main class is in the Java Class "Mainclass" then your main-class should be.

Main-Class: com.yourpackage.something.Mainclass

---

try this:
set the classpath in your manifest file like i mentioned before.

Class-Path:  sax.jar crimson.jar dom4j-1.5-rc1.jar xercesImpl.jar xml-apis.jar

copy your generated jar and all the libs in one folder and try again.
0
 

Author Comment

by:g46905
Comment Utility
That is what exactly what I have done earlier and got the above result. I am not sure what else to thinkof at this moment.I am positive that it is not able to access some of the library files duringthe runtime.Please correct me if I am understanding it incorrect.Any other valuable suggestions?

Thanks
0
 
LVL 3

Expert Comment

by:CI-Ia0s
Comment Utility
It seems that it IS managing to load the main class alright but has a problem with something else...
>>at ReportStats.loadAllProfiles(ReportStats.java:1642)

What's on line 1642 of ReportStats?
0
 
LVL 6

Expert Comment

by:manuel_m
Comment Utility
please tell me where you have your libs and how your package looks like.

when does the error occur? when you double click the jar or if you compile it?
are you sure that the "org/w3c/dom/events/EventTarget" is in one of the libs?

dom4j-1.5-rc1.jar ---> new lib is out now: dom4j-1.5.jar

did you ensure that your "normal" classpath is set to the java home dir?
0
 
LVL 5

Expert Comment

by:Naeemg
Comment Utility
//save the following lines in "MANIFEST.MF" file

Manifest-Version: 1.0
Main-Class: A
Created-By: 1.4.0-beta (Sun Microsystems Inc.)


//and save the following lines in "MakeJar.bat"

jar cvfm newFile.jar MANIFEST.MF A.class B.class C.class

//now save it and run by double clicking, after ur jar is made, create the "RunJar.bat" and enter the followin lines.

java -jar newFile.jar

//Naeem Shehzad Ghuman
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
Thanks
0
 

Author Comment

by:g46905
Comment Utility
Thanks for your help
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
The viewer will learn how to implement Singleton Design Pattern in Java.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now