Functions of classpath, path, and java_home (non-original)

Source: Internet
Author: User

What is classpath? What is its role? It is an environment variable of the javac compiler.

Its role is related to the import and package keywords.

When you write improt Java. util. * When the compiler faces the import keyword, it will know that you want to introduce Java. util is a class in the package. But how does the compiler know where you put the package? So you must first tell the compiler where the package is located. How can you tell it? Set classpath. If the java. util package is in the C: \ JDK \ directory, you must set the c: \ JDK \ path to classpath! When the compiler faces the import Java. util. * In this statement, it first looks for the directory specified by classpath, checks whether the subdirectory Java \ util exists, and then finds the compiled file (. class file ). If not found, an error is returned!

Classpath is a bit like the include path settings in the C \ c ++ compiler, isn't it? When the c \ c ++ compiler encounters a statement such as include <iostream>, how does it work? Oh, the truth is almost the same! Search include path and View File!

When you develop a package, you want to use the classes in the package. Naturally, you have to set the directory of the package to classpath!

Classpath settings are tricky for beginners of Java. So Sun makes JDK of Java2 smarter. After installation, you will find that, even if classpath is not set at all, you can still compile and execute basic Java programs.

........................................ ........................................ ........................................ .............................

1. PATH environment variable. The function is to specify the command search path. When you execute a command below the command line, such as javac, when compiling a Java program, it will go to the path specified by the PATH variable to check whether the corresponding command program can be found. We need to add the bin directory under the JDK installation directory to the existing PATH variable. The bin directory contains frequently used executable files such as javac/Java/javadoc waiting, after setting the PATH variable, You can execute tools such as javac and Java in any directory.

2. classpath environment variable. The function is to specify the Class search path and use the classes that have already been compiled. The premise is that you can find them, and JVM searches for classes through classpath. We need to set DT. jar and tools. jar in the Lib subdirectory under the JDK installation directory to classpath. Of course, the current directory "." must also be added to this variable.

3. java_home environment variable. It points to the JDK installation directory. Eclipse, netbeans, tomcat, and other software find and use the installed JDK by searching the java_home variable.

On the Windows desktop, right-click "my computer"-> "properties"-> "advanced"-> "environment variable ", in "system variables", we can see the values of various system environment variables. You can double-click a variable name to modify the variable value. The variable values are separated by commas. We can also "CREATE" variables that are not previously available. JDK-related environment variables include "java_home", "path", and "classpath ". In my system, the "path" variable already exists. You can directly add a new value (other variable values should not be dynamic to prevent other program running exceptions). The other two variables need to be created.

"Java_home", set the JDK installation path, such as "E: \ Java \ jdk1.5", hereinafter referred to as "% java_home % ".

"Path", set the path of each program in JDK, "% java_home % \ bin; % java_home % \ JRE \ bin ;"

"Classpath", set the paths of various Java classes, ".; % java_home % \ Lib \ tools. Jar ". The preceding "." is indispensable. It represents the working path we have set up for our own Java class, and the other is the standard library path that comes with JDK.

After setting environment variables, press "OK" to exit. Press "win" + "R" to enter the "run" window, Run "cmd" to enter the DOS window, enter "javac", and press Enter. If Java help information is displayed, environment variables are successfully set.

Classpath = ".; % java_home % \ Lib \ tools. Jar"

Java_home = "C: \ Program Files \ Java \ jdk1.5.0"

Path = "% java_home % \ bin; % java_home % \ JRE \ bin"

 

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.