Set java environment variables and view the installation path in java

Source: Internet
Author: User

After the jdk is installed on the computer, we can set it so that the java environment can be used. Right-click my computer. Open properties. Then select "environment variables" in "advanced". In the new open interface, you must set three attributes: "JAVA_HOME", "path", and "classpath ", in this scenario, jdk is not installed. The path attribute already exists. JAVA_HOME and classpath do not exist.

I. Click "new" and write JAVA_HOME in the variable name. The variable meaning is the installation path of java, then, write the installation path "C: \ jdk1.6" in the variable value ". (Note: if the installation path is not disk C or is not in the jdk1.6 folder, you can modify it accordingly. The following text is assumed to be installed in C: \ jdk1.6 .)
2. Find the path in the system variable and click edit. The meaning of the path variable is that the system can recognize java commands in any path, and the variable value is ".; % JAVA_HOME % \ bin "(" % JAVA_HOME % "indicates the value of JAVA_HOME just set), or you can directly write" C: \ jdk1.6 \ bin"
3. Click "new" and then write classpath on the variable name. The variable indicates the path of the java loading class (class or lib). Only the class is in classpath, java commands can be recognized. The value is ".; % JAVA_HOME % \ lib \ dt. jar; % JAVA_HOME % \ lib \ toos. jar (to add. indicates the current path) ", which means the same as" % JAVA_HOME %"

After setting the three variables, press "OK" until the attribute window disappears. Verify whether the installation is successful. Open "start"> "run", and press "cmd" to go to the DOS system interface. Then, press "java-version". If the installation is successful. The system displays java version jdk "1.6.0 ".
Make sure that the file name on drive C is jdk1.6 and the environment variables can be copied directly. On the dos interface, enter javac to check whether the command is legal, and enter java to check whether the command is legal.

Windows:

The code is as follows: Copy code
Set java_home: view the JDK installation path
Java-version: view the JDK version.


Linux:

The code is as follows: Copy code

Whereis java
Which java (java execution path)
Echo $ JAVA_HOME

Echo $ PATH

2. Configure the linux environment variables as follows: (remember source. bash_profile)
. Modify the/etc/profile File (global users)


Vi this file/etc/profile
Add the following content to the end of the profile file:

The code is as follows: Copy code
Export JAVA_HOME =/usr/share/jdk1.6.0 _ 20
Export PATH = $ JAVA_HOME/bin: $ PATH
Export CLASSPATH =.: $ JAVA_HOME/lib/dt. jar: $ JAVA_HOME/lib/tools. jar

. Modify the. bash_profile file (a user can use these environment variables)
Add the following content to the end of the. bash_profile file:

The code is as follows: Copy code


Export JAVA_HOME =/usr/share/jdk1.6.0 _ 20
Export PATH = $ JAVA_HOME/bin: $ PATH
Export CLASSPATH =.: $ JAVA_HOME/lib/dt. jar: $ JAVA_HOME/lib/tools. jar
. Shell terminal to execute the following command: (for temporary use)
Export JAVA_HOME =/usr/share/jdk1.6.0 _ 14
Export PATH = $ JAVA_HOME/bin: $ PATH
Export CLASSPATH =.: $ JAVA_HOME/lib/dt. jar: $ JAVA_HOME/lib/tools. jar


In this way, the environment variables are set. The following describes PATH, CLASSPTH, and JAVA_HOME.

Set JAVA_HOME, CLASSPATH, and PATH as follows:
1. Set JAVA_HOME:
1. For reference convenience, for example, if your JDK is installed in the C: \ Program Files \ Java \ jdk1.6.0 Directory, set JAVA_HOME to the directory path, to use this path in the future, you only need to enter % JAVA_HOME % to avoid entering a long path string for each reference;
II. Normalization principle: when your JDK path is forced to change, you only need to change the variable value of JAVA_HOME. Otherwise, you need to change any document that uses the absolute path to reference the JDK directory, if you fail to complete the changes and a program cannot find the JDK, the consequence is that the system crashes!
3. The third-party software will reference the agreed JAVA_HOME variable. Otherwise, you will not be able to use the software normally. After using JAVA for a long time, you will know that if a software cannot be used normally, think about the problem.

2. Set CLASSPATH:
This is a very interesting question, and of course it is also a problem for beginners. This variable is set so that the program can find the corresponding ". for example, compile a java program ---. java,. class File. If you execute java A in the current directory, the corresponding result will be obtained (provided that you have set CLASSPATH ". "). now. when the class is moved to another directory (for example, "e: \") and java A is executed, there will be A NoClassDefFindError exception because it cannot be found. class File, now you add CLASSPATH :".; e: \ "run java A again to see what the result will be ~~ :)~~~, Everything is normal. The java command finds the. class file through CLASSPATH!

3. Set PATH:
The principle is very simple. Do you want to use % JAVA_HOME % \ bin \ java to execute java commands at any time? Of course not, so, you can add % JAVA_HOME % \ bin to the PATH. In this way, we can use java to execute commands in any PATH. (When you enter your code in the command prompt window, the operating system will find the corresponding application procedure in the current directory and PATH variable directory and execute it.


3. Uninstall jdk
· Find the _ uninst subdirectory of the jdk installation directory.
· Run the command./uninstall. sh on the shell terminal to uninstall jdk.

Related Article

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.