runtime Java Error (just starting out

Status
Not open for further replies.
Trying to write my first java program.

public class first
{
public static void main(String[] args)
{
System.out.println("hi");

}
}

I'm using Sun's Java2 SDK v1.4 to try to compile the program.

it compiles ok using the line: javac e:\java\first.java

but when I try to run it with: java e:\java\first

i get the following error:
exception in thread main java.lang.noclassdeffounderror e:\java\first

searched on google to no avail... can someone point me in the right direction?
 

wb

Ars Legatus Legionis
25,208
Subscriptor
Simple. Your main() method should not have been declared void. That's BAD.

The main method should indeed have a return type. In fact, the compiler will return an error unless you specify the return type. Perhaps you're thinking of the constructor?

A constructor is not neccessary in this case, and an empty default constructor will be automatically provided by the compiler.
 

newton_meter

Ars Scholae Palatinae
1,424
Originally posted by Level_7:
Simple. Your main() method should not have been declared void. That's BAD.

Ha ha ha.

Seriously, the error message actually tells you (sort of) what's wrong.

"exception in thread main java.lang.noclassdeffounderror e:\java\first"

The Java Virtual Machine (the "java" executable) cannot find a class named "e:\java\first". That's because the class is named simply "first". The path that you would use to find the class file is not part of the class name (and specifying that path is not part of the Java language itself, but the tools that manipulate Java programs--compilers, virtual machines, debuggers).

You could run java in the directory e:\java, with the command line:

java first

or, give the JVM a path to find the class:

java -classpath e:\java first

You can abbreviate -classpath as -cp with the Sun JDK JVM. You can also set a CLASSPATH environment variable, but how to do that depends on your OS.

(kg*m*m)/(s*s)
 

Pika

Seniorius Lurkius
21
Originally posted by wb:
Quote: "Simple. Your main() method should not have been declared void. That's BAD."

The main method should indeed have a return type. In fact, the compiler will return an error unless you specify the return type. Perhaps you're thinking of the constructor?

A constructor is not neccessary in this case, and an empty default constructor will be automatically provided by the compiler.

Indeed the main() method does require a return type and that should be void. In fact the method signature has to be

public static void main(String [] args)

Or else it won't work. Simple as that. Everything else is fine, just follow newton_meter info on how to get it to work.
 
Status
Not open for further replies.