title: What's wrong with my java code
body: <p>I'm learning java. 
  My sample's directory is like the following:</p>
  
  <blockquote>
    <p>./src<br>
    ./src/yxy<br>
    ./src/yxy/pack<br>
    ./src/yxy/pack/testf<br>
    ./src/yxy/pack/testf/Person.java<br>
    ./Hello.java<br>
    ./Hello.class  </p>
    
    <p>./testpack<br>
    ./testpack/yxy<br>
    ./testpack/yxy/pack<br>
    ./testpack/yxy/pack/testf<br>
    ./testpack/yxy/pack/testf/Person.class<br>
    ./testpack.jar  </p>
  </blockquote>
  
  <p>The src direcotory is used as a library. It provides the Person class.<br>
  The output of the src is located in testpack direcotory.<br>
  I generate a testpack.jar according to the testpack direcotory.<br>
  The Hello.java is the main java file. It uses the Person class.  </p>
  
  <p>The <strong>Person.java</strong> source code is the following:</p>
  
  <pre><code>package yxy.pack.testf;
  
  public class Person {
    private String name;
  
    public Person(String name) {
      this.name = name;
    }
  
    public String getName() {
      return name;
    }
  }
  </code></pre>
  
  <p>The <strong>Hello.java</strong> source code is the following:</p>
  
  <pre><code>import java.io.*; //IOException
  import java.util.*; //Enumeration
  
  import yxy.pack.testf.Person;
  
  public class Hello{
  
      public static void main(String[] args)throws IOException  {
          Person p=new Person("abc");
          Class cls=Person.class;
          ClassLoader clsLoader=cls.getClassLoader();
          String clsFile=cls.getName();
          System.out.println("end");
      }
  }
  </code></pre>
  
  <p>I use the following method to generate the testpack.jar under the current directory.</p>
  
  <blockquote>
    <p>javac -verbose -d testpack  src\yxy\pack\testf\Person.java<br>
    cd testpack<br>
    jar -cvf testpack.jar ./yxy<br>
    move testpack.jar ./../testpack.jar<br>
    cd ..  </p>
  </blockquote>
  
  <p>I use the following method to build and run the app:</p>
  
  <blockquote>
    <p>javac -classpath .\testpack.jar Hello.java<br>
    java -classpath .\testpack.jar Hello  </p>
  </blockquote>
  
  <p>this is the error I get:</p>
  
  <pre>    Exception in thread "main" java.lang.NoClassDefFoundError: Hello  
      Caused by: java.lang.ClassNotFoundException: Hello  
              at java.net.URLClassLoader$1.run(Unknown Source)  
              at java.security.AccessController.doPrivileged(Native Method)  
              at java.net.URLClassLoader.findClass(Unknown Source)  
              at java.lang.ClassLoader.loadClass(Unknown Source)   
              at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)  
              at java.lang.ClassLoader.loadClass(Unknown Source)  
      Could not find the main class: Hello.  Program will exit.</pre>  
  
  <p>I don't why this happen. How can I fix?</p>
  
  <blockquote>
    <p>java  Hello</p>
  </blockquote>
  
  <pre>    Exception in thread "main" java.lang.NoClassDefFoundError: yxy/pack/testf/Person
  
              at Hello.main(Hello.java:10)
      Caused by: java.lang.ClassNotFoundException: yxy.pack.testf.Person
              at java.net.URLClassLoader$1.run(Unknown Source)
              at java.security.AccessController.doPrivileged(Native Method)
              at java.net.URLClassLoader.findClass(Unknown Source)
              at java.lang.ClassLoader.loadClass(Unknown Source)
              at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
              at java.lang.ClassLoader.loadClass(Unknown Source)
              ... 1 more</pre>
  
  <p>How can I fix this?</p>
  
  <p>===
  Anyway, I found out how to fix. Just use the following in cmd.</p>
  
  <pre><code>&gt;java -classpath testpack.jar;. Hello
  </code></pre>
  
  <p>I must add testpack.jar and current path . to the classpath.</p>