JAVAC Compiling Java files

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.