Java environment variable setting - common problem handling

Configure JDK environment variables under windows xp:

1. Install JDK, you can customize the installation directory and other information during the installation process, for example, we choose the installation directory as

G:/java11;

2. After the installation is complete, right-click "My Computer" and click "Properties";

3. Select the "Advanced" tab and click "Environment Variables";

4. In "System Variables", set 3 attributes, JAVA_HOME, PATH, CLASSPATH (case does not matter), if it exists, click "Edit", if it does not exist, click "New";

5. JAVA_HOME indicates the JDK installation path, which is the path G:/java11 selected during the installation just now. This path includes folders such as lib, bin, and jre (this variable is best set, because tomcat, eclipse, etc. need to be run in the future) *this variable);

Path enables the system to recognize java commands in any path, set to:

%JAVA_HOME%/bin;%JAVA_HOME%/jre/bin

CLASSPATH loads the class (class or lib) path for java. Only when the class is in the classpath, the java command can recognize it. Set it to:

.;%JAVA_HOME%/lib/dt.jar;%JAVA_HOME%/lib/tools.jar (add . to indicate the current path)

%JAVA_HOME% refers to the previously specified JAVA_HOME;

6. "Start" ->; "Run", type "cmd";

7. Type in the commands "java -version", "java", and "javac", and a screen will appear, indicating that the environment variable configuration is successful;

8. All right, let's call it a day. Let's start your first java program.

Let's talk about the meaning of several java environment variables and the configuration method under linux:

Usually, we need to set three environment variables: JAVA_HOME, PATH and CLASSPATH.

JAVA_HOME: The value of this environment variable is the directory where Java is located. Some Java version software and some Java tools need to use this variable. When setting PATH and CLASSPATH, you can also use this variable for easy setting.

PATH: Specifies a list of paths to search for executable files. When executing an executable file, if the file cannot be found in the current path, it will search for each path in PATH in turn until it is found. Or if the path in PATH cannot be found, an error will be reported. Java's compilation command (javac), execution command (java) and some tool commands (javadoc, jdb, etc.) are all in the bin directory under its installation path. So we should add this path to PATH variable.

CLASSPATH: also specifies a path list, which is used to search for classes that need to be used when compiling or running Java. In addition to paths, .jar files can be included in the CLASSPATH list. When Java looks for classes, it will look for this .jar file as a directory. Usually, we need to include jre/lib/rt.jar (Linux: jre/lib/rt.jar) under the JDK installation path in the CLASSPATH.

Both PATH and CLASSPATH specify a list of paths, the items in the list (that is, the individual paths) are separated by separators. Under Windows, the delimiter is a semicolon (;), while under Linux, the delimiter is a colon (:).

The following describes how to set the three environment variables under Windows and Linux respectively, but before that, we need to make an assumption. Suppose the installation path of JDK under Windows is C:/jdk/, and the installation path under Linux is /usr/local/jdk/. Then, the installed JDK will at least include the following:

***** Setup under Windows

Use the set command to set environment variables under Windows. In order to set these environment variables every time the computer is started, it should be set in the autoexec.bat file in the root directory of the system disk, such as:

set JAVA_HOME=C:/jdk

set PATH=%JAVA_HOME%/bin;C:/Windows;C:/Windows/Command

set CLASSPATH=%JAVA_HOME%/jre/lib/rt.jar;.

Some versions of Windows cannot use % variable name% to replace the content of environment variables, so you have to write C:/jdk directly instead of %JAVA_HOME%. In addition, C:/Windows and C:/Windows/Command are automatically added to the path by Windows, so they can be removed from the settings. If PATH has been set in autoexec.bat, you only need to add %JAVA_HOME%/bin to the statement that originally set PATH.

CLASSPATH can also be set or added to other paths as needed. For example, if you want to put some classes written by yourself in C:/java, you can also add C:/java to the CLASSPATH, set CLASSPATH=%JAVA_HOME%/ jre/lib/rt.jar;C:/java;..

Note that a "current directory (.)" is included in the CLASSPATH. After this directory is included, you can go to any directory to execute a Java program that needs to use a certain class in this directory, even if the path is not included in the CLASSPATH. The reason is simple: although the path is not explicitly included in the CLASSPATH, the "." in the CLASSPATH represents the path at this time, such as:

Suppose there is an executable class HelloJava.class in the C:/java directory, then

C:/> set CLASSPATH=C:/jdk/jre/lib/rt.jar;. // Set the CLASSPATH environment variable, note that there is a "." at the end

C:/> cd java // Go to the C:/java directory

C:/java> java HelloJava // run HelloJava

Hello, Java. // Running result

C:/java> _
 

Guess you like

Origin blog.csdn.net/shugyin/article/details/127195750