Java environment variable configuration - Windows

Java environment variable configuration - Windows

Text Keywords: Java, environment variables, JAVA_HOME, Path, ClassPath

  • Read Pilot: This article is for environment variables need to be configured and want to understand their children small partner

Little new to programming partners will have several questions:

  1. Why I finished installing so-called JDK still can not find a program that can be run directly after the ghost of what?
  2. After installation you need to configure it seems to have something called environment variables, it said to be very important.
  3. In the end how to be considered successful configuration, the computer always feel bullied white, encountered a variety of problems.

First, let's compare two important concepts related to do something to explain, I believe will help you troubleshoot the error, after all: the steady, we win.

First, the environment variable

Baidu Encyclopedia: environmental variables (environment variables) generally refers to the number of parameters used to specify the operating environment of the operating system in the operating system, such as: temporary folder location and location system folder.

If you have some basic computer students may be able to read about the meaning of, at least we know the temporary folder, the folder is what that means. So why is the environment variable this thing exist? Mainly on ease of use.
As you know, we have a lot of software and games are implemented by a variety of programming languages, including the entire operating system itself is the same. So, when we need to use pre-loaded with necessary, or read critical system information and configuration. If each time by the user to select or modify the configuration of each software, that would be too much trouble.

  • To open environment variable

Right-computer (this computer) -> Advanced System Settings
Java environment variable configuration - Windows
Advanced tab -> Environment Variables
Java environment variable configuration - Windows
So when we WIndows system installation is complete, we can see the environment variable has been preset number. Since it is a variable, it consists of two parts: the variable name and variable. Variable names are for identification, reference and read variable values, and variable values is what we really want to save (usually a key configuration or path). The role of environmental variables that can record some we need to use the path and effect, so when using certain software will be very convenient.
The system default environment variables
Java environment variable configuration - Windows
such as picture TEMP and TMP is set in a temporary directory location, tell other software, the default storage location for temporary files, and the software also reads the value of this variable at run-time, which allows the software use become very convenient.

  • User variables: configuration only take effect for the current user
  • System variables: configuration takes effect for all users of the system

Second, the role of the Path

We note that there is an environment variable in the system variable named Path. Here we take care not to confuse the Path variable and environment variable, the relationship between them is included. Path environment variable has a special role, what role do? Focus here!
Path itself translates to mean road, path, so the first clear, Path is stored in many paths. That these paths when it will be used in it? When we use a command in the command window, they are running out in the end? It all depends on the Path variable, when we execute a command, search the path Path setting in, to see if there is a command you need to use these paths.
Therefore, the role Path is: a collection of search path set executable files (command).

  • Windows systems: different paths between a semicolon (;) separated
  • Linux systems: different paths between colon (:) spaced

Third, the configuration environment variable JDK

After reading these two concepts, we now want to start the configuration environment variable.

1. JAVA_HOME

First, you need to configure the environment variable is our own definition.

  • Variable name: JAVA_HOME
  • The value of the variable: full JDK installation path

在系统变量中点击:新建
Java environment variable configuration - Windows
这样做的好处有两个:

  • 当我们以后的JDK版本更换了,或者改变了存放目录,只需要在这个变量中进行修改,而不需要在所有用到JDK路径的地方都去修改一遍
  • 其他需要用到JDK环境来运行的软件,首先会读取JAVA_HOME(这也是一个约定俗成的变量名车)的值,来找到JDK的所在路径

基于以上两点原因,请务必要这样配置!

2. Path

我们已经将JDK的值记录在了JAVA_HOME中,接下来就是想办法引用它,这个时候我们要借助几个符号。

  • Windows:%变量名称%,例:%JAVA_HOME%
  • Linux:$变量名称,例:$JAVA_HOME

选中系统变量中的Path -> 点击编辑
Java environment variable configuration - Windows
在窗口中添加如下内容:%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin;
注意路径的前后之间都要有英文的分号隔开
如果打开之后显示如下界面,请再次点击:编辑文本
Java environment variable configuration - Windows
或者点击新建后,分别添加以下两条路径,此时不再需要分号

  • %JAVA_HOME%\bin
  • %JAVA_HOME%\jre\bin

3. CLASSPATH

首先我们还是来科普一下,ClassPath变量有什么用,我们到底用不用配置它。

  1. 如果你配置环境变量的作用仅仅是为了让Eclipse能够正常运行,那么你只需要将JAVA_HOME配置正确,一切就以大功告成,在编译器中可以正常的执行编译运行等操作。
  2. 如果你需要在命令窗口中体验一下Java程序编译运行的全过程,那么你需要将Path以及CLASSPATH变量都配置好。

So, the role of this variable CLASSPATH actually help jvm find class files need to perform and library needs. In general, we will enter into the directory where the source (.java files), execute javac compiler command, then the class file (Java byte code file) will naturally generate in the current directory, so we first want to configure is the current directory, yes, that is the current directory, use a period (.) to represent, so that you can directly represent the position of your current location.
The other is the required library files, both files lib files in the JDK installation directory folder directly to configure.

  • Variable name: CLASSPATH (case No, but must be on the letter, recommended all uppercase)
  • Value of the variable:;.% JAVA_HOME% \ lib \ tools.jar;% JAVA_HOME% \ lib \ dt.jar

Click in the system variables: New
Java environment variable configuration - Windows

IV Notes

1. posture will do

After configuration is complete, we test method is to enter java and javac two commands in the command window. But this time the two operations must be done (very important):

  • The configuration interface environment variable to save completely closed
  • Re-open a command window

So you just configured environment variables to take effect, successful interface shown in Figure:

  • java:

Java environment variable configuration - Windows

  • javac:

Java environment variable configuration - Windows

Guess you like

Origin blog.51cto.com/10984944/2452102