10 Replies - 2803 Views - Last Post: 24 May 2009 - 10:31 AM Rate Topic: -----

#1 3xtr4  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 12-May 09

Exception in thread "main" - totally stuck!

Post icon  Posted 12 May 2009 - 01:27 PM

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:

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 - 02:29 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Exception in thread "main" - totally stuck!

#2 skibo25  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 35
  • Joined: 12-May 09

Re: Exception in thread "main" - totally stuck!

Posted 12 May 2009 - 03:08 PM

.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.
Was This Post Helpful? 1
  • +
  • -

#3 3xtr4  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 12-May 09

Re: Exception in thread "main" - totally stuck!

Posted 12 May 2009 - 03:17 PM

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
Was This Post Helpful? 0
  • +
  • -

#4 mobius4139  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 118
  • Joined: 24-March 09

Re: Exception in thread "main" - totally stuck!

Posted 12 May 2009 - 03:20 PM

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 - 03:25 PM

Was This Post Helpful? 1
  • +
  • -

#5 3xtr4  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 12-May 09

Re: Exception in thread "main" - totally stuck!

Posted 12 May 2009 - 03:25 PM

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!
Was This Post Helpful? 0
  • +
  • -

#6 mobius4139  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 118
  • Joined: 24-March 09

Re: Exception in thread "main" - totally stuck!

Posted 12 May 2009 - 03:59 PM

I am glad that everything worked out! :) I fought with that through an entire semester before I was able to get it panned out

~Bruce
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,858
  • Joined: 06-March 08

Re: Exception in thread "main" - totally stuck!

Posted 12 May 2009 - 07:32 PM

your Main class contains

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
Was This Post Helpful? 0
  • +
  • -

#8 pillpusher  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 48
  • Joined: 13-May 09

Re: Exception in thread "main" - totally stuck!

Posted 17 May 2009 - 10:24 PM

I'm getting a similar error:
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?

View Postmobius4139, on 12 May, 2009 - 04:20 PM, said:

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.
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 - 01:35 PM

Was This Post Helpful? 0
  • +
  • -

#9 pillpusher  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 48
  • Joined: 13-May 09

Re: Exception in thread "main" - totally stuck!

Posted 18 May 2009 - 01:34 PM

I found the error that was giving me the "deprecated API" error.
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 . . .)
Was This Post Helpful? 0
  • +
  • -

#10 pillpusher  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 48
  • Joined: 13-May 09

Re: Exception in thread "main" - totally stuck!

Posted 22 May 2009 - 07:10 PM

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
Was This Post Helpful? 0
  • +
  • -

#11 pillpusher  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 48
  • Joined: 13-May 09

Re: Exception in thread "main" - totally stuck!

Posted 24 May 2009 - 10:31 AM

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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1