wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes HowTo Execute a program in JAR Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "HowTo Execute a program in JAR" Watch "HowTo Execute a program in JAR" New topic
Author

HowTo Execute a program in JAR

Graeme Byers
Ranch Hand

Joined: Apr 16, 2004
Posts: 127
I know I should stick at this problem, but it's something a professional java programmer will know off the top of his head.

Message calls Hello sayGreeting().
cd to the directory containing EnglishGreetings.jar (also the root of the SourceLib and ClassLib)

>java -jar EnglishGreetings.jar // raises NoClassDefFoundError

I created an uncompressed JAR (option 0, that's zero , just in case) using
>jar cvmf0 GBManifest.txt EnglishGreetings.jar ReadMe.txt ClassLib SourceLib English
GBManifest.txt contains Main-Class: Message
Do I need to set a java -classpath or a manifest Class-Path: and what should I set them to ?



Here is EnglishGreetings.jar



Here is the manifest :
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

Inside the JAR file the folder structure should follow the package structure. Since both Message does not have a package statement, its class file should be located in the root of the JAR file, not in a subfolder. Also, Hello.class should be in absolute folder /com/hotmail/graemebyers, without the English/ClassLib.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Graeme Byers
Ranch Hand

Joined: Apr 16, 2004
Posts: 127
Rob,
Here is the directory structure and I think it is reasonable :

C:\GBJAVA\Greetings\ReadMe.txt
C:\GBJAVA\Greetings\SourceLib\Message.java
C:\GBJAVA\greetings\ClassLib\Message.class C:\GBJAVA\Greetings\English\SourceLib\Hello.java C:\GBJAVA\Greetings\English\ClassLib\com\hotmail\graemebyers\Hello.class

By 'root' I assume you mean that ReadMe.txt is in the root directory.
Well, it's in the root because C:\GBJAVA\Greetings was my current directory when I ran JAR create.
If to put Message.class in the root I would run a create for ReadMe then >cd ClassLib then run an update (yes, that can be done) to add Message.class at the root level.
JAR takes the current directory and automatically generates a jar file with subordinate directories in front of the archived file (I tested it).

The only way out of this is for the 'greetings' application there should be one C:\GBJAVA\Greetings\ClassLib and Hello put in a package named
com.hotmail.graemebyers.English (not just com.hotmail.graemebyers) and (say)GutenTag put in com.hotmail.graemebyers.German.
Is this the way to go ? I will test it tomorrow.

Where did you learn about root - do all unpackaged classes and packes need to be in the root ?

Thank you.
Graeme Byers
Ranch Hand

Joined: Apr 16, 2004
Posts: 127
Thankyou,
The following worked :

META-INF/
META-INF/MANIFEST.MF
C:/GBJAVA/greetings/ReadMe.txt
com/
com/hotmail/
com/hotmail/graemebyers/
com/hotmail/graemebyers/english/
com/hotmail/graemebyers/english/Hello.class
Message.class
C:/GBJAVA/greetings/SourceLib/Message.java
C:/GBJAVA/greetings/English/SourceLib/Hello.java
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

Originally posted by Graeme Byers:
The only way out of this is for the 'greetings' application there should be one C:\GBJAVA\Greetings\ClassLib and Hello put in a package named
com.hotmail.graemebyers.English (not just com.hotmail.graemebyers) and (say)GutenTag put in com.hotmail.graemebyers.German.
Is this the way to go ? I will test it tomorrow.

If you need a separation between English and German then different packages look like a good idea (especially if you will have many files per language). That way you get a clear separation between them.

Where did you learn about root - do all unpackaged classes and packes need to be in the root ?

I don't know what you mean with packes, but indeed - all classes without a package need to go into the root of the JAR file. All root packages (like com) need to be folders that are put directly in the root.

That's just how JAR files work. You can consider them as folders that are added to the CLASSPATH - the same structure applies.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HowTo Execute a program in JAR
 
Similar Threads
Exception in thread "main"
Setting an Entry Point with JAR Tool
Invoking method in packaged class
Creating an executable JAR
packaging up Swing application into Jar file for distribution