java.lang.NoClassDefFoundError: LearnJava/VolcanoRobot/VolcanoRobot
(wrong name: VolcanoRobot)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:537)
at
java.security.SecureClassLoader.defineClass(SecureClassLoader.java:123)
at
java.net.URLClassLoader.defineClass(URLClassLoader.java:251)
at java.net.URLClassLoader.access$100(URLClassLoader.java:55)
at java.net.URLClassLoader$1.run(URLClassLoader.java:194)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:187)
at java.lang.ClassLoader.loadClass(ClassLoader.java:289)
at
sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:274)
at java.lang.ClassLoader.loadClass(ClassLoader.java:235)
at
java.lang.ClassLoader.loadClassInternal(ClassLoader.java:302)
Exception in thread "main"
If I put the statement
package LearnJava.VolcanoRobot;
at the top of the file, all is well with NetBeans. Unfortunately then
the SDK java command gives lots of errors, like this
Exception in thread "main" java.lang.NoClassDefFoundError:
VolcanoRobot (wrong name: LearnJava/VolcanoRobot/VolcanoRobot
)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
I'm fairly usre I'm doing something really simple wrong (bad
configuration of some sort), but could use a clue.
Thanks
Realize that Netbeans adds mounted directories to the classpath. If
something isn't in a mounted directory, it is not visible to Java. That goes
for subdirectories of the mounted directories. They are considered packages
and must be imported.
I'm not sure I understand what you've said. I've got a trivial
program like this:
public class RichardFirstPackage {
/** Creates a new instance of RichardFirstPackage */
public RichardFirstPackage() {
}
public static void main(String[] args) {
String user = System.getProperty("user.name");
System.out.println("xxx " + user);
}
}
When I compile with Javac (Classpath = .;c:\j2sdk1.4.2_03\lib and
Javac et al in the Path system variable) all is well and I can execute
with Java RichardFirstPackage.
If I now use NetBeans to compile and run this program I get the errors
previously indicated. Adding
package LearnJava.RichardFirstPackage;
before the class definition fixes all and I can run in Netbeans.
Unfortunately when I now try to compile in a command window with Javac
I get the second set of errors.
Is there something I need to set either in Netbeans or in XP to
arrange things such that I can interchangeable use both tools?
Thanks.
Ryan,
As far as I know I've no directories mounted in NetBeans.
RichardFirstPackage.class is in c:\Documents and Settings\rbell\My
Documents\LearnJava\RIchardFirstPackage
At command line, I'm running Javac and Java in that same directory.
It's not so much the problem that has me concerned as the
inconsistency between NetBeans and command line. I've got to believe
I'm doing something really foolish.
Ryan,
Thanks for your patience. I'm still not entirely clear. When I
examine NetBeans "filesystems" tab I find a bunch of things listed
including
cd to C:\Documents and Settings\rbell\My Documents
Under which I can see all the files and directories in this directory.
I now have the Java code looking like this:
package LearnJava.RichardFirstPackage;
public class RichardFirstPackage {
/** Creates a new instance of RichardFirstPackage */
public RichardFirstPackage() {
}
public static void main(String[] args) {
String user = System.getProperty("user.name");
System.out.println("xxx " + user);
}
}
In NetBeans it executes as expected.
When I open a command window and
cd to C:\Documents and Settings\rbell\My
Documents\LearnJava\RichardFirstPackage>
then execute the command
java LearnJava.RichardFirstPackage.RichardFirstPackage
I get the error message
Exception in thread "main" java.lang.NoClassDefFoundError:
LearnJava/RichardFirstPackage/RichardFirstPackage
So that's a different set of messages than earlier but still NetBeans
and Java are behaving differently.
BTW, I've got classpath set to .;the directory the java sdk is
installed in. Do I have an issue with the classpath in NetBeans (I
think I read somewhere that NetBeans ignores classpath)?
Thanks for your patience and help.
Ryan,
I experimented a bit more and deleted a bunch of mounted file systems
then mounted the directory containing RichardFirstPackage.java.
Commented out the Package statement in the source. Lo and behold both
NetBeans and Java behave the same. Clearly, I'm new to this all, but
it appears that NetBeans was acting on one of the mounted file systems
before it got to the one referencing RichardFirstPackage.
A bit of searching in the help file reveals that NetBeans relies on
the mounted filesystems not the environmental variable classpath.
That explains the differences in behavior. Thanks for your patience
and help.
There may be just a bit more to it than that. As it happened, at one
point, I'm not sure when, but it was after I wrote the test program, I
had mounted RichardFirstPackage in NetBeans. That mount was way down
the list. NetBeans says it adds all the mounts to its Classpath.
Somewhere in the many mounts there was something (I never checked what
it was) that allowed NetBeans to work but only with the package
statement included. It seems that it is an order thing of some sort.
When I cleaned out all the mounts and started again as you suggested
all was well. Just a dumb new user error.
Thanks again for all your help.