views:

235

answers:

4

I know there are many posts concerning NoClassDefFoundError, they all seem to be talking about jar files. While I'm comfortable with java in eclipse, I'm pretty lost as to why the simplest thing I can come up with is not functioning, unless they broke something on the university side of this.

public class hello {
    public static void main (String args[]) {
            System.out.println ("Hello World!");
    }
}

This is the entire hello.java program which throws this:

Exception in thread "main" java.lang.NoClassDefFoundError: hello/java
Caused by: java.lang.ClassNotFoundException: hello.java
    at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
Could not find the main class: hello.java.  Program will exit.

$CLASSPATH=./:/usr/java/latest/lib:/home/41/myusername/bin

java -verbose hello.class

gives:

[Opened /usr/java/jdk1.6.0_10/jre/lib/rt.jar]
[Loaded java.lang.Object from /usr/java/jdk1.6.0_10/jre/lib/rt.jar]
[Loaded java.io.Serializable from /usr/java/jdk1.6.0_10/jre/lib/rt.jar]
[Loaded java.lang.Comparable from /usr/java/jdk1.6.0_10/jre/lib/rt.jar]
[Loaded java.lang.CharSequence from /usr/java/jdk1.6.0_10/jre/lib/rt.jar]
[Loaded java.lang.String from /usr/java/jdk1.6.0_10/jre/lib/rt.jar]
[Loaded java.lang.reflect.GenericDeclaration from /usr/java/jdk1.6.0_10/jre/lib/rt.jar]
[Loaded java.lang.reflect.Type from /usr/java/jdk1.6.0_10/jre/lib/rt.jar]
[Loaded java.lang.reflect.AnnotatedElement from /usr/java/jdk1.6.0_10/jre/lib/rt.jar]
[Loaded java.lang.Class from /usr/java/jdk1.6.0_10/jre/lib/rt.jar]
...
[Loaded sun.misc.AtomicLong from /usr/java/jdk1.6.0_10/jre/lib/rt.jar]
...

with the exception again.

java -version

gives:

java version "1.6.0_10-rc2"
Java(TM) SE Runtime Environment (build 1.6.0_10-rc2-b32)
Java HotSpot(TM) Server VM (build 11.0-b15, mixed mode)
+1  A: 
java -verbose hello
Rorick
+8  A: 

First, compile your .java file using the javac hello.java to produce a hello.class file. Then, execute the class using the command java hello - you don't include the .class extension when using the java command.

Also, I would like to point out that it is convention that class names begin with a capital letter - hello should be Hello.

Thomas Owens
yeah I'm used to the capital letters for class in C#, and I tried renaming it to a capital letter in case that was the problem.
Maslow
java hello doesn't work, until it's compiled apparently I missed the compile step and didn't know the difference between .java and .class
Maslow
+2  A: 
java -verbose hello.class

... means "hey, Java, run the main() method in the class 'hello.class'.

Java can't find a class named "hello.class". Your class is called "hello".

java -verbose hello

Since '.' is in your classpath, Java will find the 'hello' class in './hello.class'.

Extra tip: it's conventional in Java to start classes with a capital letter.

public class Hello {

This helps to distinguish between class references and variable references in the rest of your code.

Dessert dessert= new Dessert("tiramisu")
slim
Is Desert a subclass of Dessert? ;-)
spilth
Ooops. How clumsy. Fixing.
slim
@slim - java hello doesn't work, until it's compiled apparently I missed the compile step and didn't know the difference between .java and .class
Maslow
+3  A: 

I take it you did you actually compile the class first?:

javac hello.java
java hello
Henry
I thought java wasn't a compiled language.
Maslow
@Maslow: of course it is a compiled language. It's compiled to byte code.
Joachim Sauer
It sure is - but if you want to pretend it isn't you could take a look at groovy.
Henry