This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

jar creation with subdirectories

 
K. Tsang
Bartender
Posts: 3348
12
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all, not sure if the captioned topic is considered as intermediate or not but here I go. I have set up my file structure on Windows as something like *.java goes inside /src dir; *.class goes inside /class dir; and the jar file goes inside the /dist dir.

I did the following to put the files into proper location like this:
javac -d class src/MyClass.java
jar -cvfe dist/MyClass.jar MyClass class/MyClass.class

Then when I try running using java -jar dist/MyClass.jar it can't find the main class??

Exception in thread "main" java.lang.NoClassDefFoundError: MyClass
Caused by: java.lang.ClassNotFoundException: MyClass
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)

However, if I don't use subdirectories then everything works fine.
javac -d . src/MyClass.java
jar -cvfe MyClass.jar MyClass MyClass.class
java -jar MyClass.jar

Can anyone please help to clarify what I'm doing wrong or the correct to create jar files with class files inside some directory. Thanks.
 
Jorge Pinho
Greenhorn
Posts: 25
MySQL Database PHP Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi K.

java -jar dist/MyClass.jar also needs a classpath...

try...
java -cp dist -jar MyClass.jar

Hope it works, or its something else, lets see...
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jorge Pinho:
Hi K.

java -jar dist/MyClass.jar also needs a classpath...

try...
java -cp dist -jar MyClass.jar

Hope it works, or its something else, lets see...


No. If you use the -jar option then the classpath and the main class have to be specified in the jar's manifest file.

java command line options
-jar
Execute a program encapsulated in a JAR file. The first argument is the name of a JAR file instead of a startup class name. In order for this option to work, the manifest of the JAR file must contain a line of the form Main-Class: classname. Here, classname identifies the class having the public static void main(String[] args) method that serves as your application's starting point. See the Jar tool reference page and the Jar trail of the Java Tutorial for information about working with Jar files and Jar-file manifests.
When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored.
 
K. Tsang
Bartender
Posts: 3348
12
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guess what using java -cp wouldn't work. Adding the class-path thing in manifest wouldn't make a difference. It's something to with the -C option.

Suppose I'm at D:\projects\java\test directory and want to run HelloWorld (yes the most simplest of all programs lol).

File structure is like:
test
|_src
|_cls
|_dist

first compile
D:\projects\java\test>javac -d cls src/*.java

then create jar
D:\projects\java\test>jar cvfe dist/HelloWorld.jar HelloWorld -C cls .
added manifest
adding: HelloWorld.class(in = 425) (out= 286)(deflated 32%)

then run jar
D:\projects\java\test>java -jar dist/HelloWorld.jar
Hello World

for myself
[ March 04, 2008: Message edited by: K. Tsang ]
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic