[Java] set the CLASSPATH environment variable is whether the necessary

Previously studied Java readers may not be familiar to the CLASSPATH environment variable, almost everyone in this book describes Java's entry
will be introduced to set CLASSPATH environment variable, but the role CLASSPATH environment variable is often did not elaborate.


In fact, if you are using version 1.5 or later of the JDK, you can not set the environment variable CLASSPATH


So what role CLASSPATH environment variable is it?

When using the "java Java class name" command to run Java programs,
the JRE where to search for Java classes it? Readers may answer, in the current search path ah. The answer is very clever, but 1.4 the previous
version of the JDK do not design this function, which means that even if the current path already contains HelloWorld.class, and the current path of
execution "java Hello World", the system will be prompted to find the same less than HelloWorld class.


If you are using a previous version of the 1.4 JDK, you need to add a point in the CLASSPATH environment variable (.) To tell the JRE needs to
search the Java class in the current path.


In addition, compile and run Java programs also need Java classes and dt.jar tools.jar files under JDK lib path, so the
need to add these two files to the CLASSPATH environment variable.

Tip: JDK9 lib directory no longer contains dt.jar and tools .jar file.

Therefore, if you are using a previous version of JDK 1.4 to compile and run Java programs, often need to set the CLASSPATH environment variable
amount of value;.% JAVA HOME% \ lib \ dt.jar;% JAVA_HOME% \ lib \ tools.jar ( which % JAVA HOME% represents the JDK security
installation directory).

Tip: if you're using an earlier version of the JDK, only need to set the CLASSPATH environment variable.

 

Of course, even with JDK l. 5 above the JDK version, may be provided CLASSPATH environment variable (typically used for loading a third
party library), once set the environment variable, the JRE will press the specified environment variable path search Java class. This means that if
does not include the CLASSPATH environment variable dot (.), The current path is not included, JRE does not search for Java classes in the current path.

If you want to search temporarily assigned JRE Java class path when running a Java program, you can use the classpath options (or use -cp
option, -cp is short, exactly the same effect), that is to run the Java command in the following format:

java - classpath dirl;dir2;dir3 .. .; dirN Java 类

-classpath option value can be a series of paths with a semicolon on Windows platform among multiple paths (;) separated in
places by a colon (:) separated on the Linux platform.

If the value of -classpath option specifies when running a Java program, JRE will be in strict accordance with -classpath option to specify the path to search
search Java class that does not search for Java classes, CLASSPATH environment variable specified search path in the current path It is no longer valid.

If you want to make CLASSPATH environment variable specified search path is valid, but also Java classes in the current search path, you can
in order to run Java programs in the following format:
the Java - the CLASSPATH% CLASSPATH%;; dirl; dir2; dir3 ... .; dirN Java class

The above command to reference values% CLASSPATH% environment variable CLASSPATH and -classpath option to add value in
a point, forced JRE Java classes in the current search path.

 

Guess you like

Origin www.cnblogs.com/MasterMonkInTemple/p/12175415.html