• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HowTo Execute a program in JAR

 
Graeme Byers
Ranch Hand
Posts: 127
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20393
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Graeme Byers
Ranch Hand
Posts: 127
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 127
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20393
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic