Use of -classpath and path in Java

The use of javac -classpath:

javac: If the current java file you want to compile references other classes (for example: inheritance), but the .class file of the referenced class is not in the current directory, in this case, you need to use javac The -classpath parameter is added after the command, and the following three types of methods are used to guide the compiler to find the referenced class 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 %CLASSPATH% Xxx.java (Note: %CLASSPATH% means to use the value of the system variable CLASSPATH to search, here it is assumed that the path of Junit.jar is included in the CLASSPATH system variable)

javac Use of absolute path:

javac : Suppose the class file you want to compile is named: HelloWorld.java, and its full path is: D:/java/HelloWorld.java. But the current directory you are in is: C:/Documents and Settings/peng>. If you want to perform compilation here, what will be the result?
 
(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 javac is in the current directory by default Find the class file, obviously this path is not where we store the class file, so an error will be reported
        
(2).C:/Documents and Settings/peng>javac D:/java/HelloWorld.java is 
compiled successfully at this time.
Therefore, as long as the directory where you execute the javac command is not the directory where the class files are stored, you must explicitly specify the path to the class files in the javac command.

The use of java -classpath:

java: Assuming that our CLASSPATH is set to: D:/peng/java/pro, there are three files in this directory: HelloWorld.java / HelloWorldExtendsTestCase / HelloWorldExtendsHelloWorld. The class declarations for 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 

The use of absolute paths successfully completed the compilation of the three files. Now we execute these three .class files in the C:/Documents and Settings/peng> directory

(1).C:/Documents and Settings/peng>java HelloWorld 
      Hello World

You can see that the execution was successful. Why does the
 JVM find the D:/peng/java/pro/HelloWorld.class file when we execute the command in C:/Documents and Settings/peng>? This is because we configured the system variable CLASSPATH and pointed to the directory: D:/peng/java/pro. Therefore, the JVM will load the class file in this directory by default, and there is no need to specify the absolute path of the .class file.
         
(2).C:/Documents and Settings/peng>java HelloWorldExtendsHelloWorld
       Hello World

You can see that the execution was successful. HelloWorldExtendsHelloWorld inherits the HelloWorld class, so the JVM will first check whether there is a HelloWorld.class file in the CLASSPATH during execution. 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

You can see that the program throws an exception, indicating that the junit.framework.TestCase file cannot be found. Why is HelloWorldExtendsHelloWorld.class executed successfully under :/peng/java/pro, but not this one? This is because: The junit.framework.TestCase.class file does not exist in the current directory, so in order to make the program run successfully, we must specify the CLASSPATH so that the JVM can find the junit.framework.TestCase class, such as ( 4):

(4). C:/Documents and Settings/peng>java -classpath %CLASSPATH% HelloWorldExtendsTestCase
      Hello World

Summary:

(1). When to use -classpath: When the class you want to compile or execute references other class, but when the .class file of the referenced class is not in the current directory, you need to introduce the class through -classpath
(2). When you need to specify the path: when the directory where the class you want to compile is located and the directory where you execute the javac command When it is not the same directory, you need to specify the path of the source file (CLASSPATH is used to specify the .class path, not the path to the .java file)
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325123112&siteId=291194637