Java in bash at university fails with NoClassDefFoundError
-
18-09-2019 - |
Question
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)
Solution
I take it you did you actually compile the class first?:
javac hello.java
java hello
OTHER TIPS
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
.
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")
java -verbose hello