Java classpath Detail

Source: Internet
Author: User
Explanations for the role of Classpath in the thinking in Java are as follows:
   The Java interpreter's working procedures are as follows:
First, it finds the environment variable classpath (set up by the operating system when installing Java or Java-capable tools such as browsers) into the machine. CLASSPATH contains one or more directories, which are used as a special "root" to expand the search for. class files from here. Starting with that root, the interpreter looks for the package name and replaces each point number (period) with a slash to generate a pathname starting from the Classpath root (so package Foo.bar.baz becomes foo\bar\baz or Foo/bar/baz , whether a forward slash or a backslash is determined by the operating system). They are then connected together to become entries (portals) within the classpath. When you search for the. class file later, you can start looking for the name of the class name that you want to create. In addition, it searches for standard directories that are related to where the Java interpreter resides.
What is Classpath? What is its role.
Classpath is an environment variable for the Javac compiler.
Its role is to specify the class search path, which is related to the import, package keyword.
      When you write down the Improt java.util.*, the compiler faces the Import keyword Know that you want to introduce the class in Java.util this package, but how does the compiler know where you put the package? So you first have to tell the compiler where the package is, and how to tell it. is to set up 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 looks for the directory specified by Classpath, examines the existence of the subdirectory java\util, and then finds the compiled file (. class file) that matches the name.
If you don't find it, you will get an error.     classpath A bit like the set of include paths in the C\c++ compiler oh, yes. When the C\c++ compiler encounters a statement such as include <iostream>, how it works. Well, the truth is all the same.
Search for include path, view file.
     When you develop a package yourself and then want to use the class in this package, naturally you have to set the package directory to classpath. The setting of classpath is a tricky thing for the beginners of Java. So Sun makes JAVA2 's jdk a little smarter. You will find that after you install it, you can still compile the basic Java program, even without setting the classpath, and execute the 
  as you can see, CLASSPATH can contain a large number of alternate search paths. However, when using a jar file, be aware of the problem: you must place the name of the jar file in the classpath, not just the path where it resides. So for a jar file named Grape.jar, our classpath needs to include:
classpath=.;D: \ JAVA \lib; C:\flavors\grape.jar  Personal Understanding: In fact, from the above can be seen, if you use Notepad to write Java Program (under Window), and cmd Command window to enter the Javac,java command, If your code uses other jars or your own written class, but with your main program is not under a package, then you have to set in the classpath you want to use the jar or write your own path to the class, so that 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 something to the jar package and then search the path for tools to help us do it, so we don't have to add it manually. So in the actual development of eclipse, there is no use of the system configuration Classpath, but it will generate a. classpath file, only use cmd Javac compile with the system classpath (which can also be changed, see below).
Classpath=c:\test means to c:\test a folder when executing a command to find the class file that needs to be executed
before java5 the path cannot specify the current path unless this setting: Classpath=.; C:\Test, it means that first in the current directory to find the class file, if not found to C:\Test to find; (when a variable has multiple values, multiple values are separated by semicolons in English), set to look up from the current directory (java1.5 default): Set classpath=.
CLASSPATH environment variables. The role is to specify the class search path, to use the classes that have been written, and, of course, to find them, and the JVM is to find the class's. class file by Classpath. We need to set the 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 the variable.
Javac-c Path (You can specify a class file storage directory)
JAVA-CP Path (You can specify the class directory to execute)
   (1). When to use-classpath: When the class you are compiling or executing references other classes, but the. class file of the referenced class is not in the current directory, you need to introduce the class
   (2) through the-classpath. When to specify a path: When the directory you are compiling is not in the same directory as the directory where you execute the javac command, you need to specify the path of the source file (Classpath is used to specify the. class path, not the path of the. java file)
eg
   For example, one application used the two classes of java.lang.String and Com.shinechina.MyClass (which I wrote).

