I'm having a very annoying problem. Everytime when I try to run an application from DOS prompt using java blahblah.class, it gives me this error, "Exception in thread "main"".
I can compile the source code using javac or NetBean just fine. And the application runs in NetBean.
I already set the path to "C:\C:\j2sdk1.4.2_04\bin" (Without quotes in environment settings).
Anyway, spent almost an afternoon trying figure it out but it just got very frustrating.
C:\Java\Examplez\examples\ch06\exercises\ex6_6>Java SphereTest Exception in thread "main" java.lang.NoClassDefFoundError: SphereTest (wrong name: Examplez/examples/ch06/exercises/ex6_6/SphereTest) at java.lang.ClassLoader.defineClass0(Native Method) at java.lang.ClassLoader.defineClass(ClassLoader.java:537) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:12 3) 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) **********************************************
C:\Java\Examplez\examples\ch06\exercises\ex6_6>Java SphereTest Exception in thread "main" java.lang.NoClassDefFoundError: SphereTest (wrong name: Examplez/examples/ch06/exercises/ex6_6/SphereTest) at java.lang.ClassLoader.defineClass0(Native Method) at java.lang.ClassLoader.defineClass(ClassLoader.java:537) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:12 3) 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) **********************************************
Looking at the directory you are in and the error you are getting i suspect you should be in the C:\Java\Examplez\ directory and you should type java examples.ch06.exercises.ex6_6.SphereTest
You should look in the java file for SphereTest and see if it has a package statement in it. If it does you should run it as whatever the package name is "." classname
Third, your problem has nothing to do with whether or not your class contains a main method. Your problem has everything to do with the class loader not being able to load the class it says it cannot load (SphereTest). This is possibly due to a number of reasons, and the suggested reason relating to the use of packages is also my number 1 suspicion. There are other reasons, such as setting a system-wide CLASSPATH environment variable (seldom a good idea, especially when you are learning Java) incorrectly. http://www.xdweb.net/~dibblego/java/faq/answers.html#q5
So, the following actions are required to fix your problem: - STOP using NetBeans (I see this mistake far too often) - STOP using packages (would you give a loaded gun to someone who doesn't know how to shoot?) - Make sure you have no system CLASSPATH set (this is generally bad, even at the best of times). - Start learning Java, The Right Way http://java.sun.com/docs/books/tutorial/getStarted/cupojava/index.html
Well, apparently I started off with all the bad ideas, isn't that something special, and now I know all the bad ideas to start learning Java with! LOL D
Tony, I had read a little in Sun's tutorial and it felt real good, even better than book I have, a Deitel's How to program in Java. But I thought since I bought this book, and what the heck. I'm not really good at reading a few books at the same time, so would you recommend learning only from Sun's tutorial?
While you might feel distracted trying to follow more than one book at a time, as I learn things, I like to sometimes follow a single resource from beginning to end, while using multiple other references to look up alternative explanations of certain topics.
In that thought, here is a list of free on-line Java tutorials and books that I have found useful:
More than one of them are well-written and I'd recommend following from beginning to end. To get a better sense of which might suit your learning needs best, read a few pages from each of them, then decide which you like best and go through the whole thing.
I would not recommend Sun's tutorial as the primary path to learning. In my view, it's not designed to be an introductory computer programming text - it's designed to be an introduction to Java. As such, I don't believe a new programmer would get the whole picture from just Sun's tutorial as a reference.
The best thing I ever did to learn programming better, was to hang out at JavaRanch, asking and answering questions - especially the figuring out answers part. [ May 31, 2004: Message edited by: Dirk Schreckmann ]
The following will not work even if you cd to the f:\projects\advice directory and run it from there:
java test
It will work just fine, actually, unless you've got the CLASSPATH environment variable set and it doesn't include "." (the current directory.) Although you're right to say that code in a running application can't assume what the current directory is, a human typing at a command prompt can be reasonably confident about it!
Post by:autobot
You totally ruined the moon. You're gonna hafta pay for that you know. This tiny ad agrees:
a bit of art, as a gift, the permaculture playing cards