How to find classes in Java starters

Source: Internet
Author: User
Tags i18n
For details about how to find the class-general Linux technology-Linux programming and kernel information, see the following. Java initiator java initializes the Java Virtual Machine. The VM then searches for and loads classes in the following order:

UDF Class-A Java platform class, including the classes in rt. jar and i18n. jar.
Extension class-class using Java extension mechanism. They are bundled as. jar files in the extension directory.
User class-A class defined by developers and third parties. The extension mechanism is not used. Use the-classpath option (common method) on the command line or use the CLASSPATH environment variable to identify the location of these classes (see setting Classpath for Windows or Solaris ).
In fact, these three search paths are connected to a simple class path. This is a bit like the previous "single-level" class path, but the current model has some important differences:

It is relatively difficult to "hide" or omit a UDF class by chance.
Generally, you only need to specify the location of the user class. The system will automatically search for the auto-initiated class and extended class.
The tool class is now in an independent archive (tools. jar ). It is available only when it is included in the user class path (will be explained later ).

How to find a UDF class in Java starters

A custom class is a class that implements the Java 1.2 platform. The UDF class is in the rt. jar and i18n. jar archives in/jdk1.2/jre/lib. These archives are specified by the bootstrap class path value stored in the sun. boot. class. path System attribute. This system property is for reference only and should not be directly modified.

The possibility of customizing the class path is small. In rare cases, when you need to use another set of core classes, the non-standard option-Xbootclasspath allows you to redefine the custom class path.

Note: The classes that implement JDK development tools are different from the self-initiated classes in different archives. The tool archive is/jdk1.2/lib/tools. jar. When the initiator is called, the development tool adds the archive to the user class path. However, this expanded user class path is only used for execution tools. The javac and javadoc tools that process source code use the initial class path instead of the expanded class path (for more information, see how to find classes in the following Javac and Javadoc ).

How to find extension classes in Java starters

An extension class is a class that extends the Java platform. Each. jar file in the extension directory is considered an extension and is loaded using the Java extension framework. We do not provide the option to change the location of the extended directory.

How to find user classes in Java starters

User classes are constructed on the Java platform. When searching for user classes, the initiator needs to use the user class path, which is a list of directories, JAR archives, and ZIP archives containing class files.

A class file has a sub-path that reflects the full-qualified name of the class. For example, if the class com. mypackage. if MyClass is stored in/myclasses,/myclasses must be in the user class path, and the full path of the class file must be/myclasses/com/mypackage/MyClass. class. If the class is stored in the archive named myclasses. jar, myclasses. jar must be in the user class path, and the class file must be stored as com/mypackage/MyClass. class in the archive.

In Solaris, we use a string to specify the user class path, and use a colon (:) to separate class path items. The java initiator places the user class path string in the java. class. path System attribute. The possible source of this value is:

The default value "." indicates that the user class file is all class files in the current directory (if it is in the package, it is under the current directory ).
The value of the CLASSPATH environment variable, which overwrites the default value.
The value of the command line option-cp or-classpath, which overwrites the default value and CLASSPATH value.
-The jar file specified by the JAR option overwrites all other values. If this option is used, all user classes are from the specified archive.

How to find the JAR-CLASS-PATH CLASS

A jar file usually contains a "list"-this file lists the JAR content. The list can define the JAR-CLASS-PATH, which further extends the class path (only when the CLASS is loaded from the JAR ). The following sequence is used to query the classes accessed by JAR-CLASS-PATH:

Generally, when you look for a CLASS referenced by the JAR-CLASS-PATH item, it is treated as part of the JAR file. The JAR file that appears in JAR-class-path will be searched after the class path item before JAR-CLASS-PATH and before the class path item after JAR-CLASS-PATH.
However, if JAR-CLASS-PATH points to a JAR file that has been searched (for example, extension or JAR files listed before the class path ), the JAR file is no longer searched (this optimization method improves efficiency and prevents loop search ). Such a JAR file will be searched in the place where it appears (the top of the class path.
If the JAR file is installed as an extension in the ext subdirectory of jdk, the JAR-CLASS-PATH defined by the JAR file is ignored. We assume that all classes required for the extension are part of JDK or have been installed as extensions.

How to find classes in OldJava starters

The oldjava initiator does not support the Java extension framework. It provides backward compatibility in the following cases:

There is an application that implements a 1.1-style Security Manager (incompatible with the 1.2 class loading method), or
The class being loaded has been generated or managed in a way that is incompatible with the 1.2 class structure (for example, using a fuzzy parser.
(For more information about these issues, see the java starter reference page for Windows or Solaris .)

The oldjava initiator combines the UDF class and user class in a class path. There are two methods to specify the class path using oldjava:

Specify the user class path in the CLASSPATH environment variable. Add the user class path to the predefined class path.
Use the command line option-cp or-classpath to specify the complete class path. Ignore the CLASSPATH value and the pre-defined custom class path.
The combined class path is stored in the java. class. path System attribute.

How to find classes in Javac and JavaDoc

The javac and javadoc tools use class files in two different ways.

Like other Java applications, javac and javadoc must load multiple types of files to run.
To process the source code for their operations, javac and javadoc must obtain information about the object types used in the source code.
Most of the class files used to parse source code references are the same as those used to run javac and javadoc, but there are some important exceptions:

Javac and javadoc often parse references to classes and interfaces unrelated to javac or javadoc implementation. Information about referenced user classes and interfaces may exist in the form of class files, source code files, or both.
The tool class in tools. jar is only used to run javac and javadoc. The tool class does not need to parse source code references unless tool. jar is in the user class path.
Programmers may need to use another Java platform to parse self-cited classes or extend class references. Both javac and javadoc support the-bootclasspath and-extdirs options. Using these options will not modify the class file set used to run the javac or javadoc tool itself.
If the referenced class is defined in the class file and the source file, javadoc usually uses the source file (javadoc does not compile the source file ). In the same case, javac uses class files, but will automatically recompile the class files that it considers to have expired. The automatically re-compiled rules are described in the javac document (Windows or Solaris.

By default, javac and javadoc search for class files and source code files in the user class path. If the-sourcepath option is specified, javac and javadoc only search for the specified source file path.

Class loading and security policies

Class or interface must be loaded with the Class Loader before it can be used. The use of a specific class loader determines the security policies associated with the class loader.

The program can load classes or interfaces by calling the loadClass method of the Class Loader object, but usually the program is loaded only by reference. In this way, the internal class loader will be called to apply security policies to the extension class and user class. If the security policy is not enabled, all classes are "trustable ". Even if the security policy is enabled, it is not applicable to the custom class. It is always "trustworthy ".

After the security policy is enabled, the system and user policy files are configured. The JDK software includes a system policy file that grants "trusted" status to the extension class and sets basic restrictions on the user class.

To enable or configure security policies, see security features.

Note: The security programming technology used by some Java 1.1 platforms is not compatible with the 1.2 class loading model. To provide temporary support for existing code, this version includes the oldjava starter, which uses Class 1.1 to load the model.
Related Article

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.