ram
ram

Reputation: 11

Problem when creating Jar file using Command Line

I have tried to create Jar file using Command Line.

My Manifest file:

Manifest-Version: 1.0
Created-By: 1.6.0 (Sun Microsystems Inc.)
Main-Class:Home

My Files and Location of that files:

Location : D:\Application Files :

 images
     add.png
     home.png
     minus.png
 Database.java
 Home.java
 UiDesign.java
 Database.class
 Home.class// This is my main class
 UiDesign.class
 Manifest.txt
 mysql-connector-java-5.1.15-bin.jar

To create jar file i tried:

D:\Application>jar cmf Manifest.txt MyApp.jar *.class mysql-connector-java-5.1.15-bin.jar images

But the jar file is created. If i click that jar file, Error message shows like below,

Failed to Load Main-Class manifest attribute from D:\Application\MyApp.jar

Thank for all....My problem solved by adding new line in Manifest File....Thank you all...

But now i have another problem.....

D:\JavaApplication-13-8-2011\Application>jar cfm  MyApp.jar Manifest.txt *.class
 mysql-connector-java-5.1.15-bin.jar images

D:\JavaApplication-13-8-2011\Application>java -jar MyApp.jar
Connect to MySQl
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Unknown Source)
        at Database.getDBConnection(Database.java:14)
        at UiDesign.<init>(UiDesign.java:58)
        at Home.main(Home.java:6)
java.lang.NullPointerException
        at Database.getBrand(Database.java:31)
        at UiDesign.<init>(UiDesign.java:59)
        at Home.main(Home.java:6)
Exception in thread "main" java.lang.NullPointerException
        at UiDesign.<init>(UiDesign.java:64)
        at Home.main(Home.java:6)

D:\JavaApplication-13-8-2011\Application>

I think this problem occurs due to to class path not set to mysql-connector....I have added this jar file in my Application.....How to setclass path and run my Application jar file successfully.....

please help me....

Thank you...I cleared..this problem also...As Trisstan said, I added classpath in Manifest file...Now My Application Jar file Run Succesfully.....

Thank You all for your Quick Response......

Upvotes: 1

Views: 2054

Answers (4)

jackrabbit
jackrabbit

Reputation: 5663

For your second problem:

Your jar probably contains a copy of the mysql jar. That does not work because jars are supposed to contain classes. The simplest way to fix this is to indicate in your manifest that your code depends on the mysql jar. Basically add the following:

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

In your jar creation command, do not include the mysql jar. Finally, make sure that your jar and the mysql one are distributed together (in the same directory).

If you really want a single jar, extract the mysql jar and include its contents in the jar you are going to distribute.

Upvotes: 0

Tristan
Tristan

Reputation: 9111

It may be an error about classpath, because your jar depends on mysql-connector-java-5.1.15-bin.jar

Upvotes: 1

Baz1nga
Baz1nga

Reputation: 15579

make sure there is a carriage return in your manifest file and I am also guessing your jar file name should precede the manifest file as your option says cfm.

Failed to Load Main-Class manifest attribute from D:\Application\MyApp.jar

From the error it looks like it thinks that the jar file is your manifest file. That should be the problem.

update the argument or change the option parameter to mcf I guess..

Upvotes: 0

miku
miku

Reputation: 188014

Add a newline at the end of your manifest file.

Warning: The text file must end with a new line or carriage return. The last line will not be parsed properly if it does not end with a new line or carriage return.

From: http://download.oracle.com/javase/tutorial/deployment/jar/appman.html

Upvotes: 2

Related Questions