1. Compiling Java classes with package declarations
Package AA;
Public class B {
public static void Main (string[] args)
{
System.out.println ("F");
}
}
If you compile this file with Javac , you will get an error if you execute it directly with Java B.
Exception in thread "main" java.lang.noclassdeffounderror:b (wrong name:aa/b)
At Java.lang.ClassLoader.defineClass1 (Native Method)
At Java.lang.ClassLoader.defineClass (classloader.java:800)
At Java.security.SecureClassLoader.defineClass (secureclassloader.java:142)
At Java.net.URLClassLoader.defineClass (urlclassloader.java:449)
At java.net.urlclassloader.access$100 (urlclassloader.java:71)
At Java.net.urlclassloader$1.run (urlclassloader.java:361)
At Java.net.urlclassloader$1.run (urlclassloader.java:355)
At java.security.AccessController.doPrivileged (Native Method)
At Java.net.URLClassLoader.findClass (urlclassloader.java:354)
At Java.lang.ClassLoader.loadClass (classloader.java:425)
At Sun.misc.launcher$appclassloader.loadclass (launcher.java:308)
At Java.lang.ClassLoader.loadClass (classloader.java:358)
At Sun.launcher.LauncherHelper.checkAndLoadMain (launcherhelper.java:482)
The right thing to do is to go back to the directory where AA is located, like I am now .../java/aa/, then fallback a directory to,,./java/, then execute Java AA. B
This is the time to execute normally.
2. The classes in other files are declared in a single file.
Compilation: Two classes under the same package AA, Class B is declared in Class A; Directory for Java/aa/b.java note A.java is also in this directory. Then switch the working directory to Java , Javac Aa/a.java, A and B two classes will be compiled at the same time. can also be directly in the AA directory compiled , but at this time to add Classpath,
Javac-classpath. /A.java
Run: There are two different ways to run depending on the working directory. in the Java directory , the direct Java AA. A. if in the AA directory , then add Classpath to specify Java-classpath. /AA. A. The second mode of operation, as described here, can also be used in the case of 1.
2. With Jar Package compilation
Also specify Classpath, for example, if the/home/jar/org.json.jar package is referenced
Then Javac-classpath/home/jar/org.json.jar A.java
Http://wenku.baidu.com/link?url=tG2_ Fskx8fklkkt9xry3gakivgpysplbpleygvcwdyrbbd4znxvlzu7j7uryip2j2myjaurocoj4pqxmhbqzco5pqf3fyp5_bbhyi1zy0bk
JAVAC Compiling Java files