[Reprint] Java environment variable configuration

Source: Internet
Author: User
Java environment variable configuration

Configure JDK environment variables in Windows XP:
1. Install JDK. You can customize the installation directory and other information during the installation process. For example, select D:/Java/jdk1.5.0 _ 08 as the installation directory;

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 three attributes: java_home, path, and classpath (case-insensitive). if it already exists, click "edit". If it does not exist, click "new ";

5. java_home indicates the JDK installation path, which is the path D:/Java/jdk1.5.0 _ 08 selected during the installation. This path contains Lib, bin, JRE, and other folders (it is best to set this variable, because in the future, tomcat, eclipse, and so on all need to follow * this variable );

Path allows the system to recognize Java commands in any path. It is set:

% Java_home %/bin; % java_home %/JRE/bin

Classpath is the class or lib path of the Java load class. Only the class in classpath can be identified by the Java command and set:

.; % 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 the following commands: "Java-version", "Java", and "javac". If a screen is displayed, the environment variable is successfully configured;

8. All right. Finish the work. Let's start your first Java program.

The following describes the meanings of several Java environment variables and the configuration methods in Linux:

Generally, 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 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 path list for searching executable files. When executing an executable file, if the file cannot be found in the current path, find each path in the path in sequence until it is found. Or if the path in the path cannot be found, an error is returned. Java compilation commands (javac), execution commands (Java), and some tool commands (javadoc, jdb, etc.) are all in the bin directory under the installation path. Therefore, we should add this path to the PATH variable.

Classpath: Specifies a path list, which is used to search for Classes required for Java compilation or running. In addition to paths, the classpath list can also contain. Jar files. The. jar file is used as a directory to search for classes in Java. Generally, we need to include JRE/lib/RT. Jar (Linux: JRE/lib/RT. Jar) in the JDK installation path in classpath.

Path and classpath both specify the path list. The items in the list (that is, each path) are separated by separators. In Windows, the Delimiter is a semicolon (;), while in Linux, The Delimiter is a colon (:).

The following describes how to set the three environment variables in Windows and Linux, but before that, we need to make a hypothesis. Assume that the JDK installation path in Windows is C:/JDK/, and the installation path in Linux is/usr/local/JDK /. The installed JDK will include at least the following content:

C:/JDK (/usr/local/JDK)
| -- Bin
| -- Demo
| -- Include
| -- JRE
| -- Bin
| '-- Lib
'-- Lib

* ***** Set in Windows

In Windows, use the set command to set environment variables. to set these environment variables for every startup computer, you should set them in the autoexec. BAT file under the System Disk root directory, for example:

Set java_home = C:/JDK
Set Path = % java_home %/bin; C:/windows; C:/Windows/command
Set classpath = % java_home %/JRE/lib/RT. jar ;.

In some versions of Windows, % variable name % cannot be used to replace the content of the environment variable, so you have to directly write C:/JDK instead of % java_home %. In addition, C:/windows and C:/Windows/command are automatically added to the path by windows, so you can remove them 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 as needed or added to other paths. For example, if you want to put some classes you write in C:/Java, you can put C: /Java is also added to classpath, set classpath = % java_home %/JRE/lib/RT. jar; C:/Java ;..

Note that the classpath contains a "Current Directory (.)". After the directory is included, you can go to any directory and execute Java programs that need to use a class in the directory, even if the path is not included in the classpath. The reason is simple: although the path is not explicitly included in classpath, "." In classpath represents the path, for example:

Suppose there is a runable class hellojava. Class in the C:/Java directory

C:/> set classpath = C:/JDK/JRE/lib/RT. jar;. // set the classpath environment variable. Note that there is a "."
C:/> Cd Java // go to the C:/Java directory
C:/Java> JAVA hellojava // run hellojava
Hello, java. // running result
C:/Java> _

* *** Set in Linux

In Linux, use "variable name = variable value" to set the variable and use the Export command to export it as an environment variable. To enable automatic setting of these variables for every login, you need ~ /. Bash_profile or ~. /Bashrc, as shown in

Export java_home =/usr/local/JDK
Export Path = $ java_home/bin: $ path
Export classpath = $ java_home/JRE/lib/RT. jar :.

When you set the path, $ java_home is used to replace the value of java_home with the location where $ java_home is located. The above statement is actually export Path =/usr/local/JDK/bin: $ path. In this statement, $ PATH also plays the same role, but the path here refers to the value of the previously set PATH variable, rather than the value of this set PATH variable.

Note that the classpath contains a "Current Directory (.)". After the directory is included, you can go to any directory and execute Java programs that need to use a class in the directory, even if the path is not included in the classpath. The reason is simple: although the path is not explicitly included in classpath, "." In classpath represents the path, for example

Suppose there is a runable class hellojava. Class in the/home/fancy/Java directory

[Fancy @ matrix fancy] $ export classpath =/usr/local/JDK/JRE/lib/RT. jar:. // set classpath. Pay attention to the last "."
[Fancy @ matrix fancy] $ Cd ~ /Java // go to/home/fancy/Java
[Fancy @ matrix Java] $ PWD // display the current directory
/Home/fancy/Java // the current directory is/home/fancy/Java
[Fancy @ matrix Java] $ Java hellojava // run hellojava
Hello, Java // running result
[Fancy @ matrix Java] $ _

Analysis

* ***** Instance analysis

But the operating system is different, slightly different.

Both examples refer to a "runable class", which refers to a class that contains the public static void main (string [] ARGs) method, this will be detailed in the next chapter hellojava. In this example, classpath does not contain the directory (C:/Java,/home/fancy/Java) Where hellojava. Class is located, but it contains the current directory (.). Therefore, go to include hellojava. class directory, execute Java hellojava, find the ". (Current Directory, C:/Java,/home/fancy/Java) ", found hellojava. class, run successfully.

Publish via wiz

Configure JDK environment variables in Windows XP:
1. Install JDK. You can customize the installation directory and other information during the installation process. For example, select D:/Java/jdk1.5.0 _ 08 as the installation directory;

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 three attributes: java_home, path, and classpath (case-insensitive). if it already exists, click "edit". If it does not exist, click "new ";

5. java_home indicates the JDK installation path, which is the path D:/Java/jdk1.5.0 _ 08 selected during the installation. This path contains Lib, bin, JRE, and other folders (it is best to set this variable, because in the future, tomcat, eclipse, and so on all need to follow * this variable );

Path allows the system to recognize Java commands in any path. It is set:

% Java_home %/bin; % java_home %/JRE/bin

Classpath is the class or lib path of the Java load class. Only the class in classpath can be identified by the Java command and set:

.; % 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 the following commands: "Java-version", "Java", and "javac". If a screen is displayed, the environment variable is successfully configured;

8. All right. Finish the work. Let's start your first Java program.

The following describes the meanings of several Java environment variables and the configuration methods in Linux:

Generally, 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 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 path list for searching executable files. When executing an executable file, if the file cannot be found in the current path, find each path in the path in sequence until it is found. Or if the path in the path cannot be found, an error is returned. Java compilation commands (javac), execution commands (Java), and some tool commands (javadoc, jdb, etc.) are all in the bin directory under the installation path. Therefore, we should add this path to the PATH variable.

Classpath: Specifies a path list, which is used to search for Classes required for Java compilation or running. In addition to paths, the classpath list can also contain. Jar files. The. jar file is used as a directory to search for classes in Java. Generally, we need to include JRE/lib/RT. Jar (Linux: JRE/lib/RT. Jar) in the JDK installation path in classpath.

Path and classpath both specify the path list. The items in the list (that is, each path) are separated by separators. In Windows, the Delimiter is a semicolon (;), while in Linux, The Delimiter is a colon (:).

