Java FAQ _01 Basic concept (009) _classpath What is

Source: Internet
Author: User

Click to enter _ more _java thousand ask

1. What is Classpath?

Classpath is the collection that the Java interpreter uses to specify the path to the search package, which is how the Java interpreter works:

    1. The Java interpreter finds the environment variable classpath (which is set by the operating system environment variable when Java or Java-capable tools, such as browsers, are installed into the machine).

    2. Classpath contains one or more directories, which are used as a special "root path" from which 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 each 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).

    3. These path names are concatenated 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.

    4. Of course, the Java interpreter will also search for some standard directories (the directory where the Java interpreter resides).

Through the above explanations, we can understand the role of classpath. In simple terms, tell Java where we use the class:

When you write down Improt java.util. , when the interpreter faces the Import keyword, it knows that you are introducing java.util to the class in the package. But how does the interpreter know where you put the package? So you first have to tell the compiler where the package is located, that is, by setting 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 import java.util. When this statement is made, it finds the directory specified by Classpath and looks at the subdirectory java\util exists, and then finds the compiled file (. class file) that matches the name.

2. How to set Classpath

Different environment settings Classpath also vary:

    1. Set classpath through the system environment variables in the Windows system, which is for Java applications running on this system. Create a new "Classpath=;%java_home%\lib\tools.jar;%java_home%\lib\dt.jar;" with the system variable, "System properties, advanced environment variables", where%java_ Home% refers to a directory that references Java_home environment variables, typically specified as JDK: "Java_home=d:\program files\java\jdk1.6.0_10"

    2. By setting the project's classpath through Eclipse, the project right->properties->java build Path, which takes effect only for the specified project (the. classpath file is generated in the project).

Java FAQ _01 Basic concept (009) _classpath What is

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.