• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

java.lang.NoClassDefFoundError

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 218
VI Editor Ruby Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 218
VI Editor Ruby Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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



 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic