aspose file tools*
The moose likes Beginning Java and the fly likes java.lang.NoClassDefFoundError Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "java.lang.NoClassDefFoundError" Watch "java.lang.NoClassDefFoundError" New topic
Author

java.lang.NoClassDefFoundError

Jason Bock
Greenhorn

Joined: Dec 27, 2008
Posts: 19
I exported my app as a jar file and placed it on my linux home server.
The app uses two external jar files so I added them to the same directory.

When I tried to run it I got an error:
[root@localhost java]# java -cp . MyApp.jar
Exception in thread "main" java.lang.NoClassDefFoundError: MyApp/jar
Caused by: java.lang.ClassNotFoundException: MyApp.jar
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
Could not find the main class: MyApp.jar. Program will exit.


What am I doing wrong?
Wirianto Djunaidi
Ranch Hand

Joined: Mar 20, 2001
Posts: 210

You need to put both extarnal library in the classpath also. For example:

[root@localhost java]# java -cp .:extA.jar:extB.jar MyApp.jar
Jason Bock
Greenhorn

Joined: Dec 27, 2008
Posts: 19
I did exactly as you suggested but it's still throwing an error about not being able to find MyApp.jar

I tried to specify both a relative and an absolute path and neither one works.
Wirianto Djunaidi
Ranch Hand

Joined: Mar 20, 2001
Posts: 210

Sorry, wasn't paying attention. You need to use -jar option. And this will work only if you have declared your main class in the manifest by having Main-Class: classname in the manifest file in your MyApp.jar

[root@localhost java]# java -cp .:extA.jar:extB.jar -jar MyApp.jar


Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18845
    
  40

[root@localhost java]# java -cp . MyApp.jar
Exception in thread "main" java.lang.NoClassDefFoundError: MyApp/jar
Caused by: java.lang.ClassNotFoundException: MyApp.jar


This is not how you execute a class in a jar file...

If the jar file defines the main class in the manifest, then you can run it like so...

java -cp . -jar MyApp,jar

If the jar file doesn't define the main class, but you know what it is, then you can run it like so...

java -cp .;MyApp.jar mainclass


Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18845
    
  40


As for the external jars, it can be done via the classpath, or done via the Class-Path attribute in the manifest file.... but not both. I believe that the manifest takes precedence.

Henry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java.lang.NoClassDefFoundError