Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

jar file from Eclipse

Posted on 2009-02-12
30
Medium Priority
?
617 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
0
Comment
Question by:coca_cola
  • 14
  • 14
  • 2
30 Comments
 
LVL 92

Expert Comment

by:objects
ID: 23628338
you need to include it in the jars manifest

add it to your project so eclipse knows to include it

0
 
LVL 92

Expert Comment

by:objects
ID: 23628358
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

0
 
LVL 92

Expert Comment

by:objects
ID: 23628361
alternatively add the mysql driver to your jre's ext directory and it will get located automatically

0
Industry Leaders: 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!

 

Author Comment

by:coca_cola
ID: 23628365
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.
0
 
LVL 92

Assisted Solution

by:objects
objects earned 400 total points
ID: 23628382
0
 

Author Comment

by:coca_cola
ID: 23628542
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

0
 
LVL 92

Expert Comment

by:objects
ID: 23628549
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

0
 
LVL 92

Expert Comment

by:objects
ID: 23628552
> 'jar' is not recognized as an internal or external command,

add the jdk's bin directory to your PATH

0
 

Author Comment

by:coca_cola
ID: 23628632
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
0
 
LVL 92

Expert Comment

by:objects
ID: 23628670
sorry, you also need the Main-Class: attribute that was in there previously

Main-Class:

0
 

Author Comment

by:coca_cola
ID: 23628697
yes, I did

Main-Class: java.src.Main

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

0
 

Author Comment

by:coca_cola
ID: 23628704
I am not sure about this line:

 jar cfm test.jar Manifest.txt
0
 
LVL 92

Expert Comment

by:objects
ID: 23628720
Main-Class: Main

>  jar cfm test.jar Manifest.txt

you need to tell it the classes to add

0
 

Author Comment

by:coca_cola
ID: 23628761
Main-Class: Main

I tried, still
Exception in thread "main" java.lang.NoClassDefFoundError: Main
0
 
LVL 92

Expert Comment

by:objects
ID: 23628781
dod u change the jar command to specify the classes to add?

u can check they contents of jar with

jar tvf test.jar

0
 

Author Comment

by:coca_cola
ID: 23628800
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?
0
 

Author Comment

by:coca_cola
ID: 23628809
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
0
 
LVL 92

Expert Comment

by:objects
ID: 23628823
> 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

0
 

Author Comment

by:coca_cola
ID: 23628831
you not adding your class to the jar :)

what you mean?
0
 

Author Comment

by:coca_cola
ID: 23628838
you not adding your class to the jar :)

what you mean? do you mean "MyPackage/*.class" is required?
MyPackage is which package?
0
 
LVL 92

Expert Comment

by:objects
ID: 23628839
i mean you jar is empty. you need to tell it what classes you want to add to it.

0
 

Author Comment

by:coca_cola
ID: 23628852
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
0
 
LVL 92

Expert Comment

by:objects
ID: 23628853
> 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

0
 

Author Comment

by:coca_cola
ID: 23628870

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

Author Comment

by:coca_cola
ID: 23628877
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

0
 
LVL 92

Expert Comment

by:objects
ID: 23628892
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


0
 
LVL 86

Accepted Solution

by:
CEHJ earned 1600 total points
ID: 23630973
I recommend you get the FatJar plugin for Eclipse - it will make resolving the driver jar dependencies a lot easier
0
 

Author Comment

by:coca_cola
ID: 23635702
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

0
 
LVL 92

Expert Comment

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

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 23639795
:-)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month11 days, 6 hours left to enroll

572 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