The role of classpath, Path, Java_home is set in JAVA

Source: Internet
Author: User

Thinking in Java explanation of the role of Classpath

The Java interpreter's working procedure is as follows:

First, it finds the environment variable classpath (which is set by the operating system when the Java or Java-capable tool-such as a browser-is installed in the machine). CLASSPATH contains one or more directories, which are used as a special "root" to expand the search for. class files. Starting with that root, the interpreter looks for the package name and replaces each dot (period) with a slash to generate a path name starting from the Classpath root (so the packages Foo.bar.baz becomes foo\bar\baz or Foo/bar/baz , whether the forward slash or the backslash is determined by the operating system). They are then connected together to become entries (portals) within the classpath. When you search for a. class file later, you can start looking for the name that corresponds to the class name you are about to create. In addition, it will search for some standard directories-these directories are related to where the Java interpreter resides.

To further understand this problem, take my own domain name as an example, it is bruceeckel.com. After turning it upside down, Com.bruceeckel created a unique global name for my class (Com,edu,org,net and other extensions were previously capitalized in the Java package, but this has changed since Java 1.2. The entire package name is now lowercase). Since I decided to create a library called Util, I can split it further, so the last package name is as follows: Packages Com.bruceeckel.util;


The package name can now be used as a "namespace" for the following two files:
: Vector.java
Creating a Package
Package com.bruceeckel.util;
public class Vector {
Public Vector () {
System.out.println (
"Com.bruceeckel.util.Vector");
}
}
: List.java
Creating a Package
Package com.bruceeckel.util;
public class List {
Public List () {
System.out.println (
"Com.bruceeckel.util.List");
}
}
These two files are placed in a subdirectory of my own system:
C:\DOC\JavaT\com\bruceeckel\util
If you go back through it, you will find the package name Com.bruceeckel.util, but what is the first part of the path? This is made by classpath
Environment variables are determined. On my machine, it is:
classpath=.;D: \ JAVA \lib; C:\DOC\JavaT

As you can see, CLASSPATH can contain a large number of alternate search paths. However, there is a problem when using jar files: The jar must be
The name of the file is placed in the classpath, not just the path where it resides. So for a jar file named Grape.jar, our
Class paths need to include:
classpath=.;D: \ JAVA \lib; C:\flavors\grape.jar

Personal understanding: Actually from the above can be seen, if you use Notepad to write Java Program (under Window), and cmd command window

Enter the Javac,java command if your code uses a different jar or your own write class, but your main program is not under a package, then you will be in the classpath to set the jar you want to use or write the path of the class, Let Java in the compiler can find what you want to use, as shown in the example above. When we use some tools, such as Eclipse, we can add some jar packages, and then search the path of the tools to help us do, so do not have to manually add, for example, Eclipse Project has a special file to record Classpath



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

cl asspath? What is the role of it?
It is an environment variable of the Javac compiler.
Its role is related to import, the package keyword.
When you write down Improt java.util.*, when the compiler faces the import keyword, you know you want to introduce java.util the class in the package, but how does the compiler know where you put the package? So you first have to tell the compiler where the package is located, and how do you tell it? is to set classpath:) If java.util this package in the C:\jdk\ directory, you have to set c:\jdk\ this path to Classpath! When the compiler faces the import java.util.* this statement, it first looks for the directory specified by Classpath and checks to see if the subdirectory java\util exists, and then finds the compiled file (. class file) that matches the name. If you do not find it will be an error! The
     classpath is a bit like the settings of the include path in the C\c++ compiler oh, 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 the Include path to view the file!
      When you develop a package yourself and then want to use the class in the package, naturally, you also have to set the directory where the package resides in Classpath! The
     classpath setting is a tricky thing for beginners in Java. So Sun made JAVA2 's jdk a little bit smarter. You will find that after you install, you can still compile the basic Java program and execute it even if you have not set classpath at all.

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

1. Path environment variable. The function is to specify the command search path, execute the command below the command line, such as Javac compiling the Java program, it will look in the path specified by the paths variable to see if the corresponding command program can be found. We need to add the bin directory in the JDK installation directory to the existing path variable, and the bin directory contains the frequently used executables such as Javac/java/javadoc wait, and after setting the path variable, you can execute the Javac/java tool in any directory.

2. CLASSPATH environment variables. The role is to specify the class search path, to use the already written classes, the premise of course is to find them, the JVM is through Classpath to find the class of the. class file. We need to set the Dt.jar and Tools.jar in the Lib subdirectory of the JDK installation directory to classpath, of course, the current directory "." must also be added to the variable.

Javac-c Path (You can specify a class file to store the directory)

JAVA-CP Path (You can specify the class directory to execute)

3. Java_home environment variables. It points to the JDK's installation directory, and software such as Eclipse/netbeans/tomcat finds and uses the installed JDK by searching for java_home variables.

On the Windows desktop, right-click "My Computer", "Properties", "Advanced", "Environment variables", in "system variables" we can see the values of the system's various environment variables. Double-click a variable name to modify the value of the variable, using ";" between the variable values. Separated. We can also "create new" variables that were not previously available. There are 3 environment variables associated with the JDK, "java_home", "path", "Classpath". The "path" variable already exists in my system, can add the new value directly (other variable values do not move, prevent other programs to run the exception), the other two variables need to be new.

"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 the JDK, "%java_home%\bin;%java_home%\jre\bin;"

"Classpath", set the path of each class in Java, ".; %java_home%\lib;%java_home%\lib\tools.jar ". The previous "." No less, it represents the working path we have built for our Java class, and the other is the standard class library path that comes with the JDK.

After setting the environment variable, press OK to exit. Press "Win" + "R" key into the "Run" window, run "cmd" into the DOS window, enter "Javac" after entering, if there is help information showing Java, the environment variable setting is successful.

Classpath= ".; %java_home%\lib;%java_home%\lib\tools.jar "

Java_home = "C:\Program files\java\jdk1.5.0"

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

The role of classpath, Path, Java_home is set 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.