• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem with executable jar file

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Shree Java
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I have maintained the directory structure.


Not with the command line you posted above, you haven't.
 
Shree Java
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic