Why to set the Java environment variable (Windows)

Source: Internet
Author: User

In the process of learning Java, the concepts of multiple environment variables (environment variable), such as path, are involved. Correctly configuring these environment variables is a prerequisite for the smooth learning and development. And often appear the problem is: Some learners can follow the instructions to correctly configure the steps, but a long time to forget, there are problems also can not help. The reason is that they do not understand the concepts, but do not know why. The following content is to help you know why.

first, the basic principle

Environment variables (environment variables) are typically used in the operating system to specify the operating system environment of some parameters, such as: temporary folder location and System folder location.

An environment variable is an object with a specific name in the operating system that contains the information that one or more applications will use. For example, the PATH environment variable in Windows and DOS operating system, when the system is required to run a program without telling it the full path of the program, in addition to the current directory in the search for this program, but also in path specified in the path to find. The user can run the process better by setting the environment variables.

Java Beginners, you must learn to configure the PATH environment variable, otherwise you can not compile, run Java program. So why configure the PATH environment variable? What does the PATH environment variable do? We start with a common problem.

When we install Java, if the PATH environment variable is not properly configured, the following error message appears when you run the command java-version (show Java Version) command in the Command Line window (Start-to-run, input cmd, carriage return):

This error message means that the "Java" command you requested to run I do not know, I do not understand what is a thing. So what exactly is this "java" command sacred? In the JDK installation directory (my is D:\Java\jdk1.6.0_24), there is a bin directory. Open this directory, you will find a large number of executables, suffixes named EXE files. One of the executable files is java.exe. Now switch the current directory to this bin directory under the Command Line window (shift+ right).

Under this directory, run the dir command to display all files and directories in the current directory. From the display results, we can clearly see the "Java.exe" this file. As shown in the following:

In fact, we execute the "java" command, which is to run the "Java.exe" executable (we run other commands, which are essentially executing a program file). Now let's try the java-version command again. In the Command Line window, type java-version, enter, as shown in the results:

It seems that this execution has been successful. Execute Java-? To view the Help information for this command. You can see that it has many options, and-version is just one of them. Another way is to write out the complete (absolute) path to these executables (for example: D:\Java\jdk1.8.0_31\bin\java-version), which can be executed in any directory. Readers can try it on their own.

Similar to the Java command, the Javac command can now be executed in the bin directory, but only the full path can be written to other directories. In the process of learning Java, we often need to use the Javac and Java two commands to compile, run our Java program. Do we have to switch to the bin directory or write a long list of absolute paths each time we run these two commands? Is there a way that we can run these commands with ease in any directory?

With this in hand, we add the full path of the bin directory to the path. Under the command-line window, you can use the SET command to accomplish this type of task. Running set directly displays the values of all current environment variables in the system and runs set/?, which displays help information about this command.

The setting environment variable value is formatted with the SET command: Set environment variable NAME = environment variable value. We can use the command set Path=d:\java\jdk1.8.0_31\bin to add a directory of program files such as Java to the PATH environment variable (Windows environment variable names are case insensitive, unlike Unix). However, this causes the value of path to be "D:\Java\jdk1.8.0_31\bin" only, and it is pre-programmed to be overwritten with values that are used by other programs. Therefore, we should append the value "D:\Java\jdk1.6.0_24\bin" to the path. To do this, we can use the following command:

Set path=%path%;D: \java\jdk1.8.0_31\bin

Place path between the two percent signs, referring to the original value of path is taken out. The semicolon that follows indicates separating the different path values, followed by the values we want to add. Note that use this command in the English IME state. Now let's go to any directory to execute the java-version command and try it. The results are as follows:

Visible run is completely successful. However, this environment variable set using the SET command is only valid for the current command-line window. Once you close this window and run another command-line window again, the PATH environment variable is the original value. Therefore, we must modify the PATH environment variable under Windows. The steps are as follows:

Right-click the desktop My Computer icon, properties, advanced environment variable, opens the dialog box for setting environment variables. The above section is set up for a user. Windows is designed to use a single machine for multiple users, one account per person. The variable set for an account is valid only for that user. The following section sets the system variables, which are valid for each user in the system. Now the general computer has only one user, namely administrator. So you only need to set this account. Locate the PATH environment variable and click Edit. Enter the semicolon for the English input method after the existing value in the Variable Value box, and then add the value you want to add (here is D:\Java\jdk1.8.0_31\bin). Determined all the way, ok!

Now open a command-line window and enter the Echo%path% command to see the value of path (or use the path command directly).

Now we can see that the bin directory has been added to path, and then running the java-version command in any directory can be successful. As shown in the following:

In fact, we implement other commands that are the principle. For example, when you display the value of path, you will find that the first value is C:\WINDOWS\system32. Open this directory under Windows and you will see many executables whose names are consistent with the names of the commands we use. That is, we execute these commands, and the system actually finds the corresponding executable file based on the value of path, and then runs the results of those files. such as Attrib.exe file

It corresponds to the attrib command, which is the command that displays/sets the properties of the file, as shown in:

Now let's clear the value of path by command set path= (equal to nothing on the right side of the equals sign or whatever), and then run the attrib command to try it out. As shown in the following:

As you can see, the exact same command, which is now impossible to execute, is an error.

The following is the command set Path=c:\windows\system32 (not case-sensitive under Windows, so Windows and Windows are correct), add the path of the Attrib.exe file to the path, and then run the test. As shown in the following:

It is clear that it has been able to run correctly now. Another common example is the Format.com (. com file is another executable file), but this command is used for formatting, so be careful when experimenting, and do not format a disk because of errors. Also, as described above, when the PATH environment variable is not properly configured, the absolute path (full path) of these files is written out and can be executed correctly. The reader can experiment on its own (in this statement, due to the prior warning, therefore, all due to misoperation caused by the loss of data, no responsibility!) )。

ii. Internal commands, external commands, and batch files

We will now start a command-line window, clear the value of path as described above, and then run the Dir, CD, and other commands. To our surprise, they are still able to execute correctly. So, what's the reason?

Let's take a look at the information that was reported incorrectly, both of which refer to the concept of "internal command" and "External command". So what are internal commands and external commands?

Internal commands and external commands are DOS (disk operating system, early Microsoft command-line based operating system) the concept of the era, Baidu Encyclopedia interpretation is: The internal command is the command_com with each boot loaded and resident memory, and the external command is a separate executable file. Roughly speaking, the so-called internal command is the most core and most used command. In order to improve the speed of response, these commands are loaded into memory as soon as the system is started, so they can be executed quickly and directly, while external commands are not preloaded into memory due to the relatively small number of uses, and when the user is in use, then to the hard disk (C:\Windows\System32) to find the appropriate executable file, It is then loaded into memory execution. such as Dir, CD, etc. are internal commands, and such as attrib, format, and so on are external commands. Although the age of DOS has long been history, some operations must be done in command-line mode, especially for professionals. Therefore, the Windows product always retains the command-line mode of this tool.

Another concept is the batch file (suffix. bat, derived from batch of English word batches), which is another file that can be executed. In short, the batch file contains many DOS commands. When the file executes, the commands are executed in one line. Not necessarily sequential execution, like the general programming language, it also has its own process control. Batch file creation is simple: Create a text file with any text editor, such as Notepad for Windows, and then change the suffix name to. bat. Create a batch file, or you can open it with a text editor and view its source code.

iii. settings for Catalina_home environment variables (for TOMCAT6/7)

Tomcat is a free open source servlet/jsp container that is popular with Java beginners. This requires setting the CATALINA_HOME environment variable, which is the Tomcat installation directory (mine is D:\TOMCAT7), or the following error message will appear:

The meaning of this sentence is that the CATALINA_HOME environment variable is not set correctly, and this variable is necessary to run Tomcat. And when you ignore this message, the command-line window switches the current directory to the bin directory under the Tomcat installation directory, and then executes the batch file Startup.bat to start Tomcat, and the discovery can also succeed. Don't you need to catalina_home the environment variable now?

In fact, in Startup.bat, the first thing to tell is whether Catalina_home is empty. If it is empty, the current directory is set to the value of Catalina_home. Then find out if there is a bin directory under the directory indicated by Catalina_home, and whether there is a batch file called Catalina.bat in this bin directory (this file is responsible for starting Tomcat). If not, the parent directory of the current directory is set to the value of Catalina_home, and the above lookup is repeated. If it still fails, report the error shown in Figure 10. Therefore, when you run the Startup.bat file in the bin directory under the Tomcat installation directory, it automatically sets the value of Catalina_home correctly in the second way described above. Because the Catalina.bat file is in the bin directory under the Tomcat installation directory.

Although it works correctly in this case, it is recommended that you set the CATALINA_HOME environment variable correctly. Unlike path, the path system itself, and catalina_home needs your own "new", in the Environment Variables window point "new".

In order to be able to run a batch file in the Bin directory under the Tomcat installation directory under any directory, such as Startup.bat, you can add this directory to the PATH environment variable (mine is D:\tomcat7\bin). For added flexibility, you can add a%catalina_home%\bin at the end of the PATH environment variable (don't forget to separate the English semicolon from the previous value). Place the catalina_home between two percent signs, indicating the value that references the environment variable. This way, when you replace the Tomcat installation directory, you only need to modify the value of the catalina_home without modifying the value of path because it automatically calculates the new correct value.

Article source: artistic meaning

article reference: NashMaster2011

Why to set the Java environment variable (Windows)

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.