Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

Join 300,337 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,664 people online right now. Registration is fast and FREE... Join Now!




Exception in thread "main" - totally stuck!

 

Exception in thread "main" - totally stuck!, NoClassDefFoundError

3xtr4

12 May, 2009 - 12:27 PM
Post #1

New D.I.C Head
*

Joined: 12 May, 2009
Posts: 9

Hey,

So I'm learning Java. I've been using EasyEclipse and Netbeans - switched to Netbeans for the easy GUI creator. I did all the basic programs before making a very simple GUI one that I could actually use myself.

I made it in Netbeans, compiled it and tested it in Netbeans etc. So I went to my project folder and located "Main.class" - which I tried to run. It gave me this error:

CODE
C:\Java\Snooker\build\classes\snooker>java Main.class
Exception in thread "main" java.lang.NoClassDefFoundError: Main/class
Caused by: java.lang.ClassNotFoundException: Main.class
        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)
Could not find the main class: Main.class.  Program will exit.


I tried to execute the HelloWorld program with the same method and it didn't work either. I've done about 45mins of googling and I just can't find a definitive answer to this one. It compiles and runs perfectly in Netbeans. I;m new to Java so I'm right in saying that .class is Java's equivalent to .exe?

As you can see, I made a separate folder for my Java projects - C:\Java. I tried putting the Main.class file in the same directory that Java is installed in just to see - didn't work.

Any help would be awesome! Can't believe I'm getting stuck with something so minor!

This post has been edited by 3xtr4: 12 May, 2009 - 01:29 PM

User is offlineProfile CardPM
+Quote Post


skibo25

RE: Exception In Thread "main" - Totally Stuck!

12 May, 2009 - 02:08 PM
Post #2

New D.I.C Head
*

Joined: 12 May, 2009
Posts: 4



Thanked: 1 times
My Contributions
.exe and .class are not the same thing. .class refers to the actual code of your program and is thus only usable inside the compiler. .exe is an executable windows program. The java equivalent would be .jar.

Try reading this.

Also did you name your class main? Main generally refers to the "main" method inside a class.
User is offlineProfile CardPM
+Quote Post

3xtr4

RE: Exception In Thread "main" - Totally Stuck!

12 May, 2009 - 02:17 PM
Post #3

New D.I.C Head
*

Joined: 12 May, 2009
Posts: 9

Thanks for replying.

In the program there are two .java files - one Main.java and one Snooker.java. I didn't alter main, I just left it there in case it was necessary. I created a new JFrame form and named it Snooker, which became Snooker.java.

I'm just a bit lost. Basically I want to be able to use the application outside of Netbeans - I thought I'd do that by using the class file but I keep getting that error even with HelloWorld.

Sorry if I'm not being clear, if you need more info I'll send it your way
User is offlineProfile CardPM
+Quote Post

mobius4139

RE: Exception In Thread "main" - Totally Stuck!

12 May, 2009 - 02:20 PM
Post #4

D.I.C Head
**

Joined: 23 Mar, 2009
Posts: 117



Thanked: 2 times
My Contributions
Here are a few steps that helped me.

First, are you trying to run the program in the Command Prompt?

Next,you should verify that you have the Path and ClassPath set correctly.

Next, you need to find where the .jar file is located (as that is the executable file) With Netbeans it should be located somewhere like C: Program Files\netbeansprojects\ (file name)\dist\ (filename).jar

Let me know if you can find that .jar file

~Bruce

*edited file path data

This post has been edited by mobius4139: 12 May, 2009 - 02:25 PM
User is offlineProfile CardPM
+Quote Post

3xtr4

RE: Exception In Thread "main" - Totally Stuck!

12 May, 2009 - 02:25 PM
Post #5

New D.I.C Head
*

Joined: 12 May, 2009
Posts: 9

Aaaah I just built the project and read skibo25 suggested - got the JAR and I'm now sitting with my little app running behind this browser.

Thanks guys, much help!
User is offlineProfile CardPM
+Quote Post

mobius4139

RE: Exception In Thread "main" - Totally Stuck!

12 May, 2009 - 02:59 PM
Post #6

D.I.C Head
**

Joined: 23 Mar, 2009
Posts: 117



Thanked: 2 times
My Contributions
I am glad that everything worked out! smile.gif I fought with that through an entire semester before I was able to get it panned out

~Bruce
User is offlineProfile CardPM
+Quote Post

pbl

RE: Exception In Thread "main" - Totally Stuck!

12 May, 2009 - 06:32 PM
Post #7

Java Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 9,533



Thanked: 1124 times
Dream Kudos: 450
My Contributions
your Main class contains

CODE

class Main {


   public static void main(String[] args) {        // <-------- this method  eithis "main" having nothing to do with yous class Main
   }



When you do:

> java AbCdEf

java tries to excute public static void main() in AbCdEf
User is offlineProfile CardPM
+Quote Post

pillpusher

RE: Exception In Thread "main" - Totally Stuck!

17 May, 2009 - 09:24 PM
Post #8

New D.I.C Head
*

Joined: 13 May, 2009
Posts: 33



Thanked: 2 times
My Contributions
I'm getting a similar error:
CODE
chad-macbook-pro:dist Chad$ java -jar three40b.jar
Exception in thread "main" java.lang.UnsupportedClassVersionError: Bad version number in .class file
    at java.lang.ClassLoader.defineClass2(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:774)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:160)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:254)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:56)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:316)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:280)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:374)
chad-macbook-pro:dist Chad$

Can you give me more information on how to correctly set the classpath?
QUOTE(mobius4139 @ 12 May, 2009 - 04:20 PM) *
Next,you should verify that you have the Path and ClassPath set correctly.
I'm using netbeans on a mac, and I know how to right-click the library node and get to the properties where I can add a JAR/Folder. Once I'm there, do I add the "dist" folder or my project folder? Also, when I clean and build, I get a warning that I'm using or overriding a deprecated API, but I think I have my API set to JDK 1.6.
CODE
Note: /Users/Chad/NetBeansProjects/three40b/src/three40b/Three40BView.java uses or overrides a deprecated API.
Is there more than one place to set the API version?

Do I need to post my code? It's pretty lengthy, and I think my problem is a netbeans configuration problem. I can post it if I need to.

Thanks!

This post has been edited by pillpusher: 18 May, 2009 - 12:35 PM
User is offlineProfile CardPM
+Quote Post

pillpusher

RE: Exception In Thread "main" - Totally Stuck!

18 May, 2009 - 12:34 PM
Post #9

New D.I.C Head
*

Joined: 13 May, 2009
Posts: 33



Thanked: 2 times
My Contributions
I found the error that was giving me the "deprecated API" error.
CODE
Note: /Users/Chad/NetBeansProjects/three40b/src/three40b/Three40BView.java uses or overrides a deprecated API.
In case someone else comes across this problem, I'll explain how I fixed it.
In netbeans I right clicked on my project node and chose properties then clicked on compiling. In the window on the right make sure there's a check in "Report uses of deprecated APIs." Once I did this, the location of the errors related to the deprecated API showed in the output window. Ironically when I fixed that problem, I started getting the same "NoClassDefFoundError" as "3xtr4." I have tried fixing the classpath, but I must be doing something wrong because I keep getting the same error. Anyone have any other suggestions? I have Googled this topic, and there are lots of help pages out there. It seems like all those pages say pretty much the same thing. I still think I have a configuration problem in netbeans. (I'll keep looking . . .)

User is offlineProfile CardPM
+Quote Post

pillpusher

RE: Exception In Thread "main" - Totally Stuck!

22 May, 2009 - 06:10 PM
Post #10

New D.I.C Head
*

Joined: 13 May, 2009
Posts: 33



Thanked: 2 times
My Contributions
I still haven't fixed my own problem, but I found a link to what looks like a really helpful discussion of the dreaded "NoClassDefFoundError"

The link is below. Any help anyone could give would still be appreciated.

NoClassDefFoundError

User is offlineProfile CardPM
+Quote Post

pillpusher

RE: Exception In Thread "main" - Totally Stuck!

24 May, 2009 - 09:31 AM
Post #11

New D.I.C Head
*

Joined: 13 May, 2009
Posts: 33



Thanked: 2 times
My Contributions
I finally fixed my problem. If you read the article referenced in the link above you'll have noticed that the "NoClassDefFoundError" can be caused by MANY, MANY different problems. My problem was apparently some obscure configuration problem in netbeans. I still don't know what exactly caused my error, but I finally fixed it by starting over. My app is a simple database app that contains a search window, and a panel used for updating a quantity field. In creating my GUI and related files, I changed a setting or did something to cause the error. I finally renamed my project and started over. It took me less than an hour to recreate my whole project. I simply tested my .jar file after each step to try to catch the location of my error, but thankfully the error did not reoccur. I searched for hours trying to find the error, and it took less than an hour to just start over. I hope this helps someone else.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 05:10PM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month