• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception in thread "main" ERROR

 
Phil Teng
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Tim West
Ranch Hand
Posts: 539
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have a "public static void main" method in class SphereTest?
Check your class name first.
 
Greg T Robertson
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 539
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Phil Teng
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 7
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks Dirk!

    I most definitely appreciate it!!!

     
    Aaron Byrnes
    Greenhorn
    Posts: 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Pie
    Posts: 24212
    35
    Chrome Eclipse IDE Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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!
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic