[Java] classpath of Java

Source: Internet
Author: User

Http://www.cnblogs.com/wxf0701/archive/2008/08/23/1274579.html

1. Importance of Class search paths
It is important for all Java developers to understand the Class search path. However, the wide use of IDE masks this technology, which leads to a general lack of knowledge about it, even including many old birds. This problem is especially serious when developing distributed applications, because the system environment during application running may be different from that during development.
This article describes in detail how the Java compiler and JVM use the Class search path to locate some Java classes that are referenced by other code. Here is a very simple example -- the same package
The two classes in -- are described in detail. We will compile these two classes in different ways. Depending on the classpath settings, compilation may succeed or fail.
To clearly illustrate this problem, we will only use the command line tool for compilation. Interactive Development tools have their own classpath methods, which vary with products.
As a result, the Java compiler located the required classes during compilation or by JVM at runtime. There is no essential difference between the two methods. However, the compiler can compile the required classes from the source code, but not the JVM. In the following example, we use a compiler, but the implementation at runtime is similar.
2. Instance
In this example, there are two very small classes: COM. web_tomorrow.cptest1 and COM. web_tomorrow.cptest2, as shown below:

PackageCom. web_tomorrow;

Public ClassCptest1 {

Public Static VoidMain (string [] ARGs ){

System.Out. Println ("Run cptest1.main ()");

}

}

PackageCom. web_tomorrow;

Public ClassCptest2 {

Public Static VoidMain (string [] ARGs ){

System.Out. Println ("Run cptest2.main ()");

Cptest1 cpt1 =NewCptest1 ();

}

}

One of the most basic rules for Java code organization is 'package name =
Directory name' ("package name =
Directory name "). We will create the corresponding directory structure for these two classes, which are in the package com. web_tomorrow, So we create the Directory
COM/web_tomorrow to store the source code:

