2. Functions of classpath, path, and JAVA_HOME, classpathjava_home

Source: Internet
Author: User

2. Functions of classpath, path, and JAVA_HOME, classpathjava_home

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: Specifies the JDK installation path, which is the path D: \ java \ jdk1.5.0 _ 08 selected during installation. This path includes lib, bin, jre and other folders (it is best to set this variable, because tomcat and eclipse will depend on this variable in the future)

Path: allows the system to identify java commands in any Path, set to: % JAVA_HOME % \ bin; % JAVA_HOME % \ jre \ bin

CLASSPATH: class or lib path for java loading. Only classes in classpath can be identified by java commands and set :.; % JAVA_HOME % \ lib \ dt. jar; % JAVA_HOME % \ lib \ tools. jar (to add. indicates the current path)

% JAVA_HOME % refers to the previously specified JAVA_HOME;

6. "Start" → "run", and 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 method 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 should at least contain the following content: C: \ jdk (/usr/local/jdk)

| -- Bin

| -- Demo

| -- Include

| -- Jre

| -- Bin

| '-- Lib <br>' -- lib *****

Set in Windows </p> in Windows, use the set command to set environment variables. To enable these environment variables to be set for each startup computer, you should set them in the autoexec directory under the System Disk root directory. set in the bat file, such as: set JAVA_HOME = C: \ jdk <br> set PATH = % JAVA_HOME % \ bin; C: \ Windows; C: \ Windows \ Command <br> 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 or added to other paths as needed. For example, if you want to put some classes you write in C: \ java, you can set 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, "This indicates the path, for example, </p> assume that there is a runable class HelloJava under the C: \ java directory. class, then </p> C: \ & gt; set CLASSPATH = C: \ jdk \ jre \ lib \ rt. jar ;. // set the CLASSPATH environment variable. Note that "."

C: \ & gt; 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_pro file or ~. /Bashrc, such as </p> export JAVA_HOME =/usr/local/jdk <br> export PATH = $ JAVA_HOME/bin: $ PATH <br> 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 and pay attention to the last ". "[fancy @ matrix fancy] $ cd ~ /Java // go to/home/fancy/java <br> [fancy @ matrix java] $ pwd // display the current directory

/Home/fancy/java // the current directory is/home/fancy/java <br> [fancy @ matrix java] $ java HelloJava // run HelloJava

Hello, Java // running result

[Fancy @ matrix java] $ _ Analysis

* ***** Instance analysis only varies slightly with operating systems.

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.