On the configuration of path and CLASSPATH environment variables in Java

Source: Internet
Author: User

One, the compilation and running of Java programs must go through two steps:


1. Compile the source file into byte code;

2, explain the execution platform-independent bytecode program.


Second, the question


The above two steps need to use Javac and Java two commands (Javac command to compile the Java source files generated class file, Java command to the generated class file to run), If we enter Javac and Java commands directly in the CMD command Line window, you may see the following error:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/57/5E/wKiom1SY4q_ReDEGAABiJpEuxqw472.jpg "title=" "width = "385" height= "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:385px;height:70px; "alt=" wkiom1sy4q_ Redegaabijpeuxqw472.jpg "/>


Also, when we move the class file, the following error occurs when executing the Java command:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/57/5B/wKioL1SY5LuTyxbmAABQmWCoAno074.jpg "title=" "width = "532" height= "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:532px;height:55px; "alt=" Wkiol1sy5lutyxbmaabqmwcoano074.jpg "/>


Third, analysis of the problem


We already have the JDK installed on the computer, and the JDK's installation path also contains the Java and Javac commands, Why this error occurs .

error One : The reason is that the command line or console did not find the Javac and Java commands.

When we enter the Javac or Java command, the command line first looks in the current directory (the example above: E:/demo/java) to see if there is a javac.exe or java.exe this file, and if not, find it in the system's PATH environment variable.


Error Two : This is because the command line did not find the class file.

When we execute the Java command, if the CLASSPATH environment variable is not configured, then the Java virtual Opportunity looks for the class file (i.e. Java Class) under the current directory, and if it does not, it will go wrong; if we configure the CLASSPATH environment variable, The Java virtual Opportunity is located directly to the path specified by the environment variable.

Iv. Solutions

Error One :

According to the problem analysis, there are two solutions to error one: One is to find the directory where the files are located, and the second is to configure the PATH environment variable directly in the system.

Obviously the first method is too cumbersome, and the second method is once and for all, and it allows us to execute the program in any directory. How do you configure the PATH environment variable?


PATH environment variable


The Windows operating system looks for commands based on the PATH environment variable. The value of the PATH environment variable is a series of paths, such as the path variable in My computer:

D:\tellhow\java\jdk1.5.0_09\bin;%catalina_home%\lib;%catalina_home%\bin; C:\windows\system32;d:\app\administrator\product\11.2.0\dbhome_1\bin;d:\program Files\MySQL\MySQL Server 5.5\bin

Windows will look at the command in this series of paths, and if you can find the command, the command will be executable, otherwise it would be an error.

(Path and path are no different in Windows operating systems, it is not case-sensitive)

Therefore, we just need to find the Javac and Java command storage path, such as:D:\TELLHOW\java\jdk1.5.0_09\bin, add into the PATH environment variable can be, notice the ";" semicolon separated.

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/57/5D/wKioL1SZBrWwoSRGAAK6HEnasVY743.jpg "title=" "width = "572" height= "426" border= "0" hspace= "0" vspace= "0" style= "width:572px;height:426px;" alt= " Wkiol1szbrwwosrgaak6henasvy743.jpg "/>

We can enter javac directly in the command line, press "enter" key to verify that the configuration is correct, if you see the above information (this is actually the use of javac.exe, we will use a few in the future), it means that the configuration is correct (this is not related to the path "E:/demo/java").

One might ask, "E:/demo/java" is Who's address? Obviously this is the path to the file Test.java that the program Javac.exe runs.


Error Two :


Almost every book introducing the introduction to Java will introduce the configuration of CLASSPATH environment variables, there are many on the Internet, but the role of CLASSPATH environment variables is often vague.

is the class library for the runtime environment, mainly swing's package ) and Tools.jar (

However, the previous version does not have such a feature, so we will see that the network will have such a configuration:.; C:\ProgramFiles\Java\jdk1.7.0_05\lib\dt.jar; C:\Program Files\java\jdk1.7.0_05\lib\tools.jar;

The previous point in this configuration represents the current path, plus the Dt.jar and Tools.jar two files are added to the CLASSPATH environment variable.

I don't know if you've found any problems, A 1.7 version is still using the 1.4 and the previous CLASSPATH environment variable configuration method, which seems to illustrate a problem, the rapid development of the IT industry, the knowledge is constantly updated, online actually have a lot of knowledge is lagging, so we must pay attention to such a problem, not too pragmatic.

So what is the role of CLASSPATH environment variables?

If we have a lot of class files and put them in different paths, when we need to execute the files under the specified path, then the CLASSPATH environment variable is useful.


V. Extension of the problem


I wonder if you ever thought that in some special cases we might use temporary environment variable configuration. What would you do if we borrowed someone else's computer but couldn't change someone else's computer settings? I think the best way is a mobile flash drive plus temporary environment variable configuration.

Temporary environment variable configuration you can use DOS commands, enter set on the cmd command line, and you'll see all the environment variables on your computer:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/57/64/wKiom1SZGeWjBXS3AAOXL6wd63c808.jpg "title=" "width = "636" height= "493" border= "0" hspace= "0" vspace= "0" style= "width:636px;height:493px;" alt= " Wkiom1szgewjbxs3aaoxl6wd63c808.jpg "/>

By entering set path, all variables of path can be displayed, 650) this.width=650; "Src=" http://s3.51cto.com/wyfs02/M02/57/65/ Wkiom1szgswtsc8waaec8ghjiwm811.jpg "title=" "width=" 642 "height=" "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width : 642px;height:92px; "alt=" wkiom1szgswtsc8waaec8ghjiwm811.jpg "/>


Setting a temporary PATH environment variable is just a matter of typing:

Set Path=f:/demo/java, which has no effect on the configuration in the computer system.

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/57/65/wKiom1SZG9fBZs2qAACGRNTRCuo063.jpg "title=" "width = "504" height= "108" border= "0" hspace= "0" vspace= "0" style= "width:504px;height:108px;" alt= " Wkiom1szg9fbzs2qaacgrntrcuo063.jpg "/>

Another input is:

Set path=f:/demo/java;%path%

This means that a path is added to the original setting, which has no effect on the configuration in the computer system.

A summary is:

Set: Used to view all the environment variables of the machine;

Set Variable name: View a specific environment variable;

Set Variable name =: empty an environment variable;

Set Variable name = specific value: Defines a specific reference for the specified variable;

Set Variable name = specific value;%path%: Adds a new value based on the original environment variable.

Note: This configuration is only valid for the current DOS window, and the configuration disappears after the window is closed.


Similarly, the temporary configuration of the CLASSPATH environment variable is the same, not much verbose here.




This article is from the "Yunfei Dance" blog, please make sure to keep this source http://wuyunncu.blog.51cto.com/5978596/1593900

On the configuration of path and CLASSPATH environment variables in Java

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.