File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Problem with executable jar file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Problem with executable jar file" Watch "Problem with executable jar file" New topic
Author

Problem with executable jar file

Shree Java
Greenhorn

Joined: Jun 28, 2006
Posts: 3
I have a class called JarTest which is in the package Test:

package Test;

import java.io.*;

public class JarTest {
public final static void main( String[] args )
{
System.out.println( "Hello Readers" );
}
}

I have compiled and tested it . It is working fine. Then I created an exceutable jar file using the command:

jar -cmf mainClass.txt sample.jar *.class

The contents of mainClass.txt is Without the double quotes mentioned below)

"
Main-Class: Test.JarTest

"

It gives me the following error when I running the program from excutable jar file:

java -jar sample.jar

Exception in thread "main" java.lang.NoClassDefFoundError: Test/JarTest

The same stuff worked when I did not specify the package "Test" for class "JarTest".

Please help.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39547
    
  27
Welcome to JavaRanch.

In the jar file the package hierarchy needs to be preserved. The class file must be inside of a directory called "Test". Something like:

mkdir Test
mv Test.class Test
jar -cmf mainClass.txt sample.jar Test

should do the trick. You should always keep classes in directory hierarchies that correspond to their package.


Ping & DNS - updated with new look and Ping home screen widget
Shree Java
Greenhorn

Joined: Jun 28, 2006
Posts: 3
Hi,

Thanks for the reply.

I have maintained the directory structure.
And when unjared, it works fine. But I am facing problem when trying to execute from the jar file.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39547
    
  27
I have maintained the directory structure.

Not with the command line you posted above, you haven't.
Shree Java
Greenhorn

Joined: Jun 28, 2006
Posts: 3
I checked the sample.jar (opened in winzip)and it has Test.class under the directory "Test".

I have executed the command:
jar -cmf mainClass.txt sample.jar *.class
from the directory Test.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with executable jar file
 
Similar Threads
can't make java to work on jar
problem with running jar file
Invoke a Class from a Particular JAR file
strings
Sample code for loading a class file from a jar file, dynamically.