I can't seem to sort out this error using very basic packages - I think this may be resolved with a flag with javac, but first things first.
package hello;
public class HelloWorld{
public static void main(String[] args){
System.out.println("Hello World!");
}
}
Compiles fine, but :$ java HelloWorld produces:
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld (wrong name: hello/HelloWorld)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
at java.net.URLClassLoader.access$100(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
:$ java hello.HelloWorld produces:
Exception in thread "main" java.lang.NoClassDefFoundError: hello/HelloWorld
I know this is so basic and I've worked with packages previously, just not on my home machine (running 1.5 under OS X) before.
"Dj Percent",
Welcome to the ranch. You may not be aware of the ranch Naming Policy. Please read it carefully and change your name accordingly (obviously fake name are not allowed). Thank you.
You need to refer to HelloWorld via its fully qualified name. The easiest way is to go to the parent directory of the hello package and call : java -cp . hello.HelloWorld