Solved

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

Posted on 2004-10-13
32
245 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 6
  • 6
  • +4
32 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 12299589
0
 
LVL 37

Accepted Solution

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

Expert Comment

by:Breadstick
ID: 12299683
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12299698
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
ID: 12299707
should be

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

Expert Comment

by:Breadstick
ID: 12299806
> 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
ID: 12300922
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
ID: 12301199
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
ID: 12301208
how u r executing??
0
 
LVL 14

Expert Comment

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

java -jar YourJar.jar
0
 

Author Comment

by:g46905
ID: 12301294
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
ID: 12301571
try double clicking that jar file

or javaw -jar jarfile
0
 

Author Comment

by:g46905
ID: 12301788
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
ID: 12301910
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
ID: 12302103
   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
ID: 12302249
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
 
LVL 6

Expert Comment

by:manuel_m
ID: 12302288
<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
ID: 12302308
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
ID: 12302325
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
ID: 12302375
>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
ID: 12302434
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
ID: 12302525
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
ID: 12303117
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
ID: 12303122
Try using this in the manifest file:
Main-Class: org.xml.sax.ReportStats
0
 
LVL 2

Expert Comment

by:Breadstick
ID: 12303140
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
ID: 12303343
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
ID: 12303706
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
ID: 12303770
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
ID: 12303892
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
ID: 12305437
//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
ID: 12346506
Thanks
0
 

Author Comment

by:g46905
ID: 12346544
Thanks for your help
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
servlet example 11 76
iterator/ListIterator approach 17 68
ejb entity bean example 2 57
netbeans can not run self signed app 8 38
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

759 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