• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java.lang.NoClassDefFoundError

 
Jason Bock
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 210
Ruby Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 210
Ruby Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20881
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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
 
Henry Wong
author
Marshal
Pie
Posts: 20881
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic