The Java path is confusing for many people, especially beginners. I will explain it here. We have two phases: one is the compilation phase and the other is the running phase.
I. compilation phase
At this time, we need to provide two path parameters to the compiler. The first one isSource codeWhere? That is, sourcepath. Where is the dependent class? That is, classpath
Let's look at an example.
D:/temp/testcp/D1/P1/class1.java
Package P1; <br/> public class class1 {<br/> Public void show () {<br/> system. out. println ("class1"); <br/>}< br/>}
D:/temp/testcp/D2/P2/class2.java
Package P2; <br/> Import p1.class1; <br/> public class class2 {<br/> Public static void main (string [] ARGs) {<br/> class1 C1 = new class1 (); <br/> c1.show (); <br/>}< br/>}
When compiling, we first compile class1 in D:/temp/testcp/d1
D:/temp/testcp/D1> javac P1/class1.java
Compiled successfully
Then we compile class2 under D:/temp/testcp/D2. There are two methods for compilation:
First, specify the source file sourcepath.
D:/temp/testcp/D2> javac-sourcepath ../d1 P2/class2.java
2. Specify classpath
D:/temp/testcp/D2> javac-sourcepath.-CP ../d1 P2/class2.java
Both methods can achieve what we need. That is to say, in the path, the system will automatically find the source file or class file based on the package.
In another case, if we need the support of a class library, such as a jar file, we can not only include the path, but should include the entire path and file name, such
D:/temp/testcp/D2> javac-sourcepath.-CP ../d1; D:/lib/spring. Jar P2/class2.java
2. At runtime
We need to specify where other classes are and where the class libraries we need are, so the running command should be
D:/temp/testcp/D2> JAVA-CP ..; ../d1; D:/lib/spring. Jar p2.class2
Class1
Summary:
The path can be divided into two types. If it is in the jar or ZIP format, it must contain the complete path and file name, and the case cannot be wrong.
If it is a class file, you only need to include the path.