We help IT Professionals succeed at work.

jar file from Eclipse

coca_cola
coca_cola asked
on
Medium Priority
625 Views
Last Modified: 2012-05-06
I can export an executable jar file of HelloWorld.java by following the link:
http://www.cs.uta.fi/pgui/2005/local-uta/tutorials.html

then, I tried to export jar file of my own project which have more classes under class path.
I followed the link above to created test.jar.

then I copy all of the classes under my project class path to C:\Documents and Settings\s\Desktop, where test.jar located,

under cmd, I run java -jar test.jar

I got

C:\Documents and Settings\s\Desktop\New Folder>java -jar lina.jar
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
Exception in thread "main" java.lang.NoClassDefFoundError: a/b/C
        at Main.main(Main.java:29)

but I have mysql-connector-java-5.1.0-bin.jar under C:\Documents and Settings\s\Desktop\New Folder;

I also have the jar file contains a/b/C under the folder

why the classes are not found? thanks
Comment
Watch Question

Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
you need to include it in the jars manifest

add it to your project so eclipse knows to include it

Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
actually eclipse may not support it. in which case you will need to create a manifest file yourself
it needs to contain the line:

Class-Path: mysql-connector-java-5.1.0-bin.jar

And keep the generated jar and the mysql driver in the same directory

Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
alternatively add the mysql driver to your jre's ext directory and it will get located automatically

Author

Commented:
you need to include it in the jars manifest
I don't understand what you mean.

add it to your project so eclipse knows to include it
Yes, I did add it to my project under class path.
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
I read your link,

in the folder where my jars needed and the test.jar created by eclispe, I created Manifest.txt
with lines:
Class-Path: mysql-connector-java-5.1.0-bin.jar
Class-Path: 1.jar
Class-Path: 2.jar

and under cmd:
C:\Documents and Settings\s\Desktop\New Folder>jar cfm test.jar Manifest.tx
t *.class

I got :

'jar' is not recognized as an internal or external command,
operable program or batch file.

I really don't understand  "We then create a JAR file named MyJar.jar by entering the following command: " from your link

thanks

jar cfm MyJar.jar Manifest.txt MyPackage/*.class

Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
you can still create it from eclipse
it gives you an option to specify the manifest to use



Class-Path: mysql-connector-java-5.1.0-bin.jar
Class-Path: 1.jar
Class-Path: 2.jar


they need to all ne on one line
with a coupkle of blank lines following

Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> 'jar' is not recognized as an internal or external command,

add the jdk's bin directory to your PATH

Author

Commented:
ok, jar is recognized now
:-)

cmd:
 jar cfm test.jar Manifest.txt
java -jar test.jar

I got:Failed to load Main-Class manifest attribute from
test.jar
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
sorry, you also need the Main-Class: attribute that was in there previously

Main-Class:

Author

Commented:
yes, I did

Main-Class: java.src.Main

Exception in thread "main" java.lang.NoClassDefFoundError: java/src/Main

Author

Commented:
I am not sure about this line:

 jar cfm test.jar Manifest.txt
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
Main-Class: Main

>  jar cfm test.jar Manifest.txt

you need to tell it the classes to add

Author

Commented:
Main-Class: Main

I tried, still
Exception in thread "main" java.lang.NoClassDefFoundError: Main
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
dod u change the jar command to specify the classes to add?

u can check they contents of jar with

jar tvf test.jar

Author

Commented:
dod u change the jar command to specify the classes to add?

I did:

 jar cfm test.jar Manifest.txt

instead of  jar cfm MyJar.jar Manifest.txt MyPackage/*.class, which is from your link.

I don't understand MyPackage/*.class, how it applys to my case?

Author

Commented:
jar tvf test.jar
     0 Thu Feb 12 17:10:58 PST 2009 META-INF/
   132 Thu Feb 12 17:10:58 PST 2009 META-INF/MANIFEST.MF

I don;t understand the output
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> I don't understand MyPackage/*.class, how it applys to my case?

thats what tells it what classes to add.
you not adding your class to the jar :)

> I don;t understand the output

its listing the files and directories in the jar

Author

Commented:
you not adding your class to the jar :)

what you mean?

Author

Commented:
you not adding your class to the jar :)

what you mean? do you mean "MyPackage/*.class" is required?
MyPackage is which package?
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
i mean you jar is empty. you need to tell it what classes you want to add to it.

Author

Commented:
you need to tell it what classes you want to add to it.

how?

jar cfm test.jar Manifest.txt MyPackage/*.class

how to change "MyPackage/*.class" to fit my case, MyPackage is which package?

thanks
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> what you mean? do you mean "MyPackage/*.class" is required?

in your case you don't have a package so just *.class

assuming you are in the directory that contains your class and manifest use:

 jar cfm test.jar Manifest.txt *.class

Author

Commented:

I did:
jar cfm test.jar Manifest.txt *.class
*.class : no such file or directory

Author

Commented:
assuming you are in the directory that contains your class and manifest use:

in the directory contains jars and meifest, no class in this directory

Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
then you need to tell it *where* your files are you want to include in the jar :)

easiest is to run it from the directory containing the classes

jar cfm   *.class

Or use eclipses export option


CERTIFIED EXPERT
Top Expert 2016
Commented:
I recommend you get the FatJar plugin for Eclipse - it will make resolving the driver jar dependencies a lot easier

Author

Commented:
easiest is to run it from the directory containing the classes

"the directory containing the classes", do you mean the directory containing the jars which I need to include?    I found you said "classes", but there is no any class under my working diretory.

Yes, I did run in the directory containing the jars which I need to include.

from u ---"jar cfm <location of manifest> <location of jar> *.class"
since I run in the directory containing the jars which I need to include, and Manifest.txt is in the same directory, I don't need  <location of manifest> <location of jar>, so I use:
jar cfm test.jar Manifest.txt *.class
I got :
*.class : no such file or directory


 
Or use eclipses export option

Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
don't use fatjar, it has a number of well documented short comings.
End users in particular do not like apps deployed with it.

CERTIFIED EXPERT
Top Expert 2016

Commented:
:-)
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.