0

I was having trouble with a could not find main class error with a somewhat complicated program I was working on. To eliminate possible problems I decided to try a hello world program to see if I could get that to work. I am working on a server which I'm pretty sure is running Red Hat Enterprise 6. I followed these steps provided by Bart Kiers in answer to this question:

  1. create a file called HelloWorld.java;
  2. paste the code posted below
  3. inside HelloWorld.java: compile it by executing the command: javac HelloWorld.java in the same folder as HelloWorld.java is in;
  4. execute the code by doing: java -cp . HelloWorld in the same folder as HelloWorld.java is in.

I get the following error after the last step:

    Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld/
Caused by: java.lang.ClassNotFoundException: HelloWorld.
        at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: HelloWorld.. Program will exit.

If I type java -version, the version info displays, suggesting that my PATH variable is set correctly. Does anyone have any other suggestions for things that might be causing this error? Thanks!

4

1 回答 1

1

看起来您在班级名称的末尾加上了一个句点:

java -cp . HelloWorld.

改为这样做

java -cp . HelloWorld
于 2013-07-21T14:32:15.950 回答