Install the class loading mechanism of Oracle JDK 8 and JVM in Linux

Source: Internet
Author: User
Tags bootstrap classes oracle documentation

Install the class loading mechanism of Oracle JDK 8 and JVM in Linux

Reading directory

  • References
  • Preface
  • Download and install Oracle JDK
  • How to discover and load class files
  • Relationship between CLASSPATH and Package
  • Summary
References

The content in this article comes from the official Oracle documentation Java SE Tools Reference. Oracle documentation on Java is very complete. If you are interested in Java 8, you can directly find the Java SE 8 Documentation entry. This blog is occasionally updated on the Oracle official website.

Preface

When I use Java in Linux, I usually directly use the software package that comes with the Linux release. A command can be used to install JDK. However, the JDK that comes with Linux releases is usually OpenJDK. Although it does not affect the usage, it is better to install an Oracle JDK if you want to follow the Oracle documentation. OpenJDK has a lot to do with Oracle JDK, and most of its content is the same, except for a few of the Code in Oracle JDK that involves patented technology or authorization factors. In addition, Oracle JDK comes with more tools, such as commercial Java Mission Control. Finally, the trademarks are different. Remember that Java is now the registered trademark of Oracle. As for the different authorization protocols, we do not need to worry about them. Since Oracle provides free download, we just need to use it.

In addition, in the Java SE 8 document, there is such an image that completely shows the composition of Java SE. Each small square of the image on the official website is a hyperlink. If you want to learn which one, click the corresponding small square. I am only one here and have no navigation function. Put the image here, that is, let yourself have a general understanding of the core technology of Java. For example:
 

Download and install Oracle JDK

To download the Oracle JDK, you must go to the Oracle official website. It is no difficulty to find the Java download page on the official website. The version I selected is the 64-bit version of JDK 8 For Linux. At the same time, I chose the. tar.gz compressed package instead of the rpm package, for example:

After downloading the compressed package, put it in my own home directory. decompress the package and use it. This is my own private version and will not conflict with other Java versions installed in the system. The disadvantage is that when I want to calljava,javacWhen using this command, I must specify the complete path. The decompressed command istar zxf jdk1.8.0u45-linux-x64.tar.gz.

If you are familiar with Java, you must set two environment variables: PATH and CLASSPATH. However, this is not required in JDK 8. The PATH environment variable is not set because OpenJDK exists in my system.~/jdk1.8.0_45/binAdd to PATH and calljavaOpenJDK is still used for the command. So when I use Oracle JDK, I will directly enter the complete path. The reason why CLASSPATH is not set is that Java SE 8 can automatically detect the path of the Class. Of course, setting the CLASSPATH environment variable is also acceptable, but it is unnecessary. For example:

(After capturing the image, I thought that, in fact, to overwrite the OpenJDKjavaThe command is also acceptable, as long as the environment variable is set in this wayexport PATH=~/jdk1.8.0_45/bin:$PATHInsteadexport PATH=$PATH:~/jdk1.8.0_45/binYou can. When using Oracle JDK, you do not need to enter the full path every time .)

Then, write a HelloWorld program to test it. This program is too simple. There is no need to use a huge IDE such as Eclipse, and Vim is enough. For example:

Write this test program to prove that Oracle JDK 8 can run, especially to prove that JDK 8 can run without setting the CLASSPATH environment variable. In addition to output "Hello, world !" In addition, the default system configuration for Java Runtime is also output. For example:

In these default system configurations, there are several important values. I marked them with a red box. The Java runtime loads the class from these paths. For example:

The following describes how to search for and load classes during Java runtime. In the following text, I useJDKReplace~/jdk1.8.0_45Directory, indicating the installation path of Oracle JDK 8.

How to discover and load class files

Let's talk about Java first. Everyone knows that a starter is required to run a Java program, for examplejavaAs you know, in Java, everything is a class. There is only a class method, and there is no separate function. In this regard, the great god Wang has been criticized. Indeed, Java is a pure object-oriented language. It is too pure. How can everything in the world be an object? There should be something outside the object, such as pure functions that operate on multiple objects, such as the main function at the entry point of the program. Therefore, from a language perspective, it is natural to criticize Java. However, Java's "pure" simplifies its implementation. Because everything in Java is a class, every class can generate. class file, and then. put the class file in the corresponding directory or package it. jar package. When running the program, use an initiator to load the corresponding. class file and execute the bytecode. This design is very simple and convenient. C # does not use this method of organizing classes and bytecode, but learns Java to make all functions become class methods. I think the opposite is inappropriate.

The JVM loads the. class file in the following order:

  1. Bootstrap classes. These classes are the basis of the Java platform. Including rt. jar, which everyone is familiar with, and some other classes or jar packages.
  2. Extension classes. These classes extend the Java platform, or use the Java extension mechanism. These classes have two requirements: first, they must be packaged into a jar file, not a separate. class file, and these jar packages must be stored inJDK/jre/lib/extDirectory; the second is that these classes cannot reference classes other than Bootstrap classes and Extension classes.
  3. User classes. That is, the user-defined class. Third-party libraries that we have downloaded from other places are in this category. Where can I find the final classes loaded by JVM? This requires the CLASSPATH environment variable or the-classpath parameter when the program starts.

