JDK and ant environment variable configuration

Source: Internet
Author: User

Configure JDK environment variables in Windows:
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.

Configuration of JDK environment variables 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

Meanings of environment variables:

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.

Ant environment variable Configuration:

Ant_home = D:/Apache-ant-1.7.0

Ant_path = % PATH %; D: // APACHE-ant-1.7.0/bin

The previous % PATH % indicates a reference to the previous (JDK) environment variables.



Install JDK of different versions:

Jdk1.6 was first installed on my machine, and later jdk1.4. However, I have not changed the environment variable, but it still points to the jdk1.6 path. However, the input of Java-version in the CMD window is 1.4.2.
Query environment variables:

1. My computer-> properties-> advanced-> Environment Variables

2. system environment variables (HKEY_LOCAL_MACHINE/system/CurrentControlSet/control/Session Manager/environment)
Current user environment variable (HKEY_CURRENT_USER/environment)
 
The two settings are identical and all point to 1.6.
In order to find out the cause of the problem, you can query the relevant information as follows:
During JDK installation, the following three items are generated in the registry:
HKEY_LOCAL_MACHINE/software/javasoft/Java Development Kit
HKEY_LOCAL_MACHINE/software/javasoft/Java Plug-in
HKEY_LOCAL_MACHINE/software/kerberoft/Java Runtime Environment
At the same time, the jdkinstaller will copy the three executable files java.exe, javaw.exe, and javareg.exe to the % SystemRoot %/system32 directory, because % SystemRoot %/system32 is searched by the path with the highest priority set by the operating system by default, you can run java.exe to start JVM in the command line directory.
Query HKEY_LOCAL_MACHINE/software/systoft/Java Runtime Environment and find CurrentVersion = 1.4. So I manually changed it to 1.6, and then run Java-version to get the following results:
Registry key'software/container oft/Java Runtime Environment/
Has value '1. 6', but '1. 4' is required.
Error: cocould not find java. dll
Error: cocould not find Java 2 Runtime Environment.
 
Therefore, you cannot simply modify the CurrentVersion value of the registry to change the running JRE. If you modify currentversionby others, the Java. dll and JVM. dll versions that are not consistent with your current version will be loaded, which will cause unpredictable consequences!
Generally, when two or more sets of Java2 sdks are installed in the system, the Java2 sdks installed later will copy the java.exeand javaw.exe files in the library to the % SystemRoot %/system32directory directory, and overwrite the original java.exe and javaw.exe files, in the registry, change CurrentVersion to the installed version.Therefore, we recommend that you uninstall the previously installed version before installing the Java2 SDK.

 

The following describes how to install multiple JDK versions in my system:

1. Install JDK 1.3 or a later version. This ensures that any version of eclipse can be installed. (otherwise, if JDK is installed and eclipse is a later version, the system will prompt that the JDK version is low and cannot be installed; if a later version of JDK is installed, but a later version of JDK is uninstalled, only the later version of JDK and the later version of eclipse are installed on the computer, when running eclipse, only the startup interface is displayed and then disappears ).

2. Install eclipse. After a later JDK version is installed, eclipse installation is normal.

3. Depending on the situation, check the JDK of the program you want to run (some programs may be developed using JDK 1.3 ), if I want to run the program under jdk1.3, I will

(1) copy the installed jdk1.3 folder from another computer

(2) set path and classpath to the directory of the folder.

(3) add and check jdk1.3 in Eclipse/window/preferences/Java/installed jres/and check jdk1.5.

(4) set the compliance in Eclipse/window/preferences/Java/compiler to 1.3. (If 1.5 is selected, it may be compiled correctly in the command environment, but the eclipse reports the following error:Java. Lang. unsupportedclassversionerrorCocould
Not found main class). The possible error is that the default package in eclipse is compiled with a positive error, but the class fails to be compiled when you use a self-created package.

For more information, see:

Http://www.blogjava.net/zhyiwww/archive/2007/01/16/94235.html

Http://blog.csdn.net/ybyb14/archive/2007/05/09/1602230.aspx

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.