Set the class path

Source: Internet
Author: User
Set the class path

Structure
You can use the-classpath option (preferred method) or set the classpath environment variable for the JDK tool to set the class path.
C:> jdktool-classpath path1; path2...
C:> set classpath = path1; path2...
Each path ends with a file name or directory. The file name or directory depends on the class path:
For. Zip or. Jar files that contain. class files, the path ends with. Zip or. Jar files.
For the. Class file in the unnamed package, the path ends with the directory containing 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.
Use semicolons to separate multiple projects. When using the set command, spaces (=) on both sides of the equal sign must be omitted ). Among them, jdktool can be Java, javac, javadoc, and so on. For a detailed list, see JDK development tools.
Description
The class path tells the Java application where to find third-party and custom classes-that is, classes that are not Java extensions or are part of the Java platform. In JDK 1.2, JVM and other JDK tools search for classes by platform libraries, library extensions, and class paths (for more information about search policies, see how to find classes ).
Most application class libraries make full use of the extension mechanism. Therefore, you must set a class path only when you want to load a class library (a) that is not in the current directory or its branch package and (B) that is not in the position specified by the extension mechanism.

If you upgraded from the old JDK version, the startup settings may include the classpath settings that are no longer needed. In this case, you should delete any non-application-specific settings. 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 retained.

You can use the-classpath option of the Java tool to change the class path (for example, Java-classpath...) when calling JVM or other JDK tools ...). This is the preferred method for changing the class path. You can also use the classpath environment variable to change the class path.

Note: JDK 1.2 uses the current directory by default. Setting the classpath variable or using the-classpath command line will overwrite the default value. To include the current directory in the search path, you must include "." In the new settings.
Classes can be stored in directories (folders) or archive files (such as classes.zip or classes. Jar. For more information about the archive file and how it works, see the class path and package name at the end of this document.

Important: The old JDK version also includes <JDK-dir>/classes in the default class path. This directory is only for JDK and not for application classes. The application class should be placed in a directory outside JDK. In this way, you do not need to reinstall the application library when installing the new JDK. For compatibility with the old version, the application that uses the <JDK-dir>/classes directory as the class library can still run in the current version, however, it cannot be ensured that they will run in future versions.
Use the-classpath option of the Java Tool
Java, jdb, javac, and javah tools have the-classpath option. When the tool is running, it replaces the default class path or the class path specified by the classpath environment variable. This is a recommended method for changing the class path settings, because each application can have the class path it needs without interfering with other applications.
The runtime tools Java and jdb also have the-CP option. This option is short for-classpath.

In special cases, both Java and javac have a switch so that you can change the path they use to find their own class libraries. However, most users never use these switches.

Use the classpath environment variable

As described in the previous section, you generally want to use the-classpath command line option. This section describes how to set the classpath environment variables or clear the settings left behind by the previous installation.

Set classpath
At the DOS prompt, run the SET command to modify the classpath environment variable. The format is:
Set classpath = path1; path2...
The path should start with a letter of the specified drive, such as C :/.... In this way, you can still find the class when you accidentally switch to a different drive (for example, if the path item is /... and is currently on drive D:, the required class will be found on drive D: instead of drive C ).

Clear classpath
If the classpath environment variable is set to an incorrect value, or the Startup File or script program is set to an incorrect path, you can run the following command to clear the classpath:
C:> set classpath =
This command only clears the classpath of the current session. To ensure correct classpath settings are available in future sessions, delete or modify the startup settings.
Change startup settings
If you set the classpath variable when the system starts, the location of the variable depends on the operating system used:
Operating System Method
Windows 98 and
In Windows 95, check the SET command in the autoexec. BAT file.
Start "Control Panel" for Windows NT, select "system", click the "Environment" tab, and check the classpath variable in the "user variables" section.

Understanding the class path and package name

Java classes are organized into packages, which are mapped to directories in the file system. However, unlike the file system, the full package name should be specified whenever the package name is specified-never specify only one part of the package. For example, the Java. AWT. Button package name should always be specified as Java. AWT.
For example, suppose you want the Java Runtime Environment to find the class named Cool. Class in the utility. MyApp package. If the directory path is C:/Java/myclasses/utility/MyApp, set the class path to include C:/Java/myclasses.

To run the application, run the following JVM command:

C:> JAVA-classpath C:/Java/myclasses utility. MyApp. Cool
When the application is running, JVM uses the class path 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, it is impossible to set the class path to contain C:/Java/myclasses/utility and use the command Java MyApp. Cool, because this class cannot be found.

(You may want to know the package name of the definition class. The answer is: the package name is part of the class and cannot be modified unless the class is re-compiled .)

Note: An interesting result of the packet specification mechanism is that multiple files belonging to the same package can actually be stored in different directories. For each class, the package name is the same, but the path of each file can start from different directories in the class path.
Folder and archive file
When a class is stored in a directory (folder), such as C:/Java/myclasses/utility/MyApp, the class path item points to the directory containing the first element of the package name (here it is C:/Java/myclasses, because the package name is utility. myApp ).
But when the class is stored in the archive file (.zip or. jar file), the class path item is the path of the. Zip or. jar file. For example, to use a class library located in the. jar file, the command should be in the following format:

Java-classpath C:/Java/myclasses. Jar utility. MyApp. Cool
Specify multiple
To search for class files in the directories C:/Java/myclasses and C:/Java/otherclasses, you can set the class path:
Java-classpath C:/Java/myclasses; C:/Java/otherclasses...
Note that the two paths are separated by semicolons.

Specify order
It is important to specify the order of multiple class path items. The Java interpreter searches for classes in each directory in the class PATH variable order. In the preceding example, the Java interpreter first finds the required class in the directory c:/Java/myclasses. The interpreter can be searched in the C:/Java/otherclasses directory only when the class is not found in this 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.