[Root] com
Web_tomorrow
Cptest1.java
Cptest2.java
In this article, I use the '[root]' symbol to indicate any directory that stores the above structure. That is to say, the location of the root directory is determined by you. Of course, this varies with how these files are installed.
3. Basic Principles
Let's try to use the command line tool javac to compile cptest1.java. To completely disable the Class search path (in case the existing settings affect this example), we add the option '-classpath ""' on javac ""'.
As the first test, we first switch to the directory where cptest1.java is located, and try to compile with javac and file name.
CD [root]/COM/web_tomorrow
Javac-classpath "" cptest1.java
The operation is successful because the compiler can find cptest1.java
(It is in the current working directory), and cptest1 does not reference any other classes. The output file cptest1.class is in the same directory of cptest1.java, because
You didn't give the compiler any information for other processing. It has been good till now. Now let's try cptest2 in the same way. Still in the web_tomorrow directory
Run the following command:
Javac-classpath "" cptest2.java
Although the directory is just the directory, cptest1 and cptest2 are in the same package, this time it failed.
The error message may be as follows:
Cptest2.java: 7: cannot resolve symbol
Symbol: Class cptest1
Location: Class com. web_tomorrow.cptest2
Cptest1 cpt1 = new cptest1 (); ^
One of the differences between this and the previous success is that cptest2 has a reference to cptest1:
Cptest1 cpt1 = new cptest1 ();
What happened this time? When the compiler encounters a reference to cp1test, it considers that the cp1test class is in the same package as the currently compiled cp2test class. This assumption is positive
Yes, so the compiler looks for com. web_tomorrow.cp1test, but there is no place to look for it, because we have already put the Class search path
It is explicitly specified as "" (that is, null ).
You may think that this problem can be solved by telling the compiler to look for it in the current directory. In UNIX and Windows systems, the standard symbol "Current directory" is a dot (.), that is, the command:
Javac-classpath "." cptest2.java
Fail
Againt! The problem is that although cptest1.java is in the current directory, its implementation class is not cptest1,
Com. web_tomorrow.cptest1, the compiler will find the directory COM/web_tomorrow in the current directory. That is, it is in the directory
[Root]/COM/web_tomorrow to find a Java source file or class file. They do not actually exist, so an error occurs.
To make the compiler work normally, we cannot point the Class search path to a directory containing cptest1, but follow the rules of 'package name = directory name' in Java standards, specify the compiler to find the root directory of cptest1. This way, although not very nice:
Javac-classpath ".../" cptest2.java
Let's take a look at this example (still in the same directory) before thinking about how to make it difficult)
Javac-classpath "" cptest1.java cptest2.java
Although classpath is empty, it can work this time. This is because the Java compiler looks for references in all source code explicitly listed in the command line. To compile multiple classes in the same directory, there is a very simple method:
Javac-classpath "" *. Java
'*. Java' is extended to the list of all. java files in the current directory. This shows why multiple files are successfully compiled at a time, but the same files fail to be compiled at a time.
There is a more convenient way to compile cptest2:
CD [root]
Javac-classpath "." com/web_tomorrow/cptest2.java
This time, we specified the complete path for cptest2.java and used '.' In the-classpath option '.'. In addition, we didn't tell the compiler to look for a file in the current directory, but asked it to start searching from the current directory.
Because the class we are looking for is com. web_tomorrow.cptest1, the compiler will look for it under./COM/web_tomorrow (that is, the COM/web_tomorrow subdirectory under the current directory. This is the correct position of cptest1.java.
In fact, although I only specify cptest2 in the command line, cptest1 will be compiled at the same time. The compiler finds the. Java file in the correct position, but it cannot determine whether the. Java file contains the correct class, so it compiles the file. Note that:
CD [root]
Javac-classpath "." com/web_tomorrow/cptest1.java
Cptest2.java will not be compiled because the compiler does not need to know about cptest2 when compiling cptest1.
Separation of class files and. java files
All successful examples so far put the output. Class file in the same position as the. Java file. This is a simple mode and is widely used. But many developers like
Separate the source file from the generated file, so it must tell the compiler to maintain different directories for the. Class file. Next, let's take a look at how this affects the Class search path.
First, we will delete all. class files generated in the previous examples. We also need a new directory to store the generated. Class file. The operation procedure in the command line mode is as follows:
CD [root]
Rm COM/web_tomorrow/*. Class
Mkdir classes
If you use Windows, do not forget to replace '/' with '\'. The current directory structure is as follows:

[Root] com
Web_tomorrow
Cptest1.java
Cptest2.java classes
Compile cptest1.java to specify the target directory of the class file (through the-D option)
CD [root]
Javac-D classes-classpath "" com/web_tomorrow/cptest1.java
But you will notice that the. Class file is not directly placed in the classes directory, but has a new directory structure:

[Root] com
Web_tomorrow
Cptest1.java
Cptest2.java
Classes com
Web_tomorrow
Cptest1.class
The compiler creates a directory structure that conforms to the package structure. This is useful and we will see it immediately. When we start to compile cptest2.java, we have two options:
Let the compiler compile cptest1 at the same time; the other is to use the-classpath option to specify the. Class file that was just compiled. This method is better, because we don't have to come back.
Compile cptest1 once:
CD [root]
Javac-D classes-classpath classes COM/web_tomorrow/cptest2.java
After completion, our directory structure is as follows:

[Root] com
Web_tomorrow
Cptest1.java
Cptest2.java
Classes
Com
Web_tomorrow
Cptest1.class
Cptest2.class
Of course, we can also compile two. java files in the same command, with the same results.
4. jar package file in classpath
The Java compiler and runtime environment can not only search for classes in independent files, but also search for classes in jar package files. A jar package file can maintain its own directory
Structure. Java adopts the same structure as normal directories. 'Directory name = package
Name. Jar itself is a directory, so when the class search path contains the jar package file, the path must reference the jar itself, rather than its directory. If you are
There is a jar myclasses. Jar under/myclasses. I need to specify: javac-classpath
/Myclasses. Jar... is not just the directory myclasses.
5. search directories for multiple classes
In the preceding example, we only search javac in one directory. In practice, your class search path contains many directories and jar files. The-classpath option of javac allows multiple locations to be specified, but note that its syntax is slightly different in UNIX and Windows systems.
In UNIX systems:
Javac-classpath dir1: dir2: dir3...
Windows systems:
Javac-classpath dir1; dir2; dir3...
This is because Windows uses the colon (:) as part of the file name, so it cannot be used as the file name separator. Of course, the directory separators are different: Unix forward slash (/) and Windows backslash (\).
6. System classpath
In addition to specifying the Class search path in the javac command, we can also use the 'system' class path. If no specific path is specified in the command, both the Java compiler and JVM use the system path. In UNIX and Windows systems, the system path is set through environment variables.

For example, in a Linux System Using bash shell:
Classpath =/myclasses. jar; export classpath
In Windows:
Set classpath = c: \ myclasses. Jar
This is a good way to temporarily change the system variable classpath, but if you want to keep these changes, you need to make some modifications to your system. For example, in Linux
. In Windows 2000/NT, you can use the 'Control panel 'to modify the settings.
If you have a lot of jar files that you can use at any time, it is especially important to set the system variable classpath. For example, if I use sun's J2EE reference to develop an EJB application,
All EJB-related classes are published in a jar package named "J2EE. Jar". I hope this jar is always in the class search path. In addition, many people want
The search path always contains the current directory. So there is such a line in my. bashrc file:
Classpath =/usr/J2EE/J2EE. jar:.; export classpath
'.' Indicates the 'current directory'

It is easy to see that the-classpath option in the command line overwrites the default system class path; it
It is not an extension but an overwrite. Therefore, if we want to include both the default system path and some additional system paths, what should we do? We can simply use the-classpath option to list both the default value and
Additional parts. A better way is to reference the system variable classpath. Of course, this syntax is different for Windows and UNIX systems.

On Unix:
Javac-classpath $ classpath: dir1: dir2...
$ Classpath is extended to the system variable classpath. On Windows:
Javac-classpath %; dir1: dir2...

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.