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!!!");
    }
}
Greg Kopff
  • 15,945
  • 12
  • 55
  • 78
TheOnlyIdiot
  • 1,182
  • 7
  • 17
  • 37
  • 2
    Which folder are you running the java command from? – Chetter Hummin Jun 11 '12 at 02:13
  • @AmitBhargava Home folder. The same directory where my HelloWorld.java and HelloWorld.class resides. – TheOnlyIdiot Jun 11 '12 at 02:14
  • 2
    Could you please try the following : java -classpath HelloWorld – Chetter Hummin Jun 11 '12 at 02:16
  • Can you give us the output of `ls -l` on home directory – jmj Jun 11 '12 at 02:17
  • This looks like a classpath problem. There is some information here: http://stackoverflow.com/questions/120662/could-not-find-the-main-class-program-will-exit You may need to include -classpath . – teambob Jun 11 '12 at 02:18
  • @AmitBhargava It shows a long lis of instructions. The first instruction is Usage: java [-options] class [args...] (to execute a class) or java [-options] -jar jarfile [args...] (to execute a jar file) – TheOnlyIdiot Jun 11 '12 at 02:19
  • Yet more information on this problem: http://stackoverflow.com/questions/1417328/could-not-find-the-main-class – teambob Jun 11 '12 at 02:19
  • @JigarJoshi It shows 3 files: HelloWorld.class, HelloWorld.java, and HelloWorld.java~. – TheOnlyIdiot Jun 11 '12 at 02:20
  • Just wanted the output to check if you have appropriate permission, Generally user in home folder has the access but just to check – jmj Jun 11 '12 at 02:22
  • @JigarJoshi Thanks I didn't know that Ubuntu has some permission issues. Very new to Ubuntu. – TheOnlyIdiot Jun 11 '12 at 02:23

1 Answers1

7

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

java -classpath . HelloWorld

V

vellvisher
  • 484
  • 4
  • 12
  • 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. – Makoto Jun 11 '12 at 02:25
  • 1
    @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. – Stephen C Jun 11 '12 at 02:26
  • 2
    @Makoto - (I suspect that the OP has set $CLASSPATH to something that doesn't work ...) – Stephen C Jun 11 '12 at 02:27
  • 1
    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` – vellvisher Jun 11 '12 at 02:36