wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Exception in thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Exception in thread "main" ERROR" Watch "Exception in thread "main" ERROR" New topic
Author

Exception in thread "main" ERROR

Phil Teng
Greenhorn

Joined: Jan 06, 2004
Posts: 7
Hi all,

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.

Thanks a lot in advance!!!

Phil


5th Dan wannabe
Tim West
Ranch Hand

Joined: Mar 15, 2004
Posts: 539
Are you really using "java blahblah.class"? Try "java blahblah" instead.

If that doesn't help, post the whole error message and we'll have another look.


--Tim
Phil Teng
Greenhorn

Joined: Jan 06, 2004
Posts: 7
Hi Tim,

The following is the error message

*****************************************

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)
**********************************************

Thanks

Phil
Zhengwu Lee
Greenhorn

Joined: Oct 05, 2002
Posts: 3
Do you have a "public static void main" method in class SphereTest?
Check your class name first.
Greg T Robertson
Ranch Hand

Joined: Nov 18, 2003
Posts: 91
Originally posted by Phil:

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
Tim West
Ranch Hand

Joined: Mar 15, 2004
Posts: 539
Yeah I'd say Greg's on the right track. If you can't get it to work, post the 'package' statement from the top of the class.


--Tim
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Phil,

Welcome to JavaRanch!

We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.

Thanks Pardner! Hope to see you 'round the Ranch!


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
There are a number of forces at work here.

First and foremost using an IDE (such as NetBeans) to learn Java is A Bad Idea(TM). http://www.xdweb.net/~dibblego/java/faq/answers.html#q34

Second, using packages without at least a very basic understanding (even more basic than that required for packages) is yet another Bad Idea.
http://java.sun.com/docs/books/tutorial/java/interpack/packages.html

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

Good luck


Tony Morris
Java Q&A (FAQ, Trivia)
Phil Teng
Greenhorn

Joined: Jan 06, 2004
Posts: 7
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?

Thanks a lot for yall's help!!!

Later!
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
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:
  • Sun's Java Tutorial
  • Introduction to Computer Science using Java by Bradley Kjell
  • Introduction to Programming Using Java by David J. Eck
  • Dick Baldwin's Java Programming Tutorials
  • Interactive Programming In Java by Lynn Andrea Stein
  • Bruce Eckel's Thinking In Java
  • JavaRanch's own Campfire Stories
  • Allen B. Downey's How To Think Like A Computer Scientist

  • 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 ]
    Phil Teng
    Greenhorn

    Joined: Jan 06, 2004
    Posts: 7
    Thanks Dirk!

    I most definitely appreciate it!!!

    Aaron Byrnes
    Greenhorn

    Joined: Jun 04, 2004
    Posts: 1
    You can't assume you ever know what the current directory is.

    The name of the program is test. The file name is test.class

    The location of the program is F:\projects\advice

    The command to get the program to run is:

    java -classpath f:\projects\advice test

    The following will not work even if you cd to the f:\projects\advice directory and run it from there:

    java test
    Ernest Friedman-Hill
    author and iconoclast
    Marshal

    Joined: Jul 08, 2003
    Posts: 24183
        
      34

    Originally posted by Aaron Byrnes:

    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!


    [Jess in Action][AskingGoodQuestions]
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Exception in thread "main" ERROR
     
    Similar Threads
    Very Basic doubt about CLASSPATH
    Java Virtual Machine troubleshooting
    Problem with iText
    Setting up JAVA
    problem with webservice dynamic proxyclient