title: Java class execution problem: java.lang.ClassNotFoundException
body: <p>Below is what I tried in linux terminal: compiled Test.java, run Test.class, and got an error. Then, I tried the same command with "-classpath ." option and "-cp ." option, but also failed.</p>
  
  <pre><code>/testpackage$ cat Test.java 
  package testpackage;
  
  public class Test {
  
      /**
       * @param args
       */
      public static void main(String[] args) {
          // TODO Auto-generated method stub
          System.out.println("May I take your order?");
      }
  
  }
  /testpackage$ javac Test.java 
  /testpackage$ java testpackage.Test
  Exception in thread "main" java.lang.NoClassDefFoundError: testpackage/Test
  Caused by: java.lang.ClassNotFoundException: testpackage.Test
      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: testpackage.Test. Program will exit.
  
  /testpackage$ java -cp . testpackage.Test
  Exception in thread "main" java.lang.NoClassDefFoundError: testpackage/Test
  Caused by: java.lang.ClassNotFoundException: testpackage.Test
      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: testpackage.Test. Program will exit.
  
  /testpackage$ java -classpath . testpackage.Test
  Exception in thread "main" java.lang.NoClassDefFoundError: testpackage/Test
  Caused by: java.lang.ClassNotFoundException: testpackage.Test
      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: testpackage.Test. Program will exit.
  /testpackage$ 
  </code></pre>
  
  <p>But if I remove the package "testpackage" and recompile the source code, the resulting class file is executed well.</p>
  
  <pre><code>/testpackage$ cat Test.java
  //package testpackage;
  
  public class Test {
  
      /**
       * @param args
       */
      public static void main(String[] args) {
          // TODO Auto-generated method stub
          System.out.println("May I take your order?");
      }
  
  }
  /testpackage$ javac Test.java
  /testpackage$ java Test
  May I take your order?
  /testpackage$
  </code></pre>
  
  <p>What's wrong with my code, or execution command? Please help me.
  Thank you. :)</p>