Classpath explanation:
-Classpath:
Sets the user class path, which will overwriteCLASSPATHPath of the user class in the environment variable. If noCLASSPATHNot Specified-ClasspathThe user class path consists of the current directory.
-Sourcepath:
Specifies the source code path used to find the class or interface definition. Like the user class path, the Source Path item uses a semicolon (;Directory, JAR file, or ZIP file. If a package is used, the local path name in the directory or archive file must reflect the package name.
If the source file of a class found through the class path is found, it may be automatically re-compiled.
The above statements are concise, but the specific operations are not clear. Let's look for several examples to analyze them:
Example:
I:
When compiling a servlet, you must specify the class package, that is, specify the corresponding source file for compilation. Otherwise, the import class package cannot be found.
The directory structure of tomcat is as follows: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + signature/Signature + signature + CjxwPgrS1M/Signature/u9sr3io7o8L3A + CjxwPgq2/Signature/wry94bm5o7o8L3A + Signature ="
|-Foo-|
|-Testfoo. java
|-Baz-|
|-Testbaz. java
Classes-|
Testfoo. java:
Package foo;
Public class Testfoo {
//....
}
Testbaz. java:
Package baz;
Import foo. Testfoo;
Public class Testbaz {
//...
}
Testbaz references Testfoo.
Our goal is to compile Testbaz into the directory classes and try the following command:
Javac-d classes src/baz/Testbaz. java
Command failed because Testfoo on which it depends cannot be found. We can implement the following three methods:
1. The easiest thing to consider is to compile the dependent Testfoo class and add it to the CLASSPATH of Testbaz.
Javac-d classes src/foo/Testfoo. java
Javac-d classes-classpath classes src/baz/Testbaz. java
The second line uses-classpath, which enables the compiler to use classes as the root directory when searching for the Testfoo class. Based on the root directory and package name, the class name finally locates the (Compiled) Testfoo class to be used.
2. Add the dependent Testfoo class to SOURCEPATH when compiling Testbaz.
Javac-d classes-sourcepath src/baz/Testbaz. java
Add-verbose in the compilation conditions. We can clearly see that the compiler uses src as the root directory when looking for the Testfoo class, according to the root directory and package name, the class name finally locates the (source code) Testfoo class to be used.
3. Add both CLASSPATH and SOURCEPATH.
Javac-d classes src/foo/Testfoo. java
Javac-d classes-sourcepath src-classpath classes src/baz/Testbaz. java
Both attributes are added. The Compiler first checks whether Testfoo. class and Testfoo. java both exist. If it exists independently, it is applicable to one of the above methods. If both exist, judge whether the. class is the latest compilation of. java. If there are differences, re-compile. java to overwrite. class.
It is necessary to add the-d parameter:
The-d parameter is a good parameter for separating the source code from the target code. -D specifies the root directory of the target code. The structure of the source file package is reflected in the root directory as a directory. In the US,-d needs to specify an existing Directory and cannot be created automatically.