Use of javac-classpath:
Javac: if other classes (for example, inheritance) are referenced in the Java file to be compiled. the class file is not in the current directory. In this case, you must add the-classpath parameter after the javac command, you can use the following three methods to guide the compiler to search for reference classes in the specified path during compilation.
(1). absolute path: javac-classpath C:/junit3.8.1/JUnit. Jar XXX. Java
(2). Relative Path: javac-classpath ../junit3.8.1/JUnit. javr XXX. Java
(3 ). system variable: javac-classpath % XXX. java (Note: % classpath % indicates using the value of the system variable classpath for search. Here we assume JUnit. the jar path is included in the classpath system variable)
Use of the absolute javac path:
Javac: Suppose the name of the class to be compiled is helloworld. Java, and its full path is D:/Java/helloworld. java. However, your current directory is: C:/Documents and Settings/Peng>. If you want to execute compilation here, what will happen?
(1). C:/Documents and Settings/Peng> javac helloworld. Java then the compiler will give the following error message:
Error: cannot read: helloworld. Java
This is because by default, javac searches for class files in the current directory. Obviously, this path is not where class files are stored, so an error is reported.
(2). C:/Documents and Settings/Peng> javac D:/Java/helloworld. Java
The compilation is successful.
Therefore, as long as the directory for executing the javac command is not the directory for storing class files, you must explicitly specify the path of class files in the javac command.
Use of Java-classpath:
Java: assume that our classpath is set to D:/Peng/Java/Pro. There are three files in this directory: helloworld. Java/helloworldextendstestcase/helloworldextendshelloworld. The class declarations of these three files are as follows:
Helloworld. Java: public class helloworld
Helloworldextendshelloworld. Java: public class helloworldextendshelloworld extends helloworld
Helloworldextendstestcase. Java: public class helloworldextendstestcase extends JUnit. Framework. testcase
Suppose we have successfully compiled three files according to the above usage of the javac-classpath and javac absolute paths. Now we can execute these three. class files in the C:/Documents and Settings/Peng> directory.
(1). C:/Documents and Settings/Peng> JAVA helloworld
Hello World
The execution is successful. Why can the JVM find the D:/Peng/Java/Pro/helloworld. Class file by executing the command in C:/Documents and Settings/Peng>? This is because we have configured the system variable classpath and pointed to the Directory D:/Peng/Java/Pro. Therefore, by default, the JVM will load class files in this directory without specifying the absolute path of the. Class file.
(2). C:/Documents and Settings/Peng> JAVA helloworldextendshelloworld
Hello World
The execution is successful. Helloworldextendshelloworld inherits the helloworld class. Therefore, during execution, the JVM first checks whether a helloworld exists under classpath. class file, because we have successfully compiled the helloworld class, we can successfully execute helloworldextendshelloworld. class
(3). C:/Documents and Settings/Peng> JAVA helloworldextendstestcase
Exception in thread "Main" Java. Lang. noclassdeffounderror: JUnit/framework/testcase
The program throws an exception and prompts that the JUnit. Framework. testcase file cannot be found. Why is it that helloworldextendshelloworld. class can be successfully executed under/Peng/Java/Pro? This is because: JUnit. framework. testcase. the class file does not exist in the current directory. To make the program run successfully, we must specify the classpath method so that the JVM can find the JUnit. framework. the testcase class, such as (4 ):
(4). C:/Documents and Settings/Peng> JAVA-classpath % helloworldextendstestcase
Hello World
Summary:
(1 ). -classpath: when the class to be compiled or executed references other classes. when the class file is not in the current directory, you must use-classpath to introduce the class.
(2 ). when to specify the path: When the directory of the class you want to compile is not the same as the directory where the javac command is executed, You need to specify the path of the source file (classpath is used to specify. class path, not used to specify. java file path)