• 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

NoClassDefFoundError

 
Ranch Hand
Posts: 42
Firefox Browser Tomcat Server Windows XP
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

In Eclipse
I created a simple Employee class.
It consists of String name; and getters and setters
Simply converted it to jar.

Created another project that references the Employee jar.
It consists of main where I am simply setting a name and displaying it.

I converted this project to an executable jar.

Now I took both the jars
Employee.jar
PrintEmployee.jar (executable)

put in a simple directory EMP
and when i used the command

java -classpath employee.jar PrintEmployee.jar
from the EMP directory

It threw the java.lang.NoClassDefFoundError


Please help.

Where am I wrong?
 
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Show me the full trace. What does the line after java.lang.NoClassDefFoundError say? Which class is it looking for?
 
Anupam Dee
Ranch Hand
Posts: 42
Firefox Browser Tomcat Server Windows XP
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I solved the problem by adding the classpath in the MANIFEST itself because the classpath is invalid when we run the jar file from command line.

But the real problem is:
If there are 25 jars in my project that i need to load, How I am going to load them all?
I tried using

Class-Path: jars/* it does not work

But when i use
Class-Path: jars/employee.jar
it works fine.
 
Anupam Dee
Ranch Hand
Posts: 42
Firefox Browser Tomcat Server Windows XP
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This is the Stacktrace

Exception in thread "main" java.lang.NoClassDefFoundError: abc/employee/Employee
at abc.printemployee.PrintEmployee.main(PrintEmployee.java:16)
Caused by: java.lang.ClassNotFoundException: abc.employee.Employee
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
... 1 more
 
Mansukhdeep Thind
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anupam Dee wrote:I solved the problem by adding the classpath in the MANIFEST itself because the classpath is invalid when we run the jar file from command line.

But the real problem is:
If there are 25 jars in my project that i need to load, How I am going to load them all?
I tried using

Class-Path: jars/* it does not work

But when i use
Class-Path: jars/employee.jar
it works fine.



Why do you think jars/* should work? Or let me ask you this. If I ask you to search a folder in your PC for all the java files, how do you write that search pattern? Do you write * or do you write *.java in the search box?
 
Anupam Dee
Ranch Hand
Posts: 42
Firefox Browser Tomcat Server Windows XP
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Mansukh

See this
http://stackoverflow.com/questions/219585/setting-multiple-jars-in-java-classpath
 
Mansukhdeep Thind
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the command that you are giving?
 
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That page states specifically "However, class path wildcards are not honored in the Class-Path jar-manifest header."
 
Anupam Dee
Ranch Hand
Posts: 42
Firefox Browser Tomcat Server Windows XP
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes I know.

But what is the solution then?
Actually I tried both. with MANIFEST and without MANIFEST.

Is there no way for adding multiple jars from a different directory in classpath and running the jar file?
 
Ulf Dittmer
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes there is - you need to list them individually in the manifest's Class-Path entry. That page has an example of listing two libraries - granted, they're in the same directory, but it is easy to see how you would change that.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But why this peculiar behavior for a class path Ulf? Why does the JVM not honor the wildcards in the manifest header?
 
Anupam Dee
Ranch Hand
Posts: 42
Firefox Browser Tomcat Server Windows XP
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes of course.

This is creating a big problem for me. I ve to use a lot of jars. I can't write them individually.
 
Ulf Dittmer
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can't say I find it peculiar. Up until Java 6, it didn't work for the -cp switch either. And manifests are used only for desktop apps - something that hasn't exactly taken the world by storm, so I can understand why it wouldn't be a priority to implement.
 
Ulf Dittmer
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anupam Dee wrote:I can't write them individually.


Why not? I don't think there's a length restriction on manifest entries.

But if this is a complex project, then you should be using a build tool like Ant anyway - which can generate the manifest and its entries for you. Problem solved.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic