In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how to use classpath in Java. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Summary
Classpath is the path where the Java runtime environment searches for classes and other resource files, such as resources such as jar\ zip. You can set classpath through the-classpath parameter after the JDK tool (such as javac command, java command) (you can view the parameter options that can be entered by entering sdktool under the dos window with java environment, as shown in the following figure)
Or by setting the CLASSPATH environment variable. This-classpath option is a preference because you can set it separately for each application without affecting other applications without requiring other applications to modify their values.
Here are examples of these two ways:
(1) set for each application through the command line of jdk
This setting is mainly set for each application and does not affect the classpath of other programs and the classpath of the environment variable, it only takes effect in the current window.
C: > sdkTool-classpath classpath2; classpath3...
(2) set the classpath environment variable, which is a global setting and is effective for all applications.
C: > set CLASSPATH=classpath2; classpath3...
SdkTool
A command line tool such as java, javac,javadoc, or apt. For a list, see the JDK tool.
Classpath2; classpath3
Classpath of .jar, .zip or .class files. Each classpath should end with a file name or directory. For different situations, the format of the classpath is as follows:
(1) for .jar or .zip files that contain .class files, classpath ends with the name of the .zip or .jar file.
The following is an example. The author packages a project into a jar package and specifies a class file under this jar package to run. The results are as follows:
The following is the catalog of the project, in which other jar packages are referenced in the project. When packaging, the author uses maven's assembly plug-in to package into a fat jar (to learn more about the specific packaging method, check out these two blog posts:
[Java] package Jar package and execute it with script
[Maven] Maven Series (2)-- packaged with assembly plug-ins
See how the value of classpath is specified below.
(2) for .class files in an unnamed package, classpath ends with a directory containing .class files. For example, an a.class file is under d:path2\ path3, but the a.class does not have package, then if you want jvm to search the class file, then when you specify classpath, you can use d:path2\ path3, because the default path of classpath is the current path, so when in use, if you open the dos window under the current path, the classpath command defaults, then the effect is equivalent to specifying the current path as classpath. An example is as follows:
Test.java
Public class test {public static void main (String [] args) {System.out.println ("hello world!");}}
There is no package for this class.
The following is the running result:
The program runs successfully
(3) for .class files in named packages, the classpath ends with the directory that contains the "root" package (the first package in the full package name).
Examples are as follows:
Equals.java is under the package named string
Package string;public class Equals {public static void main (String [] args) {String a=new String ("test"); String b=new String ("test"); System.out.println (astatb);}}
In this case, the way to specify classpath is as follows:
From the above commands, you can see that the correct command line format is as follows:
Java-classpath package_rootpath package_name.class_name
Multiple path entries are separated by semicolons. Using the set command, spaces around the equal sign (=) can be ignored.
The default classpath is the current directory. Setting the CLASSPATH variable or using the-classpath command-line option overrides the default value, so if you want to include the current directory in the search path, you must include "." in the new setting (for a description of the path, please refer to another blog post by the author).
Classpath that is neither a directory nor an archive (.zip or .jar file) and * is ignored.
Description
The classpath tells JDK tools and applications where to find third-party and user-defined classes-that is, classes that are not part of the Java extension or the Java platform. The classpath needs to find any classes you compile with the javac compiler-the default value is the current directory to find them easily.
JDK,JVM and other JDK tools find classes by sequentially searching for Java platform (bootstrap) classes, any extended classes and classpath. (for more information about search strategies, see how to find classes. Most application class libraries want to take advantage of the extension mechanism You only need to set up classpath, and when the class you want to load satisfies the following conditions (a) classes that are not in the current directory or any of its subdirectories, (b) are not in the location specified by the extension mechanism.
If you are upgrading from an older version of JDK, the startup settings may contain settings that CLASSPATH no longer needs. You should remove any settings that are not application-specific, such as classes.zip. Some third-party applications that use Java virtual machines may modify your CLASSPATH environment variables to include the libraries they use. This setting can remain the same.
When you call JVM or other JDK tools or use environment variables, you can use the JDK tool'- classpath option to change classpath. Using this option is better than setting environment variables because you can set them separately for each application without affecting other applications without requiring other applications to modify their values.
Classes can be stored in directories (folders) or archived files (that is, those jar and zip packages). The Java platform class is stored in rt.jar. For more details on archiving and information about how classpath works, see the classpath and package name near the end of this document.
Important: some older versions of JDK sofware contain a / classes entry in the default classpath. This directory exists for use by JDK software and should not be used for application classes. Application classes should be placed outside the JDK level directory. In this way, installing a new JDK does not force you to reinstall the application class. For compatibility with older versions, applications that use the / classes directory as the class library will run in the current version, but there is no guarantee that they will run in future versions.
Use the-classpath option of the JDK tool
The JDK tools java,jdb,javac and javah have a-classpath option to replace the path specified by the classpath environment variable. This is the recommended option to change the classpath setting because each application can have the required classpath without interfering with any other application.
The runtime tool java also has a-cp option. This option is an abbreviation for-classpath.
For very special cases, both java and javac have options that allow you to change the path used to find your own class library. However, the vast majority of users will never need to use these options.
Use CLASSPATH environment variabl
In general, you should use the-classpath command line option as described in the previous section. This section describes how to set the CLASSPATH environment variable or clear the settings left over from previous installations.
Set up CLASSPATH
Group commands modified in CLASSPATH environment variables. The format is:
Set CLASSPATH = path2; path3...
The path should begin with the letter of the specified drive, such as C:\. This way, if you happen to switch to another drive, you will still find the class file. (if the set path is D:\, then jvm will look for the class under disk D instead of disk C.)
Clear CLASSPATH
If your CLASSPATH environment variable is set to an incorrect value, or if your startup file or script is set to an incorrect path, you can unset it by CLASSPATH using the following command:
C: > set CLASSPATH =
This command cancels CLASSPATH only in the current command prompt window. You should also delete or modify startup settings to ensure that CLASSPATH has the correct settings in future sessions.
Change startup settings
If the CLASSPATH variable is set when the system boots, the location to look for depends on your operating system:
For Windows 95 and 98, check the set command for autoexec.bat.
Others (Windows NT,Windows 2000, … The CLASSPATH environment variable can be set using the system utility in the control panel
Understanding classpath wildcards
The classpath entry can contain the base name wildcard character *, which is considered equivalent to a list of .jar or .jar of all files specified in the extension directory. For example, the classpath entry foo/* refers to all the JAR files in the foo directory. A simple classpath consisting of * refers to a list of all jar files in the current directory.
The classpath writing of the wildcard * does not apply to class files (meaning that the wildcard * applies to jar rather than class). To match the class and the JAR file foo in a single directory, use foo;foo/* or foo/*;foo. The order of the former determines that the classes and resources in the foo file directory are loaded first, jar is loaded later, and vice versa.
Subdirectory is not a recursive search. For example, foo/* is only looking for the JAR file foo, not subdirectories such as foo/bar,foo/baz.
The order of the JAR files in the enumerated directory in the extended classpath is not specified (The order in which the JAR files in a directory are enumerated in the expanded class path is not specified, this sentence does not understand, readers can reply if they have any understanding, thank you! And may vary from platform to platform, even on the same machine A well-built application should not depend on any particular order. If a specific order is required, JAR files can be explicitly enumerated in the classpath.
The process of extending wildcards is before calling the program's main method, during the class loading process. Each input classpath that contains wildcards will be enumerated to all JAR file paths in this directory. For example, if the directory foo contains a.jarjag.jar and c.jar, then extend the classpath foo/* to foo/a.jar;foo/b.jar;foo/c.jar, and the string will be the value java.class.path of the system property.
The CLASSPATH environment variable is no different from the command-line option of-classpath (or-cp). That is, wildcards apply equally in all cases. However, the classpath wildcard does not meet the requirements in the Class-Path jar-manifest header file (this sentence is not very well understood, I hope the reader can give me some advice! ).
Understand the classpath and package name
The Java class is organized into packages that are mapped to directories in the file system. However, unlike the file system, whenever you specify a package name, you can specify the entire package name instead of part of it. For example, the package name java.awt.Button is always specified as java.awt.
For example, suppose you want the Java runtime to find the Cool.class class under the utility.myapp package. If the path to this directory is C:\ java\ MyClasses\ utility\ myapp, then the set classpath should contain C:\ java\ MyClasses.
To run the application, use the following JVM command:
C: > java-classpath C:\ java\ MyClasses utility.myapp.Cool
When the application is running, JVM uses this classpath setting to find any other classes (including the Cool class) defined in the utility.myapp package.
Notice that the entire package name is specified in the command. It is impossible to succeed if you set classpath to C:\ java\ MyClasses\ utility and use the command java myapp.Cool. JVM could not find this class.
You may want to know what defines the package name of a class. The answer is that the package name is part of the class and cannot be modified, except for recompiling the class.
Note: an interesting consequence of the package specification mechanism is that files that are part of the same package may actually exist in different directories. The package name for each class will be the same, but the path to each file can start from a different directory in the classpath.
Folders and archived files
When a class is stored in a directory (folder), such as c:\ java\ MyClasses\ utility\ myapp, the classpath entry points to the directory that contains the first element of the package name. This is the directory one level above package (in this case, classpath is C:\ java\ MyClasses, because the package name is utility.myapp. )
However, when the class is stored in an archive file (.zip or .jar file), the classpath entry is the path to the .zip or .jar file and contains the .zip or .jar file. For example, to use the class library in the .jar file, the command would look like this:
C: > java-classpath C:\ java\ MyClasses\ myclasses.jar utility.myapp.Cool
Multiple assignment
To find classes in C:\ java\ MyClasses directory and classes in C:\ java\ OtherClasses directory, set the classpath to:
C: > java-classpath C:\ java\ MyClasses; C:\ java\ OtherClasses...
Notice that the two paths are separated by semicolons.
Specify order
The order of multiple classpath is important. The Java interpreter will find the classes in the directory in the order in which they appear in the classpath variable. In the above example, the Java interpreter will first look for classes in the C:\ java\ MyClasses directory. The interpreter looks in the C:\ java\ OtherClasses directory only if a class with the correct name cannot be found in that directory.
Thank you for reading! This is the end of the article on "how to use classpath in Java". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.