In the preceding three processes, you do not need to specify the directory where Bootstrap classes and Extension classes are located. You only need to specify the directory where User classes is located. This is the difference between JDK 8 and earlier versions. The paths of Bootstrap classes and Extension classes can be searched automatically. In the preceding figure, the sun. boot. class. path in the Java System Configuration that I marked represents the directory where Bootstrap classes is located. The default value isJDK/jre/libInrt.jarAnd some other jar files. You can use the-Xbootclasspath option to change the path when starting the program. The Extension classes path isJDK/jre/lib/extDirectory, which is marked in the previous image.

If the User classes search path is not specified, it is the current directory by default..The default value is marked in the preceding figure. If you re-specify the User classes search path, the current directory will not be searched. to search from the current directory, you must explicitly add the current directory to the CLASSPATH. You can specify the User classes search path in the following ways:

  1. Set the CLASSPATH environment variable;
  2. Use the-cp or-classpath option when the program starts;
  3. If you usejava -jarTo start the program, search for the Path specified in the Class-Path configuration item in the manifest file in the jar package.

The search path can be a directory, a jar package, or any combination between them.:Split. In addition, wildcards can be used in the path.*However, this wildcard can only match all the jar packages in a directory, but cannot match the. class file, and cannot match sub-directories.

The problem arises. Since the CLASSPATH can contain the jar package and the Class-Path can be specified in the jar package, other jar packages can be specified in this Class-Path, will this cause an infinite loop? Does JVM have any rules when searching for jar packages? Yes, there are three items as follows:

  1. The specified Class-Path in the jar package will be treated as a part of CLASSPATH and placed before other class files in the jar package, the search for class files is performed according to the order in which their paths appear in CLASSPATH. You must first find them first.
  2. If the jar package that has been scanned has appeared again, the scan will not be repeated;
  3. If a jar package is installed as a Java extensionJDK/jre/lib/extThe Class-Path configuration item is ignored.

Aboutjavac,javadocCommands andtools.jarSome exceptions exist becausejavac,javadocThese programs are required to run the. class file. As a JDK tool,javac,javadocRequired to runtools.jarIn addition, whenjavac,javadocWhen compiling a program, you need to parse references to other classes in the source code. Its basic rules are as follows:

  1. Runjavac,javadocWhen using these commands, the Bootstrap classes, Extension classes, and tools. jar used by these commands cannot be changed. They only use the version in their JDK;
  2. If yesjavac,javadocTools are used in other code to be parsed. jar class, the tools. jar can be added to the User classes search path. If you want the code to be parsed to reference different versions of Bootstrap classes and Extension classes, you can use the-bootclasspath and-extdirs options of these two commands to specify.

Finally, Class loading is also affected by Class Loader and Security Policies. In Java, we can use different Class loaders or write our own Class loaders. However, most of the time, we use built-in Class loaders. Different security policies can be enabled in Java. classes we trust can be loaded without trust. If the security policy is not enabled, all classes are considered as trusted. However, even if the security policy is enabled, only Extension classes and User classes are affected, and Bootstrap classes is not affected.

Relationship between CLASSPATH and Package

Another headache in Java is the relationship between the storage location of the. class file and package. In Java, the package mechanism is introduced to avoid naming conflicts, and classes in Java are divided into different packages. In the HelloWorld. java I showed earlier, I set its package to com. xkland. java_8_study. After compiling this program, I used it specially.javac -d .Option to change the current directory.As the destination location after compilation. After compilation, I also usetreeCommand to view the directory structure.

As shown in the results,HelloWorld.classThe storage location is./com/xkland/java_8_study/HelloWorld.class. The directory hierarchy is exactly the same as the package name. Otherwise, the. class file cannot be found and the program cannot be run. Therefore, if your package is named a. B. c. da/b/c/d/xx.classSuch directory tree, andaYou can find this class only when you start searching for the upper-level directory of the directory. That is to say, you mustaAdd CLASSPATH to the upper-level directory of the directory. There is no such restriction on the source code, but it is a good practice to organize the source code in the same way. If the source code directory is flat, as in the previous examplejavacYou must specify-dThe directory structure of the. class file is automatically generated.

Summary

There is nothing to summarize. It is a record after reading the official Oracle Java SE 8 documents. How classes are found? It is worth reviewing occasionally.

Install JDK 1.8.0 _ 25 on Ubuntu 14.04 and configure Environment Variables

Install Oracle JDK 14.04 on Ubuntu 1.8 LTS

CentOS6.3 install JDK and environment Configuration

Install JDK 8 on Ubuntu 14.04

Install JDK graph analysis in Ubuntu

This article permanently updates the link address:

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.