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:
- create a file called HelloWorld.java;
- paste the code posted below
- inside HelloWorld.java: compile it by executing the command: javac HelloWorld.java in the same folder as HelloWorld.java is in;
- 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!