| Start | |||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Different ways to specify the classpath There are three ways to set the classpath. Note that the Class-Path element in MANIFEST.MF: Note that this only applies when the JAR file is executed like this: In this mode of execution, the If no classpath is specified, then the default classpath is the selected JAR file when using Related: |
|||||||||||||||
| Adding all JARs in a directory to the classpath If you want to add all the JARs in directory to the classpath, you can do this concisely using classpath wildcard syntax; for example: This tells the JVM to add all JAR and ZIP files in the Notes:
|
|||||||||||||||
| Load a resource from the classpath It can be useful to load a resource (image, text file, properties, KeyStore, ...) that is packaged inside a JAR. For this purpose, we can use the Suppose we have the following project structure : And we want to access the contents of By using the classloader, we need to specify the fully qualified path of our resource (each package). Or alternatively, we can ask the Test class object directly Using the class object, the path is relative to the class itself. Our We can, however, use absolute paths from the class object, like so : |
|||||||||||||||
| Classpath path syntax The classpath is a sequence of entries which are directory pathnames, JAR or ZIP file pathnames, or JAR / ZIP wildcard specifications. Sometimes it is necessary to embed a space in a classpath entry (The details may depend on the command shell that you use.) |
|||||||||||||||
| Dynamic Classpath Sometimes, just adding all the JARs from a folder isn't enough, for example when you have native code and need to select a subset of JARs. In this case, you need two Here is an example which selects the correct SWT native JAR for your platform, adds all your application's JARs and then invokes the real |
|||||||||||||||
| Mapping classnames to pathnames The standard Java toolchain (and 3rd-party tools designed to interoperate with them) have specific rules for mapping the names of classes to the pathnames of files and other resources that represent them. The mappings are as follows
This is illustrated in the following table:
|
|||||||||||||||
| What the classpath means: how searches work The purpose of the classpath is to tell a JVM where to find classes and other resources. The meaning of the classpath and the search process are intertwined. The classpath is a form of search path which specifies a sequence of locations to look for resources. In a standard classpath, these places are either, a directory in the host file system, a JAR file or a ZIP file. In each cases, the location is the root of a namespace that will be searched. The standard procedure for searching for a class on the classpath is as follows:
The procedure for searching for a resource on the classpath depends on whether the resource path is absolute or relative. For an absolute resource path, the procedure is as above. For a relative resource path resolved using (Note these are the procedures implemented by the standard Java classloaders. A custom classloader might perform the search differently.) |
|||||||||||||||
| The bootstrap classpath The normal Java classloaders look for classes first in the bootstrap classpath, before checking for extensions and the application classpath. By default, the bootstrap classpath consists of the "rt.jar" file and some other important JAR files that are supplied by the JRE installation. These provide all of the classes in the standard Java SE class library, along with various "internal" implementation classes. Under normal circumstances, you don't need to concern yourself with this. By default, commands like Very occasionally, it is necessary to override the normal behavior of the Java runtime by using an alternative version of a class in the standard libraries. For example, you might encounter a "show stopper" bug in the runtime libraries that you cannot work around by normal means. In such a situation, it is possible to create a JAR file containing the altered class and then add it to the bootstrap classpath which launching the JVM. The
Note that when use the bootclasspath options to replace or override a Java class (etcetera), you are technically modifying Java. There may be licensing implications if you then distribute your code. (Refer to the terms and conditions of the Java Binary License ... and consult a lawyer.) RemarksJava class loading The JVM (Java Virtual Machine) will load classes as and when the classes are required (this is called lazy-loading). Locations of the classes to be used are specified in three places:-
Classes are loaded using classes that are subtypes of Classpath The classpath is a parameter used by the JVM or compiler which specifies the locations of user-defined classes and packages.
This can be set in the command line as with most of these examples or through an environmental variable ( |
Read Article
https://develop.horje.com/learn/1434/reference