Classpath (for those who can't find the north)

Source: Internet
Author: User
Tags command line contains file system include variables switches version variable
To set the class path
Structure
You can set the Classpath by using the-classpath option (preferred method) or by setting the CLASSPATH environment variable for the JDK tool.
C:> Jdktool-classpath path1;path2 ...
C:> Set Classpath=path1;path2 ...
Each path ends with a file name or directory, which depends on what the classpath is set to:
For a. zip or. jar file that contains a. class file, the path ends with a. zip or. jar file name.
For the. class file in the unnamed package, the path ends with the directory that contains the. class file.
For the. class file in the named package, the path ends with the directory containing the "root" package (the first package in the full package name).
Separate multiple items with semicolons. When you use the SET command, you omit the space (=) on either side of the equal sign. Where jdktool can be Java, Javac, Javadoc, and so on. For a detailed list, see JDK development tools.
Description
The classpath tells the Java application where to look for Third-party and custom classes-classes that are not part of a Java extension or a Java platform. In JDK 1.2, the JVM and other JDK tools find classes by searching for platform libraries, library extensions, and class paths in turn (for more information about search policies, see how to find classes).
The class libraries of most applications take full advantage of the extension mechanism. Therefore, you only need to set the classpath if you want to load a class library (a) that is not located in the current directory or its branch package and (b) is not located at a location specified by the extension mechanism.

If the user is upgraded from an older version of the JDK, the startup settings may include CLASSPATH settings that are no longer needed. You should remove any non-application-specific settings at this time. Some third-party applications that use Java virtual machines may modify the CLASSPATH environment variables to include the class libraries they use. This setting can be preserved.

You can change the classpath (for example, Java-classpath ...) by using the Java tool's-classpath option when invoking the JVM or other JDK tools. )。 This is the preferred method of changing the classpath. You can also change the classpath by using the CLASSPATH environment variable.

Note: The JDK 1.2 default class path is the current directory. Setting the CLASSPATH variable or using the-classpath command line switch overrides the default value, so if you want to include the current directory in the search path, you must include the "." in the new setting.
A class can be stored in a directory (folder) or archive (such as Classes.zip or Classes.jar). For more information about the archive and how the Classpath works, see the final understanding of the Classpath and package name in this document.

Important: The JDK old version also includes the <jdk-dir>/classes entry in the default classpath. This directory is intended for JDK use only and is not intended for application classes. Application classes should be placed in directories outside the JDK. This eliminates the need to reinstall the application library when installing a new JDK. For compatibility with older versions, applications that use the <jdk-dir>/classes directory as class libraries can still run in the current version, but they are not guaranteed to run in future releases.
-classpath options for using Java tools
Java tools Java, Jdb, Javac, and Javah have the-classpath option, which replaces the default classpath or the classpath specified by the CLASSPATH environment variable when the tool is run. This is a recommended way to change the classpath settings because each application can have the classpath it needs without interfering with other applications.
Runtime tools Java and Jdb also have-CP options. This option is an abbreviation for-classpath.

For very special situations, both Java and Javac have switches that allow them to change the paths they use to find their own class libraries. However, most users never use these switches.

Using CLASSPATH Environment variables
As described in the previous section, the general user will want to use the-classpath command-line option. This section describes how to set the CLASSPATH environment variable or clear the settings that were previously installed.
Set CLASSPATH
At the DOS prompt, you can modify the CLASSPATH environment variable with the SET command. The format is:
Set Classpath=path1;path2 ...
The path should begin with the letter of the specified drive, such as C:\ .... This way, you can still find a class when you accidentally switch to a different drive (for example, if the path item is \ ...). And is currently on the drive D: the desired class will be found on the D: instead of the C: drive.
Clear CLASSPATH
If the CLASSPATH environment variable is set to an incorrect value, or if the startup file or script program sets an incorrect path, you can clear the CLASSPATH by using the following command:
C:> Set Classpath=
This command clears only the CLASSPATH of the current session. To ensure that you have the correct CLASSPATH settings in a future session, you should remove or modify the startup settings.
Change startup settings
If you set the CLASSPATH variable at system startup, the location of the lookup depends on the operating system you are using:
Operating system methods
Windows 98 and
Windows 95 checks the set command in the Autoexec.bat file.
Windows NT starts Control Panel, selects system, clicks the Environment tab, and in the User Variables section, checks the CLASSPATH variable.

Understanding class paths and package names
Java classes are organized into packages, and these packages are mapped to directories in the file system. However, unlike the file system, whenever you specify a package name, you should specify the full package name-never specify only part of it. For example, Java.awt.Button's package name should always be specified as java.awt.
For example, suppose you want the Java runtime environment to look for a class named Cool.class in the package Utility.myapp. If the path to the directory is C:\java\MyClasses\utility\myapp, you should set the Classpath to include C:\java\MyClasses.

To run the application, you can use the following JVM commands:

C:> Java-classpath C:\java\MyClasses Utility.myapp.Cool
When the application runs, the JVM uses the Classpath settings to find any other classes defined in the Utility.myapp package.
Note: The full package name should be specified in the command. For example, the set Classpath contains C:\java\MyClasses\utility and uses the command Java MyApp. Cool is impossible because this class is not found.

(You might want to know what defines the package name for the class.) The answer is: The package name is part of the class and cannot be modified unless the class is recompiled. )

Note: One interesting consequence of the package specification mechanism is that multiple files belonging to the same package can actually exist in different directories. The package names are the same for each class, but the path of each file can be started from a different directory in the classpath.
Folders and archive files
When a class is stored in a directory (folder), such as C:\java\MyClasses\utility\myapp, the class path entry points to the directory that contains the first element of the package name (here is C:\java\MyClasses, because the package name is Utility.myapp).
However, when a class is stored in an archive file (. zip or. jar file), the Classpath entry is the path to the. zip or. jar file. For example, to use a class library that is located in a. jar file, the command should resemble the following:

Java-classpath C:\java\MyClasses\myclasses.jar Utility.myapp.Cool
Multiple designations
To find a class file in the directory C:\java\MyClasses and C:\java\OtherClasses, you can set the Classpath to:
Java-classpath C:\java\MyClasses; C:\java\OtherClasses ...
Note that the two paths are separated by semicolons.
Specify order
It is very important to specify the order of multiple classpath items. The Java interpreter looks for the classes in each directory in the order in which the directories are in the CLASSPATH variable. In the example above, the Java interpreter first finds the required class in the directory C:\java\MyClasses. The interpreter can look in the C:\java\OtherClasses directory only if it does not find the class in the directory.

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.