This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Error in running Jar File. Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Error in running Jar File." Watch "Error in running Jar File." New topic
Author

Error in running Jar File.

Anshuman Chakraborty
Greenhorn

Joined: Oct 05, 2010
Posts: 20
Hi,

I am currently running on Windows OS. I am having problem running a jar file (Main.jar) which is importing class from a different jar file (Util.jar).

Hierarchy goes like this:-

Relative path (from current directory):-
1. lib/Util.jar
2. Main.jar

i. Util.jar contains
a. classfile :- com/util/DateTime
b. META-INF/MANIFEST.MF ( Main-Class: com.util.DateTime)

Code :-

package com.util;
import java.util.Date;

public class DateTime{
public String getDate(){
return new Date().toString();
}
}


ii. Main.jar contains
a. classfile :- com/main/Main
b. META-INF/MANIFEST.MF (Main-Class: com.main.Main)

Code :-

package com.main;
import com.util.DateTime;

public class Main{
public static void main(String[] args){
System.out.println("DateTime "+ new DateTime().getDate());
}
}

I am able to run the Main file using class hierarchy from the current directory.
Command goes like this
D:\>java -cp "lib/Util.jar;." com.main.Main // this command is successful.

But while running the same command using jar file i am getting an Error.
Command goes like this

D:\>java -cp "lib/Util.jar;." -jar Main.jar

The Exception generated is
NoClassDefFoundError: com/util/DateTime
at com.main.Main.main(Main.java :5)
.......

I have also tried replacing -cp with -classpath but the problem persists.

There is no problem with either of the jar files.
But don't know where i am lacking....


Please Help !!!




Anshuman Chakraborty
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3011
    
    9
The -cp option is ignored if you use the -jar option. You have to specify your classpath in the manifest file of main.jar.


Joanne
Anshuman Chakraborty
Greenhorn

Joined: Oct 05, 2010
Posts: 20
Dear Joanne

Thank you for your quick response.

I modified my jar file and now Manifest.MF contains

Manifest-Version: 1.0
Created-By: 1.6.0_17 (Sun Microsystems Inc.)
Main-Class: com.main.Main
Class-Path: lib/Util.jar


But still I am getting the same Exception.


Please Help !!!.

Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24081
    
  15

Try to run the app with both jar files in the -cp argument, with no "." and no -jar flag, specifying the main class on the command line. If that doesn't work, then the jar files don't contain what you think they do-- for example, the DateTime class folder hierarchy is wrong.


[Jess in Action][AskingGoodQuestions]
Anshuman Chakraborty
Greenhorn

Joined: Oct 05, 2010
Posts: 20
Hi Ernest,

It worked. But I was little disappointed since I wanted to execute jar using -jar option.
In the process of my Exploration I found that The contents of the manifest must be encoded in UTF8.

For jar creation I used External file and it contained

Manifest-Version: 1.0
Main-Class: com.main.Main
Class-Path: Util.jar

My default encoding is Windows 1252. During my jar creation I had two very interesting observations.

1. When the external file was created using Windows 1252 encoding. The jar creation process was successful but, the META-INF\MANIFEST.MF did not contain Class-Path: Util.jar

2. When the external file was created using UTF8 (I used an editor that allowed this conversion.). The jar creation process failed and below error was generated

java.io.IOException: invalid header field name: Manifest-Version
at java.util.jar.Attributes.read(Attributes.java:416)
at java.util.jar.Manifest.read(Manifest.java:182)
at java.util.jar.Manifest.<init>(Manifest.java:52)
at sun.tools.jar.Main.run(Main.java:132)
at sun.tools.jar.Main.main(Main.java:1022)

Please guide me.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Error in running Jar File.
 
Similar Threads
ejb-jar / utility jar dependencies
Updating a JAR manifest
Running java with a classpath on cygwin
Can I deliver a Java Application in One-JAR
Jar file help