version 1:
Package Com.shinechina;
public class MyClass
{public
   static void main (String args[])
   {
      string str = ' version 1:hello, welcome to Chin aasp Java technology ";
      System.out.println (str);
   
Version 2:
Package Com.shinechina;
public class myclass{public
   static void main (string args[]) {
      string str = ' version 2:hello,welcome to chinaasp Java technology Operation ";
      System.out.println (str);
   }
   Now I'm going to run it. Java Com.shinechina.MyClass Now the problem is how Java knows where to find it, what exactly should he do to assume version one in C:\MYLIB\COM\SHINECHINA\MYCLASS.J Ava assumes version II in D:\mylib\com\shinechina\MyClass.java Note: Do not forget to compile into class file Yo, java files are not executable CD C:\mylib\com\shinechina JAV AC *.java CD D:\mylib\com\shinechina Javac *.java has: He first went to the machine classpath to find, assuming that the Windows system C:\MYLIB;D:\MYLIB;D:\JDK1.
2.2\lib\rt.jar; or Unix system/home/local/:/usr/local/:/usr/local/jdk1.2.2/lib/rt.jar. Well, c:\mylib he found the COM directory, there is a chance, and down to find a Shinechina directory, the directory found Myclass.class, Ok is he, as for d:\mylib my version of the 2 MyClass he will ignore, As for the string is to be rt.jar in the compressed package. The result of his execution is the version 1:hello, welcome to chinaasp Java Technology in turn D disk Path in C: Before the result of execution is version 2:hello, WELC ome to chinaasp Java technology, and so on, only the first one found on the classpath will execute, you have to control him. The one that must be done: Java-classpath d:\mylib;%classpath% com.shinechina.My

 The result of class execution is the version 2:hello, welcome to Chinaasp Java Technology development environment, where there is always a place to specify the classpath.

 Note that the development environment is often not to read the system path, which is running outside the east of the development of the environment on the dead vegetables. For example JBuilder, jdevoloper from menu Project->project PROPERTIES...-&GT;PAths tag->add...-> add a defined library point OK or new-> enter name and Classpath->ok. This dongdong is added to the classpath (runtime can see in Message view) and then for example, VisualAge for Java, the class you want to run in the dot-> the right mouse click to select the attribute-> the classpath tag
       1,-> edit-> Select All, good, all the classes in the development environment can be found.

2, immediately calculate (only select the class you need, if useful class.forname, such as through the program explicitly loaded class words, need their own from the editor of the manual home into the development environment every ability to help you calculate out, if the memory is no problem, directly edit-> select more convenient yo)

3, if your class does not want to introduce the development environment, add additional path to the point, edit (below), I do not like this, do not detail the

master words, directly with the JDK, then have trouble you either add to the system Classpath (Unix under $ CLASSPATH), or compile the runtime to explicitly indicate CLASSPATH

below describes a beginner's most easily made mistake.

The example described above

assumes version one in C:\mylib\com\shinechina\ Myclass.java

compile C:\mylib\com\shinechina\MyClass.class

How to specify classpath?

common Problems

1, c:\mylib\com\ Shinechina\

2, c:\mylib\com\

correct

1, c:\mylib\

2, C:\mylib
The importance of Classpath
     When you develop a project with eclipse, you need to configure CLASSPATH for each project, specifically how to do it with just the right mouse button build path on the project. Then step by step, after you've finished, open your project's workpace, open your project file, There is a. classpath file, you open the file and you will see the results of your previous build path in Eclipse. For example, I added a user Library that I had previously configured in my project, named Mysqljdbcdriver, and added a <classpathentry kind= "con" in my project's. classpath file. Path= "Org.eclipse.jdt.user_library/mysqljdbcdriver"/>
I think the project is independent of the computer environment variable CLASSPATH, each project needs its specific classpath, So in eclipse development, each project will have a. classpath file (PS: Why each project needs its own classpath. Because each project needs to introduce its own specific class library, and the computer environment variable classpath is generic, you can not make all of your projects are only using the computer environment variable CLASSPATH set of these libraries bar.
I think. Classpath independent of the computer Classpath also has an important reason: when you finally make the jar package available to the user, users want to run this jar, you can not ask the user's computer classpath have your current reference package path, because the user's system is clean, so we need to put my We need to refer to the package build to the. classpath
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.