Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

java.lang.NoClassDefFoundError - package issue

Status
Not open for further replies.

mydavor

Programmer
Mar 21, 2004
41
0
0
AU
I presume this problem has to do with the CLASSPATH, but I it is set (".;C:\jdk1.4;C:\jdk1.4\demo") in System as well as -classpath as below, still it works only when its package is explicitely called:

C:\jdk1.4\demo>java demo.SprinklerSystem
valve1 = null
C:\jdk1.4>java demo.SprinklerSystem
valve1 = null

Otherwise the exception occurs:
C:\jdk1.4>java demo
Exception in thread main
java.lang.NoClassDefFoundError: demo

C:\jdk1.4\demo>java SprinklerSystem
Exception in thread main
java.lang.NoClassDefFoundError: demo.SprinklerSystem
java.lang.Class java.lang.ClassLoader.defineClass0(java.lang.String, byte[], int, int, java.security.ProtectionDomain)..

C:\jdk1.4>java -classpath C:\jdk1.4\demo;. SprinklerSystem
Exception in thread main
java.lang.NoClassDefFoundError: demo.SprinklerSystem

Even this one failes:
C:\jdk1.4\demo>java -classpath C:\jdk1.4\demo;. demo.SprinklerSystem
Exception in thread main
java.lang.NoClassDefFoundError: demo/SprinklerSystem


 
Can you provide a bit more information?

What is the full path of the SprinklerSystem.class file? In other words, where is it located?

Can you tell us what the package statement says at the top of your source code?

And what directory are you in when you try to run it?
 
When you use a package declaration in your code
for example ----- package demo;

The classpath you need to give java is the directory structure in which the "demo" directory exists.

Then when you execute java, you still need to explicitly describe the package path in which it will find your class.

the class path specified can be relative to the directory you are currently in
ie. if I'm in c:\jdk1.4\demo I would use the following c:\jdk1.4\demo>java -classpath .. demo.SprinklerSystem

if I'm in c: I would use the following
c:>java -classpath .\jdk1.4 demo.SprinklerSystem

You can always use absolute paths also. Then it wouldn't matter what directory you're currently in.

Bottom line is that you must have the root directory (the place where your package root sits) in the classpath. And you must specify the fully qualified classname (which includes the entire package path) in order for java to drill down and find the specific class you want to execute.

It might help to think of it this way:

- Your class is not called SprinklerSystem. It is called demo.SprinklerSystem
 
Thanks for the replies. Note that I had put "." in the classpath but it did not make any difference, it still needs "demo.". While this involves only one program, it gets tricky with a whole package that we were given to compile. JBuilder complains in every program not recognising package names.
Must files for a single package reside in a directory named after this package ? What if they do not ? How to refer to classes in a package, with "package." prefix ? How can I change -classpath option for JBuilder ?
Thank you in advance
 
>>Must files for a single package reside in a directory named after this package ?

See :
In JBuilder, you can set the "output path" and the "required libraries" via menu "Project" --> "Project Properties..." -->
Tab "Paths" ... --> "output path"
--> Tab "required libraries"
 
classes in a single package must reside in a directory structure that mirrors the package name.

In other words:
if your package is: com.system.app.options then the class files themselves must reside in c:\somepath\com\system\app\options

and c:\somepath must be in your classpath
 
I build a package with
jar cvf com.symantec.itools.javax.icons.jar *.class *.gif
in a C:\something\com\symantec\itools\javax\icons
directory where I downloaded beforehand all the class and gif files available on the the above package.

I am still getting following error in JBuilder:

"DPSysNavClass.java": cannot access ImageIcon,bad class file: C:\Documents and Settings\shome\jbproject\com\symantec\itools\javax\swing\icons\com.symantec.itools.javax.swing.icons.jar\ImageIcon.class,class file contains wrong class: com.symantec.itools.javax.swing.icons.ImageIcon,Please remove or make sure it appears in the correct subdirectory of the classpath. at line 341, column 9

I have also put .jar in the default library and classes directories, no change. classpath in JBuilder is also pointing to all these directories.
 
You know you may need permission from symantec, to redistribute their icons?

Note, that the name of your jar-file is not interesting.
You have to be in 'something' and jar the whole f...in directory-structure.
Better you move a step up, and keep the work to jar:
Code:
cd .../something
cd ..
jar -cvf pirate.jar -C something .
-C: go to that directory, and include everything recursivly
therefore it is handy, if you have a directory, which includes everything you need, and nothing else (different projects).

java will look in your pirate.jar for the 'com/....../' and not for a jar, named 'com........'.
 
Thanks a lot to sedj, and especially to stefanwagner for a good explanation.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top