I'm just trying to compile a simple hello world file via the terminal.
Here's the code for Hello.java
:
package Hello;
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
this is saved in another folder, so in terminal I typed:
cd code/repositories/java
to navigate to the correct directory (where I saved Hello.java
)
I next typed javac Hello.java
and hit return. It compiled without any errors.
I then tried to open the file with java Hello
and it threw the following exception:
Exception in thread "main" java.lang.NoClassDefFoundError: Hello (wrong name: Hello/Hello)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
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
How/why does this happen and how do I go about fixing it?