ClassPath Describes to the Blog from Dazern

xiaoxiao2021-03-06  24

Set the class path

structure

Class paths can be set by using the -classpath option (preferred method) or setting the ClassPath environment variable to 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 is set to set the class path:

The path to the .zip or .jar file containing the .class file, the path ends with the .zip or .jar file name.

For the .class file in the unnamed package, the path is ending 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 a semicolon to separate multiple items. When using the set command, you need to omit the space (=) on both sides of the equal number. Among them, JDKTool can be Java, Javac, Javadoc, and more. See JDK Development Tools for detailed lists.

Description

The classpath tells the Java application where to find a third party and custom class - ie not a Java extension or a class part of the Java platform. In JDK 1.2, JVM and other JDK tools look for classes by searching for platform libraries, library expansion and classpaths in turn (see how to find classes).

Most applications class libraries will take advantage of the expansion mechanism. Therefore, you need to set the class path only if you want to load a class library (a) is not in the current directory or its branch package and (b) is not specified by the expanded mechanism.

If the user is upgraded from the old version of the JDK, the start settings may include the ClassPath settings that are no longer needed. At this time, any non-application-specific settings should be deleted. Some third-party applications using Java virtual machines may modify the classpath environment variable to include the class libraries they use. This setting can be retained.

Class paths (for example: java -classpath ...) can be changed using the -classpath option using Java tools when calling the JVM or other JDK tool. This is the preferred method of changing class paths. You can also change the class path by using the ClassPath environment variable.

Note: JDK 1.2 The default 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 ".".

Classs can be stored in a directory (folder) or archive (such as classes.zip or classes.jar). See the last understanding path and package name of this document on the working principle of the detailed information and classpaths of the archive file.

Important: The old version of the JDK also includes / classes items in the default path. This directory is for JDK only and is not used by application classes. The application class should be placed outside the JDK outside. This way, do not need to reinstall the application library when installing new JDK. In order to compatibility with older versions, use / classes directory as a class library can still run in the current version, but it is not guaranteed that they can run in later versions.

Use the -classpath option using the Java tool

Java Tools Java, JDB, Javac, and Javah have -classpath options, which will replace the default class path or classpaths specified by the ClassPath environment variable. This is a recommended method for changing the class path, as each application can have the class path required and will not interfere with other applications.

Runtime Tools Java and JDB also have -CP options. This option is the abbreviation of -classpath.

For very special situations, Java and Javac have a switch that allows you to use the path to find their own class libraries. However, most users have never used these switches.

Use ClassPath environment variables

As mentioned 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 left under the 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: / ... In this way, classes can still be found when changing to different drives (for example, if the path item is started in / ..., and the desired class will be on D: instead of C: Drive ).

Clear ClassPath

If the ClassPath environment variable is set to an incorrect value, or the startup file or scriptor is set up, the ClassPath can be cleared by using the following command:

C:> set classpath =

This command only clears the ClassPath of the current session. To make sure you have the correct ClassPath settings in later sessions, you should delete or modify the startup settings.

Change startup settings

If you set a ClassPath variable at the time of the system, the location of the lookup looks on the operating system used:

Operating system method

Windows 98 and Windows 95 check the set command in the autoexec.bat file.

Windows NT launches the Control Panel, select "System", click the Environment tab, and check the ClassPath variable in the User Variables section.

Understand class path and package

The Java class is organized into a package, and these packages are mapped to the directory in the file system. However, it is different from the file system that the full package name should be specified whenever you specify the package name - never specify only part of it. For example, Java.awt.Button's name should always be specified as java.aw.

For example, assume that you want a Java runtime environment to find classes called Cool.class in the package uTility.myApp. If the path of the directory is C: / java / myclasses / utility / myApp, the class path should be set to include C: / Java / MyClasses.

To run the app, you can use the JVM command:

C:> java -classpath c: / java / myclasses utility.myapp.cool

When the application runs, JVM uses 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, setting class paths containing C: / Java / MyClasses / Utility and uses command java myapp.cool because it can't find this class.

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

Note: An interesting result of the package specification mechanism is that multiple files belonging to the same package can actually exist in different directories. For each class, the package name is the same, but the path to each file can start from different directories in the classpath.

Folders and archive files

When 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 (here is C: / Java / MyClasses because The package name is utility.myapp). But when class is stored in an archive (.zip or .jar file), the class path item is the path to the .zip or .jar file. For example, to use the class library located in the .jar file, the command should be similar to the following form:

Java-ClassPath C: /java/myclasses/myclasses.jar utility.myapp.cool

Multiple designation

To find class files in directory C: / Java / MyClasses and C: / Java / OtherClasses, set the class path to:

Java-Classpath C: / Java / MyClasses; C: / Java / OtherClasses ...

Note that the two paths are separated by a semicolon.

Specified order

The order of specifying multiple type path items is quite important. The Java interpreter will find classes in each directory in the order in the class path variable according to the directory. In the above example, the Java interpreter first looks forward to the required classes in the directory C: / Java / MyClasses. The interpreter can be found in the C: / Java / OtherClasses directory when it does not find this class in this directory.

转载请注明原文地址:https://www.9cbs.com/read-65133.html

New Post(0)