The following describes how to set the three environment variables in Windows and Linux, but before that, we need to make a hypothesis. Assume that the JDK installation path in Windows is C:/JDK/, and the installation path in Linux is/usr/local/JDK /. The installed JDK will include at least the following content:

C:/JDK (/usr/local/JDK)
| -- Bin
| -- Demo
| -- Include
| -- JRE
| -- Bin
| '-- Lib
'-- Lib

* ***** Set in Windows

In Windows, use the set command to set environment variables. to set these environment variables for every startup computer, you should set them in the autoexec. BAT file under the System Disk root directory, for example:

Set java_home = C:/JDK
Set Path = % java_home %/bin; C:/windows; C:/Windows/command
Set classpath = % java_home %/JRE/lib/RT. jar ;.

In some versions of Windows, % variable name % cannot be used to replace the content of the environment variable, so you have to directly write C:/JDK instead of % java_home %. In addition, C:/windows and C:/Windows/command are automatically added to the path by windows, so you can remove them 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 as needed or added to other paths. For example, if you want to put some classes you write in C:/Java, you can put C: /Java is also added to classpath, set classpath = % java_home %/JRE/lib/RT. jar; C:/Java ;..

Note that the classpath contains a "Current Directory (.)". After the directory is included, you can go to any directory and execute Java programs that need to use a class in the directory, even if the path is not included in the classpath. The reason is simple: although the path is not explicitly included in classpath, "." In classpath represents the path, for example:

Suppose there is a runable class hellojava. Class in the C:/Java directory

C:/> set classpath = C:/JDK/JRE/lib/RT. jar;. // set the classpath environment variable. Note that there is a "."
C:/> Cd Java // go to the C:/Java directory
C:/Java> JAVA hellojava // run hellojava
Hello, java. // running result
C:/Java> _

* *** Set in Linux

In Linux, use "variable name = variable value" to set the variable and use the Export command to export it as an environment variable. To enable automatic setting of these variables for every login, you need ~ /. Bash_profile or ~. /Bashrc, as shown in

Export java_home =/usr/local/JDK
Export Path = $ java_home/bin: $ path
Export classpath = $ java_home/JRE/lib/RT. jar :.

When you set the path, $ java_home is used to replace the value of java_home with the location where $ java_home is located. The above statement is actually export Path =/usr/local/JDK/bin: $ path. In this statement, $ PATH also plays the same role, but the path here refers to the value of the previously set PATH variable, rather than the value of this set PATH variable.

Note that the classpath contains a "Current Directory (.)". After the directory is included, you can go to any directory and execute Java programs that need to use a class in the directory, even if the path is not included in the classpath. The reason is simple: although the path is not explicitly included in classpath, "." In classpath represents the path, for example

Suppose there is a runable class hellojava. Class in the/home/fancy/Java directory

[Fancy @ matrix fancy] $ export classpath =/usr/local/JDK/JRE/lib/RT. jar:. // set classpath. Pay attention to the last "."
[Fancy @ matrix fancy] $ Cd ~ /Java // go to/home/fancy/Java
[Fancy @ matrix Java] $ PWD // display the current directory
/Home/fancy/Java // the current directory is/home/fancy/Java
[Fancy @ matrix Java] $ Java hellojava // run hellojava
Hello, Java // running result
[Fancy @ matrix Java] $ _

Analysis

* ***** Instance analysis

But the operating system is different, slightly different.

Both examples refer to a "runable class", which refers to a class that contains the public static void main (string [] ARGs) method, this will be detailed in the next chapter hellojava. In this example, classpath does not contain the directory (C:/Java,/home/fancy/Java) Where hellojava. Class is located, but it contains the current directory (.). Therefore, go to include hellojava. class directory, execute Java hellojava, find the ". (Current Directory, C:/Java,/home/fancy/Java) ", found hellojava. class, run successfully.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.