[Logo]
Forums Register Login
Exception in thread "main"
I'm learning Java using the Head First Java book. The first code sample provided (just an example, not a tutorial, they say) isn't working.

The .java file compiles without error but I get an error when I execute the program. Here are the details...

Party.java:

import java.awt.*;
import java.awt.event.*;

class Party {
public void buildInvite() {
Frame f = new Frame();
Label l = new Label("Party at Time's");
Button b = new Button("You bet");
Button c = new Button("Shoot me");
Panel p = new Panel();
p.add(l);
} // More code here...
}

The commands I use to compile Party.java and execute Party.class are:

> javac Party.java
> java Party
(I've also tried the full file name, Party.class)

The following error is thrown when I execute "java Party":

Exception in thread "main" java.lang.NoClassDefFoundError: Party

Here are the Java-related environment variables:

CLASSPATH="C:\WINNT\system32\QTJava.zip"
QTJAVA="C:\WINNT\system32\QTJava.zip"
PATH=%PATH%;"C:\Program Files\Java\jdk1.5.0_04\bin"

What am I doing wrong?
 

Originally posted by Tom Smith:
I'm learning Java using the Head First Java book. The first code sample provided (just an example, not a tutorial, they say) isn't working.

The .java file compiles without error but I get an error when I execute the program. Here are the details...

Party.java:

import java.awt.*;
import java.awt.event.*;

class Party {
public void buildInvite() {
Frame f = new Frame();
Label l = new Label("Party at Time's");
Button b = new Button("You bet");
Button c = new Button("Shoot me");
Panel p = new Panel();
p.add(l);
} // More code here...
}

The commands I use to compile Party.java and execute Party.class are:

> javac Party.java
> java Party
(I've also tried the full file name, Party.class)

The following error is thrown when I execute "java Party":

Exception in thread "main" java.lang.NoClassDefFoundError: Party

Here are the Java-related environment variables:

CLASSPATH="C:\WINNT\system32\QTJava.zip"
QTJAVA="C:\WINNT\system32\QTJava.zip"
PATH=%PATH%;"C:\Program Files\Java\jdk1.5.0_04\bin"

What am I doing wrong?



You should get the following error:
Exception in thread "main" java.lang.NoSuchMethodError: main

bec you didnt added main method.

You will get NoClassDefFoundError when your class is not in classpath.
Thanks KJ!

I added the current directory, ".", to my CLASSPATH var and that resolved the NoClassDefFoundError. Afterwards, the error your predicted (NoSuchMethodError) occured.

I guess this particular code example wasn't meant to be used but rather was just illustrating... something.

Thanks again for your reply.

This thread has been viewed 875 times.

All times above are in ranch (not your local) time.
The current ranch time is
Dec 10, 2018 01:19:54.