3

I am currently using Ubuntu 11.10 and java SE 1.6.0_26. I am trying to run a very simple "Hello World" app. I placed the the java file HelloWorld.java on the Home folder. I compiled it using the command javac HelloWorld.java. I think its working because it doesn't show any compilation error and a HelloWorld.class is created.

When I typed the command java HelloWorld I have this error:

Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld
Caused by: java.lang.ClassNotFoundException: HelloWorld
        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: HelloWorld.  Program will exit.

BTW, here is my java code:

public class HelloWorld {
    public static void main (String args[]) {
        System.out.println("Hello World!!!");
    }
}
10
  • 2
    Which folder are you running the java command from? Commented Jun 11, 2012 at 2:13
  • @AmitBhargava Home folder. The same directory where my HelloWorld.java and HelloWorld.class resides. Commented Jun 11, 2012 at 2:14
  • 2
    Could you please try the following : java -classpath HelloWorld Commented Jun 11, 2012 at 2:16
  • Can you give us the output of ls -l on home directory Commented Jun 11, 2012 at 2:17
  • This looks like a classpath problem. There is some information here: stackoverflow.com/questions/120662/… You may need to include -classpath . Commented Jun 11, 2012 at 2:18

1 Answer 1

7

It could be that the file is not in your classpath..try the following command:

java -classpath . HelloWorld

V

Sign up to request clarification or add additional context in comments.

4 Comments

I find it strange that Java would have an issue with the classpath, unless it was improperly installed. Normally, one shouldn't have this issue with non-package Java files on (U)buntu, but this is always a good thing to know anyway.
@TheOnlyIdiot - Like he said, the problem is that the class was not on your classpath. It is all explained in the manual entry for the java command. In particular the page about setting your classpath.
@Makoto - (I suspect that the OP has set $CLASSPATH to something that doesn't work ...)
yeah..the $CLASSPATH is generally at fault..I think the main reason is that by default, Ubuntu ships with OpenJDK and not Oracle JVM which requires you to set the CLASSPATH while the latter doesn't..either you can switch or add this line to your .bashrc file in the home directory.. export CLASSPATH=$CLASSPATH:. You would have to then type source .bashrc

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.