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

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,
g46905Asked:
Who is Participating?
 
zzynxConnect With a Mentor Software engineerCommented:
Maybe this can help too: http://mindprod.com/jgloss/jar.html
0
 
zzynxSoftware engineerCommented:
0
 
BreadstickCommented:
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
sudhakar_koundinyaCommented:
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
 
sudhakar_koundinyaCommented:
should be

Java -cvfm main.jar Main.mf A.class B.class C.class D.class
0
 
BreadstickCommented:
> 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
 
g46905Author Commented:
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
 
g46905Author Commented:
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
 
sudhakar_koundinyaCommented:
how u r executing??
0
 
sudhakar_koundinyaCommented:
set classpath=%classpath%;c:\jdk1.4\jre\lib\rt.jar;c:\yourjar.jar;

java -jar YourJar.jar
0
 
g46905Author Commented:
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
 
sudhakar_koundinyaCommented:
try double clicking that jar file

or javaw -jar jarfile
0
 
g46905Author Commented:
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
 
sudhakar_koundinyaCommented:
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
 
g46905Author Commented:
   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
 
manuel_mCommented:
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
 
manuel_mCommented:
<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
 
g46905Author Commented:
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
 
g46905Author Commented:
manuel,
           Is this line <attribute name="Built-By" value="${user.name}"/> necessary ini the manifest file?

Thanks,
0
 
manuel_mCommented:
>Is this line <attribute name="Built-By" value="${user.name}"/> necessary ini the manifest file?
no, only information who creates this file
0
 
manuel_mCommented:
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
 
g46905Author Commented:
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
 
g46905Author Commented:
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
 
BreadstickCommented:
Try using this in the manifest file:
Main-Class: org.xml.sax.ReportStats
0
 
BreadstickCommented:
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
 
manuel_mCommented:
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
 
g46905Author Commented:
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
 
CI-Ia0sCommented:
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
 
manuel_mCommented:
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
 
NaeemgCommented:
//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
 
zzynxSoftware engineerCommented:
Thanks
0
 
g46905Author Commented:
Thanks for your help
0
All Courses

From novice to tech pro — start learning today.