javajavac

Cannot run simple compiled java program?


I am on Arch Linux, I just installed JRE and JDK and all the proper bin files (javac and java) are in /opt/java/bin/

I simply compiled a standard hello world, and compiled it with javac running javac ./hello.java and that made a class.

Now my problem is running it. I run java ./helloworld.class and it gives me an error, even if the file I point java to is non-existant:

Exception in thread "main" java.lang.NoClassDefFoundError: //helloworld/class
Caused by: java.lang.ClassNotFoundException: ..helloworld.class
(..omitted for clarity..)
Could not find the main class: ./helloworld.class.  Program will exit.

You will notice the first line of the error, it munges the path //helloworld/class

When I feed java an absolute path, i.e java /home/foo/helloworld.class it gives the same error, but replaces the path's / with . in the first line, again munged.

What do you think is wrong? I really don't know why it is doing this..


Solution

  • When you run java, you just pass it the fully qualified class name (including package), not the file name.

    java helloworld will look for helloworld.class.

    java helloworld.class will look for helloworld/class.class