Download and install Oracle JDK
How class file is found and loaded
Package and the relationship CLASSPATH
to sum up
In this paper, the contents of official documents from Oracle Java SE Tools Reference. Oracle documentation Java area is very well. Java 8 to interested friends, you can directly find the total inlet Java SE 8 Documentation, what you want to read something. This blog from time to time to move bricks from Oracle's official website.
Using Java in Linux, I usually direct use Linux distribution comes with the package, a command to get JDK installation. But Linux distributions comes with the JDK often OpenJDK, though not affect the use, but if you want to follow Oracle's documentation to learn, or to install an Oracle JDK better. OpenJDK and Oracle JDK quite closely, most of its contents are the same, part of the code related to patents or authorization factors in addition to a small number of Oracle JDK. In addition, Oracle JDK tool that comes to a little more, such as commercial Java Mission Control. Finally, there is not the same as a trademark, remember, Java is a registered trademark of Oracle now oh. As different license agreement, we do not care about ordinary users, Oracle now available for free download, we just use it wants.
In addition, the Java SE 8 document, there is such a picture, a complete demonstration of the Java SE components. Each small square in the official website of the picture is a hyperlink, if you want to learn which piece of knowledge, click the small box on it. I am here just a screenshot, no navigation function. The picture on this is to make yourself to have a Java core technology overview.
Download and install Oracle JDK
To download the Oracle JDK, definitely going to Oracle's official website, the official website found inside the download page for Java is not difficult, I will not say more. I chose the version is JDK 8 For Linux 64-bit version. At the same time, I have no choice rpm packages, but chose this .tar.gz archive
After downloading the tarball down, put my own home directory, after decompression can be used. This is entirely my own private version of the other version of Java is not installed with the system conflict. The disadvantage is that when I want to call java, javac such commands, I must specify the full path. The command to extract the tar zxf jdk1.8.0u45-linux-x64.tar.gz.
Familiar with Java's friends certainly know, before the installation of the JDK, you must set two environment variables PATH and CLASSPATH. But in the JDK 8, which are not necessary. PATH environment variable is not set because of OpenJDK in my system, even if the ~ / jdk1.8.0_45 / bin was added to the PATH, call the java command when the use is still OpenJDK. So when using Oracle JDK, I'll just lose the full path of good. The reason is not set CLASSPATH Java SE 8 can automatically detect the path where the Class. Of course, you can also set the CLASSPATH environment variable, but not that necessary.
After (End cross-sectional diagram I think, in fact, in order to cover the OpenJDK java command is also possible, as long as such set the environment variable export PATH = ~ / jdk1.8.0_45 / bin: $ PATH, instead of export PATH = $ PATH: ~ /jdk1.8.0_45/bin will be able to use Oracle JDK when you do not always enter the full pathname.)
Then, write a HelloWorld program test. This program is so simple, there is no need to use such a huge Eclipse IDE, use Vim enough.
I wrote this test program is to prove that you can run Oracle JDK 8, in particular in order to prove JDK 8 do not set the CLASSPATH environment variable can also be run. In addition to the program output "Hello, world!", It also output Java Runtime default system configuration.
In the default system configuration, there are several more important value, I deliberately use the red box to mark it out. Java Runtime class is loaded from the path.
Let's look at how the Java runtime search and load the class. In the following text, I use the JDK instead of ~ / jdk1.8.0_45 directory indicates Oracle JDK installation path 8.
How class file is found and loaded
First is that the gossip about Java, we all know, running a Java program needs a starter, such as java command, as we all know, everything is in the Java class, only the method belong to the class, without separate function. This is Wang Yin Great God had criticism. Indeed, Java is a pure object-oriented language, too pure, how could this world all things are objects? There is supposed to be something that should exist outside the object thing, such as the operation of multiple objects pure functions, such as the entry point of the main function. So from the perspective of language speaking, Java criticized for granted. However, Java this "pure", simplify its implementation. Because everything in Java classes, so you can let the Java code compiled, each class to generate a .class file, then .class files into the appropriate directory, or packaged into .jar archive. When the program is running, it will start with a corresponding .class file is loaded, and executes bytecode can. This design is very simple and convenient. The C # way this organization does not use class and bytecode, but to learn Java so that all functions have become class methods, I think the opposite is inappropriate.
JVM loads .class file in the following order:
Bootstrap classes. These classes are the basis of the Java platform. Which contains all familiar rt.jar, also contains a number of other classes or jar package.
Extension classes. These classes extend the Java platform, or that these classes use the Java extension mechanism. These classes have two requirements: One is to be packaged into a jar, not a separate .class file, and these packages must be stored in a jar under JDK / jre / lib / ext directory; the other is not reference these classes Bootstrap classes and Extension classes outside of the class.
User classes. That is, user-defined classes. We have our own written procedures from other places to download third-party libraries fall into this category. JVM last load these classes, where to find it? That's where CLASSPATH environment variable, or use the -classpath parameter when the program starts.
In the above three processes, the user is not required to specify the directory Bootstrap classes and Extension classes are located, only you need to specify the directory where the User classes on it. This is the JDK 8 and earlier distinction. Path and Extension classes Bootstrap classes where you can automatically search. Java system configuration in front of the picture, I marked the sun.boot.class.path item represents the directory where the Bootstrap classes, default JDK / jre / lib in rt.jar and other jar files. You can use this option to change the path -Xbootclasspath start the program in time. Extension classes and the path is JDK / jre / lib / ext directory, in front of the picture I have marked out.
User classes on search paths, if not specified, the default is the current directory, the winning pictures in front of me is the default value. If you reassign User classes search path, then it will not search from the current directory, and if you want to search the current directory, the current directory must be explicitly added to the CLASSPATH. User classes specified search path in the following ways:
Setting the CLASSPATH environment variable;
Use -cp or -classpath option when the program starts;
If you are using java -jar way to start the program, from the jar package manifest file Class-Path configuration items specified in the search path.
The search path can be a directory, can also be a jar package, it can be any combination of them, each with between one item: segmentation. Additionally, the path can also use the wildcard *, but the only wildcard matching all the jar packages under a directory, but can not match the .class file, can not even match subdirectory.
So the question is, since the CLASSPATH can contain jar package, while the jar package and can specify Class-Path, and the Class-Path in the jar and can specify a different package, this will cause an infinite loop it? JVM jar package in the search when what rules? Yes, the following three:
jar package specified in the Class-Path will be treated as part of the CLASSPATH, and the package before the jar files in the other class, and search for class files in accordance with its path appear in the order in CLASSPATH conducted, first-served basis to find .
If the surface before sweeping over the jar package appeared again, repeat the scan is not performed;
If a jar package is installed as a Java extension, that is, its JDK / jre / lib / ext directory, ignore its Class-Path configuration items.
About javac, javadoc command and tools.jar there are some exceptions, the presence of these exceptions is due to the dual needs to run javac, javadoc these procedures for .class files the decision. As JDK tools, javac, javadoc run need tools.jar the support classes. In addition, when javac, javadoc compiler, but also need to parse the source code references to other classes. The basic rules are as follows:
Run javac, javadoc these commands, Bootstrap classes themselves use these commands, Extension classes and tools.jar can not be changed, they only use JDK version in which they are located;
If other code javac, javadoc used to parse the tools.jar the class, it must be added to the User classes tools.jar search path in order to work; if they want to resolve the references to different versions of the code the Bootstrap classes and Extension classes, you can use these two commands -bootclasspath and -extdirs option specifies.
Finally, the type of load is also affected by Class Loader and Security Policy (Security Policies) impact. In Java, we can use different Class Loader, you can write your own Class Loader, but most of the time, built-in Class Loader we are using. You can enable different security policies in Java, we trust class allows it to load, distrust is not allowed. If you do not turn on security policy, all classes are considered to be trusted. But even opened a security policy, it will only affect the Extension classes and User classes, for Bootstrap classes are not affected.
Package and the relationship CLASSPATH
In Java, where there is a headache, is the relationship between the stowed position and the package .class files between. In the Java language, in order not naming conflicts occur, deliberately introduced into the package mechanism, Java Classes into different package being. I show in front of HelloWorld.java, I will package it to com.xkland.java_8_study, then afterwards compile this program, I deliberately use the javac -d. Option to set the current directory as the target compiled position. After compilation, I deliberately use the tree command checked the directory structure.
As can be seen from the results, HelloWorld.class storage location ./com/xkland/java_8_study/HelloWorld.class. This package naming and directory hierarchy is entirely consistent. And it must be consistent, otherwise it will not find the .class files, and can not run the program. So, if your package is named abcd such, can only be organized into a / b / c / d / xx.class such a directory tree and a directory from one directory to begin searching for this class to find That is, the parent directory must be a directory added CLASSPATH. Without this limitation on the source code, but use the same way of organizing source code is a good practice. If the source directory is flat, like my previous example, the call must be specified when javac -d parameter, it will automatically generate a directory structure .class files.
to sum up
Nothing good summary. Just read that record Oracle Java SE 8 after the official documentation. How classes are found? It's worth the occasional refresher.