The concept of multiple environment variables environment variable is involved in learning JAVA, such as PATH. Correct configuration of these environment variables is a prerequisite for smooth learning and development. The common problem is that some learners can correctly configure the settings step by step as prompted. However, after a long period of time, they will not be able to solve the problem. The reason is that you do not understand these concepts, but do not know why. The following content helps everyone know why.
I. Basic Principles
First, check the PATH environment variable.
JAVA beginners must learn to configure PATH environment variables. Otherwise, they cannot compile or run JAVA programs. So why should we configure the PATH environment variable? What is the PATH environment variable? Let's start with a common problem.
After we install JAVA, if the PATH environment variable is not correctly configured, start in the command line window-& gt; Run-& gt; Enter cmd and press Enter) run the java-version command to display the JAVA version.) the following error message is displayed:
650) this. width = 650; "width =" 588 "height =" 85 "border =" 0 "title =" 01 "style =" border-top-0px; display: block; border-left-width: 0px; float: none; border-bottom-width: 0px; margin-left: auto; margin-right: auto; border-right-width: 0px "alt =" 01 "src =" http://www.bkjia.com/uploads/allimg/131228/1340033140-0.jpg "/>
Figure 1
This error message indicates that I don't know the "java" command you want to run, and I don't know what it is. So what is the significance of this "java" command? In the JDK installation directory, my directory is D: \ Java \ jdk1.6.0 _ 24) and there is a bin directory. Open this directory and you will find a large number of executable files suffixed with exe. By default, Windows hides the file suffix. To display the extension name, click "Tools"> "Folder Options"> "View"> "Advanced Settings"> "Advanced Settings"> ">" in the top menu bar of the current window; clear the hooks before "Hide extensions of known file types"-& gt; OK ). One executable file is java.exe. Now switch the current directory to this bin directory in the command line window. Run the following command:
650) this. width = 650; "width =" 505 "height =" 125 "border =" 0 "title =" 02 "style =" border-top-0px; display: block; border-left-width: 0px; float: none; border-bottom-width: 0px; margin-left: auto; margin-right: auto; border-right-width: 0px "alt =" 02 "src =" http://www.bkjia.com/uploads/allimg/131228/13400345S-1.jpg "/> Figure 2
In this directory, run the dir command to display all files and directories in the current directory. From the result, we can clearly see the example java.exe file. As shown in:
650) this. width = 650; "width =" 555 "height =" 411 "border =" 0 "title =" 03 "style =" border-top-0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px "alt =" 03 "src =" http://www.bkjia.com/uploads/allimg/131228/1340034920-2.jpg "/>
Figure 3
In the example, we execute the example java.exe command to run the example java.exe executable file. We run other commands to execute a program file ). Now let's run the "java-version" command again. In the command line window, type java-version and press Enter. The result is as follows:
650) this. width = 650; "width =" 587 "height =" 101 "border =" 0 "title =" 04 "style =" border-top-0px; display: block; border-left-width: 0px; float: none; border-bottom-width: 0px; margin-left: auto; margin-right: auto; border-right-width: 0px "alt =" 04 "src =" http://www.bkjia.com/uploads/allimg/131228/1340034G4-3.jpg "/> figure 4
It seems that this execution is successful. The command result shows that the current JAVA version is "1.6.0 _ 24 ". Run java -? You can view the help information of this command. We can see that it has many options, and-version is only one of them. Another method is to write out the complete and absolute executable files.) path: d: \ Java \ jdk1.6.0 _ 24 \ bin \ java-version ), you can run it in any directory. You can try it on your own.
Similar to the java command, the javac command can be executed in the bin directory, but the complete path can only be written in other directories. When learning JAVA, we often need to use the javac and java commands to compile and run our JAVA program. Do we have to switch to the bin directory or write a long string of absolute paths before running these two commands? Is there a way for us to run these commands freely in any directory?
The solution lies in the PATH environment variable.
PATH indicates the PATH. The value stored in the PATH environment variable is a series of paths. Different paths are separated by semicolons. When the system executes USER commands, if the user does not provide an absolute path, first find the corresponding executable file in the current directory, another executable file in the batch processing file) and so on. If no executable program file is found, search for the executable program files in the PATH. The system takes the first PATH as the standard. If none of the paths saved in the search PATH are found, an error message similar to Figure 1 is displayed.
After understanding this, we will add the full PATH of the bin directory to the PATH. In the command line window, you can use the set command to complete such tasks. Run set directly. The values of all the current environment variables of the system are displayed. Run set /?, The help information about this command is displayed. Use the set command to set the format of the environment variable value to: set environment variable name = environment variable value. Run the set path = D: \ Java \ jdk1.6.0 _ 24 \ bin command to add the directory where the java and other program files are located to the PATH environment variable. The environment variable names in Windows are case insensitive, this is different from UNIX ). However, this will make the PATH value only "D: \ Java \ jdk1.6.0 _ 24 \ bin", and the pre-set values for other programs will be overwritten. Therefore, we should append the value "D: \ Java \ jdk1.6.0 _ 24 \ bin" to the PATH. Therefore, we can use the following command:
Set path = % path %; D: \ Java \ jdk1.6.0 _ 24 \ bin
Place the PATH between two percentage signs to retrieve the original value of PATH. The semicolon (;) indicates that different path values are separated, and then the values to be added. Note: Use this command in the English input method. Now let's go to any directory and execute the java-version command. The result is shown in:
650) this. width = 650; "width =" 699 "height =" 215 "border =" 0 "title =" 05 "style =" border-top-0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px "alt =" 05 "src =" http://www.bkjia.com/uploads/allimg/131228/1340034539-4.jpg "/>
Figure 5
The operation is successful.
However, the environment variables set using the set command are only valid for the current command line window. Once this window is closed and another command line window is run again, the PATH environment variable is still the original value. Therefore, we must modify the PATH environment variable in Windows. The procedure is as follows:
Right-click "my computer" icon on the desktop-& gt; properties-& gt; advanced-& gt; environment variable. The "set environment variable" dialog box is displayed. The above section is for a user. Windows is designed to allow multiple users to use one machine and one account for each user. Variables set for an account are only valid for this user. The following section sets system variables, which are valid for every user in the system. Currently, there is only one user on the computer, that is, Administrator. Therefore, you only need to set this account. Find the PATH environment variable and click "edit ". Enter a semicolon for the English input method next to the existing value in the variable value box, and then add the value you want to add here D: \ Java \ jdk1.6.0 _ 24 \ bin ). All the way, OK!
Open a command line window, enter the echo % path % command to view the path value, or directly use the path command ). Now, we can see that the bin directory has been added to the PATH, and then run the java-version command in any directory. As shown in:
650) this. width = 650; "width =" 616 "height =" 455 "border =" 0 "title =" 06 "style =" border-top-0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px "alt =" 06 "src =" http://www.bkjia.com/uploads/allimg/131228/1340033Y9-5.jpg "/>
Figure 6
In fact, this principle is used to execute other commands. For example, when you display the PATH value, you will find that the first value is C: \ WINDOWS \ system32. Open this directory in Windows and you will see many executable files whose names are consistent with the names of Common commands. That is to say, when we execute these commands, the system actually finds the executable files based on the value of PATH and then runs the results of these files. For example, in the attrib.exe file, it corresponds to the attrib command. This is the command for displaying/setting file attributes, as shown in:
650) this. width = 650; "width =" 580 "height =" 374 "border =" 0 "title =" 07 "style =" border-top-0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px "alt =" 07 "src =" http://www.bkjia.com/uploads/allimg/131228/134003C12-6.jpg "/>
Figure 7
Now we can clear the value of path by running the set PATH = "no" command on the right side of the equal sign, or just write something), and then run the attrib command to try again. As shown in:
650) this. width = 650; "width =" 724 "height =" 142 "border =" 0 "title =" 08 "style =" border-top-0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px "alt =" 08 "src =" http://www.bkjia.com/uploads/allimg/131228/1340031a0-7.jpg "/>
Figure 8
It can be seen that the same command cannot be executed and an error is reported.
Run the set path = c: \ windows \ system32windowscommand to add the attrib.exe file PATH to the path. As shown in:
650) this. width = 650; "width =" 528 "height =" 395 "border =" 0 "title =" 09 "style =" border-top-0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px "alt =" 09 "src =" http://www.bkjia.com/uploads/allimg/131228/1340031916-8.jpg "/>
Figure 9
It is clear that the current operation is correct. Another common example is that the format.com.com file is another executable file). However, this command is used for formatting. Be careful when you experiment. Do not format a disk due to mistakes. In addition, according to the above description, when the PATH environment variable is not correctly configured, write the absolute PATH of these files to the complete PATH), you can also correctly execute. Readers can make their own experiments and declare that they are not responsible for any data loss caused by misoperations because they have been notified in advance !).
2. Internal commands, external commands, and batch files
Now we start a command line window, clear all the PATH values in the preceding method, and then run commands such as dir and cd. Unexpectedly, they can still be correctly executed. What is the reason for this?
Let's take a look at the reported errors. They all mentioned the concepts of "Internal commands" and "External commands. So what are internal and external commands?
Internal commands and external commands are concepts of the DOSdisk operating system, Microsoft's early command-line-based operating system) era. Baidu encyclopedia's explanation is: the internal command loads and resident memory with COMMAND_COM started each time, while the external command is a separate executable file. Roughly speaking, the so-called Internal commands are the most core and used commands. To speed up the response, these commands are loaded to the memory as soon as the system is started, so they can be executed quickly and directly. External commands are not pre-loaded to the memory because they are rarely used, when using the tool, go to c: \ windows \ system32 on the hard disk to find the executable file and load it To the memory for execution. For example, dir and cd are both internal commands, while attrib and format are all external commands. Although the DOS era has already become history, some operations must be completed in command line mode, especially for professionals. Therefore, Windows products keep the command line mode tool.
Another concept is that the batch processing file is suffixed with. bat, which is a batch of English words (batch). It is another executable file. To put it simply, the batch processing file contains many DOS commands. When the file is executed, these commands are executed one by one. It is not necessarily executed in sequence. Like a general programming language, it also has its own process control. Batch File Creation is simple: Use any text editor such as Windows notepad to create a text file and change the suffix to. bat. After creating a batch file, you can open it in a text editor to view its "source code ".
3. The setting of the CATALINA_HOME environment variable applies to tomcat6/7)
Tomcat is a free and open-source Servlet/JSP Container, which is favored by JAVA beginners. In this case, you need to set the CATALINA_HOME environment variable with the value of "d: \ tomcat7" in the tomcat installation directory. Otherwise, the following error message will appear:
650) this. width = 650; "width =" 592 "height =" 50 "border =" 0 "title =" 10 "style =" border-top-0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px "alt =" 10 "src =" http://www.bkjia.com/uploads/allimg/131228/1340034M5-9.jpg "/> figure 10
This means that the CATALINA_HOME environment variable is not correctly set, and this variable is required to run Tomcat. If you ignore this information, switch the current directory to the bin directory under the tomcat installation directory in the command line window, and then execute the batch file startup. bat can also be successful when tomcat is started. Do you not need the CATALINA_HOME environment variable now?
In fact, in startup. bat, first determine whether CATALINA_HOME is empty. If it is null, set the current directory to the value of CATALINA_HOME. Check whether a bin directory exists in the directory indicated by CATALINA_HOME, and whether a batch file named catalina. bat exists in the bin directory to start tomcat ). If no, set the parent directory of the current directory to the value of CATALINA_HOME and repeat the preceding search. If the error persists, the error shown in Figure 10 is returned. Therefore, when you run startup in the bin directory under the tomcat installation directory. when the bat file is used, it automatically and correctly sets the value of CATALINA_HOME according to the second method described above, because catalina. the bat file is in the bin directory under the tomcat installation directory.
In this case, it is recommended that you set the CATALINA_HOME environment variable correctly. Different from PATH, the PATH system is based on itself, while CATALINA_HOME requires you to "CREATE" and click "CREATE" in the "environment variables" window.
To run the batch files in the bin directory under the tomcat installation directory in any directory, such as startup. bat, you can add this directory to the path environment variable. I am d: \ tomcat7 \ bin ). To improve flexibility, you can add % catalina_home % \ bin at the end of the PATH environment variable and do not forget to separate it with a semicolon ). Place catalina_home between two percentage signs to reference the value of this environment variable. In this way, after you change the tomcat installation directory, you only need to modify the value of catalina_home without modifying the value of PATH, because it can automatically calculate the correct value.
This article is from the "Xiao fan's column" blog, please be sure to keep this source http://legend2011.blog.51cto.com/3018495/553255