Detail the use of classpath and paths in Java

  • 2020-06-19 10:19:44
  • OfStack

Use of javac-classpath:

javac: if you want to compile Java current file references the other classes (for example: inheritance), but the reference classes. class file is not in the current directory, this case needs to be in javac command followed by - classpath parameters, by using the following three types of methods to guide the compiler at compile time to find the reference classes under the specified path.

(1). An absolute path: javac - classpath c: / junit3. 8.1 / junit jar Xxx. java

(2). Relative path: ES23en-ES24en.. . / junit3. 8.1 / Junit. javr Xxx java

(3). System variable: ES33en-ES34en %CLASSPATH% Xxx. java (note: %CLASSPATH% means that the value of system variable CLASSPATH is used for lookup, and it is assumed that the path of ES40en. jar is included in the CLASSPATH system variable)

Use of javac absolute path:

javac: suppose you to compile the class file name is: HelloWorld. java, its full path is: D: / java/HelloWorld java. But the current directory you are in is C:/Documents and Settings/peng > . What happens if you try to compile here?

(1).C:/Documents and Settings/peng > The compiler gives the following error message:
error: cannot read: HelloWorld.java

This is because by default javac looks for class files in the current directory, which is obviously not where we store the class files, so an error will be reported

(2).C:/Documents and Settings/peng > javac D:/java/HelloWorld.java
The compilation succeeded.

So, as long as you execute the javac command in a directory other than the one where the class files are stored, you must explicitly specify the path to the class files in the javac command.

Use of java-ES90en:

java: suppose we CLASSPATH set to: D: / peng/java/pro, in that directory has 3 files: HelloWorld. java/HelloWorldExtendsTestCase/HelloWorldExtendsHelloWorld. The class declarations of the three files are as follows:

HelloWorld: public class HelloWorld
HelloWorldExtendsHelloWorld: public class HelloWorldExtendsHelloWorld extends HelloWorld
HelloWorldExtendsTestCase.java: public HelloWorldExtendsTestCase extends framework

Assume that we have successfully compiled three files using the javac-ES131en and javac absolute paths above. Now we are at C:/Documents and Settings/peng > Execute the three.class files in the directory

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

Hello World

You can see that the execution was successful. Why are we at C:/Documents and Settings/peng > Execute commands, JVM D can be found: / peng/java/pro/HelloWorld class file? This is because we CLASSPATH configuration the system variables, and point to a directory: D: / peng/java/pro. So JVM will load the class files in that directory by default, without specifying 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 when executing JVM first looks for an ES187en.class file under CLASSPATH. Since we have successfully compiled the HelloWorld class, we can execute ES190en.class successfully

(3).C:/Documents and Settings/peng > java HelloWorldExtendsTestCase
Exception in thread "main" java.lang.NoClassDefFoundError: junit/framework/TestCase

You can see that the program is throwing an exception. The junit.framework.TestCase file is not found. Why the same: / peng java/pro, HelloWorldExtendsHelloWorld. class can succeed, and this is alright? . This is because: junit framework. TestCase. class file does not exist in the current directory, so in order to be able to let the program run successfully, we must, by means of designated CLASSPATH JVM can find junit. framework. TestCase this class, such as (4) :

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

Hello World

Conclusion:

(1). When you need to use -ES232en: When the class you want to compile or execute references another class, but the.class file of the referenced class is not in the current directory, you need to use -ES234en to introduce the class

(2). When you need to specify a path: If the class you want to compile is in a directory other than the one in which you execute the javac command, you need to specify a path to the source file (CLASSPATH is used to specify a path to.class, not.java).


Related articles: