JDK, full name Java Development Kit, contains the basic tools needed to engage in Java program Development. The JDK we use on all platforms is a generic version provided by Oracle. Because Java programming can be done at the top level with only simple environment variables, the author layer ignores many details inside the JDK.
If we can figure out exactly what the JDK contains, we can even make minor tweaks to the JDK itself at a later date through the source code. I recently picked up The Book Understanding the Java Virtual Machine in Depth by Zhiming Zhou and plan to follow the book’s outline to learn more about the JVM virtual machine.
Before taking a look at how the JVM works, I chose to start by manually compiling the JDK. It is recommended that you read the official Open Jdk documentation: OpenJDK Build README before you start compiling
This blogger provides a very clear idea: compile OpenJDK 8. In addition, those of you who choose to compile in Ubuntu can also refer to this article: How to compile OpenJDK source code hand in hand.
Lead to
I compiled the JDK in a CentOS 7 virtual machine environment. Note that it is best to allocate as much memory as possible for this virtual machine (I assume there will be a lot of intermediate content during compilation), otherwise you will have problems with memory allocation during compilation, or seriously reduce compilation efficiency.
Notice, we need to do two things:
-
JDK 8 source Baidu Cloud download extract code: 74FE
-
JDK 7 baidu Cloud download extract code: DXKY
Why is that? The OpenJDK we’re compiling is still mostly Java code, except that the HotSpot virtual machine is written in C/C++, so we’re relying on a Java environment that works properly during compilation, which is called “compiling JDK’s JDK”. Also known as the “BootStrap JDK”.
If we want to compile the JDK with version N, we need a JDK with at least version N-1 as the BootStrap JDK. Obviously, here our target JDK version is 8, and the BootStrap JDK is 7.
After you download the JDK, don’t rush to configure the JAVA_HOME environment variable. On the other hand, if your system already has JAVA_HOME ready for you, you’ll need to remove the configuration first. (CentOS can use the env command to quickly check environment variables)
After downloading the above two packages, use tar -zxvf to decompress the.gz package of JDK 7 and unzip to decompress the.zip package of JDK 8 (this command is decompressed to the current directory by default).
OpenJDK directory structure
Below is the OpenJDK directory structure, which gives you a glimpse of the components that the JDK contains, including the core of Java, the virtual machine.
Openjdk -- CORBA: multi-language, distributed communication interface -- Hotspot: Java Virtual Machine -- JAXP: XML processing -- JAXWS: Java API for a set of XML Web Services -- JDK: Nashorn: JavaScript runtime on the JVMCopy the code
Its with Oracle JDK
We will use the OpenJDK here for the compilation exercise, whereas previously we used the Oracle JDK directly downloaded from the Oracle website. The history of Java lies in the history of many tech companies, which I won’t talk about here.
Here’s a brief description of the connection between the OpenJDK and the Oracle JDK: OpenJDK is Sun’s 2006 project to open source Java. Every major technology company has a separate JDK based on the OpenJDK after secondary development, including IBM’s J9 and Oracle’s Oracle JDK.
The OpenJDK and Oracle JDK are almost indistinguishable from each other in terms of usage, after all, they are both cut from the same mold. However, due to property rights issues, OpenJDK has had to replace some of its forced closed source content with its own open source implementation. In addition, OpenJDK contains only minimal packages such as Rhino, Java DB…… But you can still choose to add them as needed.
So don’t worry too much, whatever THE XX JDK is, it should be guaranteed to run basic Java code, or it won’t be called a JDK.
HotSpot virtual machine directory structure
HotSpot is currently the most common Java virtual machine, where the HotSpot VM implementation is in the HotSpot/SRC directory. Its structure is as follows:
├─ Agent Serviceability Agent client implementations. ├─ Make various configuration files for building HotSpot. ├─ SRC HotSpot VM source code │ ├─ EXERCISES │ ├─ EXERCISES │ ├─ Exercises │ ├─ Exercises │ ├─ Exercises │ ├─ Exercises │ ├─ Exercises │ ├─ Exercises │ ├─ Exercises │ ├─ Exercises │ ├─ Exercises │ ├─ ├─LogCompilation. │ ├─ Bass Exercises for Visualizing the intermediate code of the Server compiler │ ├─ Bass Exercises for Java │ ├─LogCompilation │ ├ ─ProjectCreator │ ├ ─ VM Hotspot │ ├─ Interface to the ASM Assemble.│ ├─ C1 Client Compiler (also known as "c1") │ ├─ Ci Public service for the dynamic compiler/Interface from the dynamic compiler to the VM │ ├─ Implementation GC │ ├─ Implementation GC │ ├─ Implementation GC │ ├─ Implementation GC Exploiture │ │ ├─parallelScavenge │ │ ├─ Exploiture. Exploiture and exploiture Exploiture ParallelScavenge GC │ │ ├─ Exercises │ ├─ exercises │ ├─ exercises │ ├─ exercises │ ├─ exercises │ ├─ Exercises │ ├─ Exercises For example, the template interpreter and the C++ interpreter. For example, the template interpreter and the C++ interpreter. For example, for example, the template interpreter and the C++ interpreter │ ├─ HotSpot VM For Prims │ ├─ Services is an interface for supporting management functions such as JMX │ ├─shark │ ├ ─ Utilities │ ├ ─test (PDF, PDF, PDF, PDF, PDFCopy the code
Prepare the compile tool and compile
You can quickly install the tools you need to compile using the yum tool:
$ sudo yum groupinstall "Development Tools" \
yum install libXtst-devel libXt-devel libXrender-devel \
yum install cups-devel \
yum install freetype-devel \
yum install alsa-lib-devel
Copy the code
Once everything is ready, we need to go to the root directory of the OpenJDK and prepare some configuration information:
$ sudo cd yourOpenJdk
$ sudo bash ./configure --with-target-bits=64 --with-boot-jdk=yourBootJdk --with-debug-level=slowdebug --enable-debug-symbols ZIP_DEBUGINFO_FILES=0
Copy the code
Here is a brief introduction to the parameters:
with-target-bits
: specifies whether to generate a 64-bit or 32-bit JDK.with-boot-jdk
:Point to the actual path of your own BootStrap JDK.with-debug-level
: Debug level at compile time, including release, FastDebug, and slowDebug. The default is release.enable-debug-symbols
: generates debugging symbol information.
The configure command takes care of dependency checking, parameter configuration, and so on. If some tools or dependencies are missing, this command will give you a clear indication. If nothing goes wrong with the configure command, we can compile using the make command.
$ sudo make all ZIP_DEBUGINFO_FILES=0
Copy the code
The author allocated a CPU and 3 GB of memory space for the virtual machine, and the compilation process took a long time (the author allocated only 1GB of memory space initially, which resulted in a memory allocation failure error during the compilation process, so the compilation had to be recompiled after adjusting the virtual machine configuration). Warnings may be reported at multiple points during compilation, but as long as there are no warnings that interrupt compilation, it generally does not affect compilation results.
In addition, the console may print the following error during the build process, which is a bug in the build process itself and can be ignored for the time being.
Generating Nimbus source files Verifying /u/alanb/ws/tl/build/linux-x86_64-normal-server-release/jdk/gensrc_x11wrappers/sizes.64.verification.tmp to /u/alanb/ws/tl/build/linux-x86_64-normal-server-release/jdk/gensrc_x11wrappers/sizes.64 [Error] encoded value was less Than 0: encode(-8.326673e-17, 5.0, 11.0, 16.0) [Error] encoded value was less than 0: Encode (-0.05882353, 1.0, 24.0, 25.0) [Error] encoded value was greater than 3: Encode (15.029411, 1.0, 14.0, 15.0) [Error] encoded value was less than 0: Encode (-0.05882353, 1.0, 24.0, 25.0) [Error] encoded value was greater than 3: Encode (15.029411, 1.0, 14.0, 15.0) [Error] encoded value was less than 0: Encode (-0.05882353, 1.0, 24.0, 25.0) [Error] encoded value was less than 0: Encode (-0.05882353, 1.0, 24.0, 25.0) [Error] encoded value was greater than 3: Encode (15.029411, 1.0, 14.0, 15.0) [Error] encoded value was less than 0: Encode (-0.05882353, 1.0, 24.0, 25.0) [Error] encoded value was greater than 3: Encode (15.029411, 1.0, 14.0, 15.0) [Error] encoded value was less than 0: Encode (-0.05882353, 1.0, 24.0, 25.0) [Error] encoded value was less than 0: Encode (-0.05882353, 1.0, 24.0, 25.0) [Error] encoded value was greater than 3: Encode (15.029411, 1.0, 14.0, 15.0) [Error] encoded value was less than 0: Encode (-0.05882353, 1.0, 24.0, 25.0) [Error] encoded value was greater than 3: Encode (15.029411, 1.0, 14.0, 15.0) [Error] encoded value was less than 0: Encode (-0.05882353, 1.0, 24.0, 25.0) [Error] Encountered Infinity: encode(-0.00877193, 0.0, 7.0, 7.0)Copy the code
See this link for an answer to this question. Finally, when the compilation is complete, the console will output the compilation time:
----- Build times -------
Start 2020-08-12 04:26:55
End 2020-08-12 04:40:23
00:00:00 corba
00:00:55 demos
00:05:08 docs
00:00:01 hotspot
00:01:47 images
00:00:01 jaxp
00:00:01 jaxws
00:04:58 jdk
00:00:02 langtools
00:00:35 nashorn
00:13:28 TOTAL
Copy the code
The follow-up work
After the compilation is complete, the BootStrap JDK is complete. After the compilation is complete, we can uninstall the BootStrap JDK.
$sudo yum list installed | grep Java $sudo yum - remove Java - 1.7.0 - its * yCopy the code
The compiled JDK will appear in.. /openjdk/build/linux-x86_64-normal-server-slowdebug/ JDK folder. Because the config parameters may differ at compile time, the folder names printed after compilation may also differ.
We now use the compiled JDK to configure the Java environment variables:
$ sudo vim /etc/profile
Copy the code
Add the following configuration, note that JAVA_HOME should point to your actual folder path, this is my guide only. Do not forget to refresh the configuration using source /etc/profile:
JAVA_HOME=/root/openjdk/build/linux-x86_64-normal-server-slowdebug/jdk/
JRE_HOME=$JAVA_HOME/jre
PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib
export JAVA_HOME JRE_HOME PATH CLASSPATH
Copy the code
Using Java -version print results, the author here shows:
Openjdk Version "1.8.0_181" OpenJDK Runtime Environment (build 1.8.0_181-b13) OpenJDK 64-bit Server VM (build 25.181 - bl3, mixed mode)Copy the code
A machine with the JDK installed will display:
Java Version "1.8.0_144" Java(TM) SE Runtime Environment (build 1.8.0_144-B01) Java HotSpot(TM) 64-bit Server VM (build 25.144 - b01, mixed mode)Copy the code
We can prepare a test jvmtest. Java file to see if the openJDK works:
public class JvmTest {
public static void main(String[] args) {
System.out.println(System.getProperty("user.home"));
System.out.println(System.getProperty("java.version"));
System.out.println(System.getProperty("os.name"));
System.out.println(System.getProperty("java.vendor.url")); }}Copy the code
If both Java and JavAC are ok, the console can output the following:
/ root 1.8.0 comes with _181 Linux http://java.oracle.com/Copy the code