Home >Java >javaTutorial >java environment variable settings and java view installation path
Friends who use Java development may know that the environment variable settings and installation paths in Java vary depending on the system. Let me introduce how to set and view them under Linux and Windows systems.
After installing jdk into the computer, let's set it up so that the java environment can be used. First, right-click on My Computer. Open properties. Then select "Environment Variables" in "Advanced". The system variables in the new opening interface need to set three attributes: "JAVA_HOME", "path", and "classpath", among which in an environment where jdk has not been installed. The path attribute originally exists. And JAVA_HOME and classpath do not exist.
1: Click "New", then write JAVA_HOME in the variable name. As the name implies, the meaning of this variable is the installation path of java, haha, then write the path just installed "C:\jdk1.6" in the variable value ". (Note: If the installation path is not disk C or is not in the jdk1.6 folder, it can be modified accordingly. The following text is assumed to be installed in C:\jdk1.6.)
Two: Next, find the path in the system variable , and then click Edit. The meaning of the path variable is that the system can recognize the java command in any path, and the variable value is ".;%JAVA_HOME%\bin", (where "%JAVA_HOME%" means the value of JAVA_HOME just set ), you can also directly write "C:\jdk1.6\bin"
Three: Finally, click "New", and then write classpath on the variable name. The meaning of this variable is the path to load the class (class or lib) for java , only if the class is in the classpath, the java command can recognize it. The value is ".;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\toos.jar (add . to indicate the current path)", which is the same as "%JAVA_HOME% has the same meaning"
the above three After the variables are set, press "OK" until the properties window disappears. The next step is to verify whether the installation is successful. First open "Start" -> "Run", type "cmd" to enter the dos system interface. Then hit "java -version" if the installation is successful. The system will display java version jdk "1.6.0".
Make sure the file name installed on the C drive is jdk1.6. Just copy the environment variable directly. Enter javac in the dos interface to check whether the command is legal. Enter java to check whether the command is legal.
windows:
set java_home:查看JDK安装路径 java -version:查看JDK版本
linux:
whereis java which java (java执行路径) echo $JAVA_HOME echo $PATH
Two: The following are the environment variables for configuring linux: (remember source .bash_profile)
. Modify the /etc/profile file (global for all users)
vi This file /etc/profile
is added at the end of the profile file :
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 certain user has permission to use these environment variables)
Add at the end of the .bash_profile file:
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终端执行下列命令: (临时用) 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. Let’s explain PATH, CLASSPTH, JAVA_HOME
below For the purpose of setting JAVA_HOME, CLASSPATH, PATH:
1. Set JAVA_HOME:
1. For convenience of reference, for example, if your JDK is installed in the C:\Program Files\Java\jdk1.6.0 directory, set JAVA_HOME as the directory path , then when you want to use this path in the future, just enter %JAVA_HOME% to avoid entering a long path string for each reference;
2. The principle of normalization, when your JDK path is forced to change, you You only need to change the variable value of JAVA_HOME. Otherwise, you will have to change any document that uses an absolute path to reference the JDK directory. If you do not change everything, a certain program cannot find the JDK, and the consequences can be imagined - ---System crash!
3. Third-party software will reference the agreed JAVA_HOME variable. Otherwise, you will not be able to use the software normally. You will know after using JAVA for a long time. If a certain software cannot be used normally, you might as well think about it. Is this the problem?
2. Set CLASSPATH:
This is a very interesting problem, and of course it also tortures beginners. The purpose of setting this variable is so that the program can find the corresponding ".class" file. Let's give an example. : If you compile a JAVA program ---A.java, you will get a class file of A.class. If you execute java A in the current directory, you will get the corresponding results (provided that you have set CLASSPATH to ".") . Now, if you move A.class to another directory (for example: "e:\") and execute java A, there will be a NoClassDefFindError exception because the .class file cannot be found. Now you increase CLASSPATH to :".;e:\"Run java A again and see what the results will be~~:)~~~, everything is normal, the java command found the .class file through CLASSPATH!
3. Set PATH:
It makes sense Simple, do you want to use %JAVA_HOME%\bin\java to execute java commands at any time? Of course not, so you can choose to add %JAVA_HOME%\bin to the PATH path. In this way, we can execute java commands in any path. You can just use java to execute the command. (When you enter your code in the command prompt window, the operating system will search for the corresponding application in the current directory and the PATH variable directory, and execute it.
3. Uninstall jdk
·Find the _uninst subdirectory of the jdk installation directory
·Execute the command ./uninstall.sh in the shell terminal to uninstall jdk.
For more articles related to java environment variable setting and java viewing installation path, please pay attention to the PHP Chinese website!