To run your compliled program you will use the java virtual machine JVM. Now follow these steps EXACTLY. No really follow them EXACTLLLLLYYYY! If you dont you will get a class not defined excpetion which is bad, mkay?

  1. Use cd to go to your example1 folder.
  2. cd .. (that is cd followed by two full stops)
  3. java exmple1.MyFirstProgram

You will either get it to say "hello world" or...

Exception in thread "main" java.lang.NoClassDefFoundError: MyFirstProgram (wrong name: example1/MyFirstProgram)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:632)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)

Epic fail! Java references code through package names so you must execute it by typing in the full package name followed by the class name. Notice that their is no ".class" or ".java" at the end. The reason why we must go back one directory is that java is actually using the package name to find the correct folder. So by typing in "example1.MyFirstProgram" we are saying look for a folder called example1 with a class file called MyFirstProgram.class.

next summary >>

 

 
Install
Menu
Search