JDK1.8版本号:jdk1.8.0_202(提取码:n937)
一、找到源码位置
其实我们在安装JDK完时就源码就已经存在,所有要找到JDK所安装的目录,会看到src.zip、javafx-src.zip的压缩包,这里面就是JDK的源码,例如下图:
二、Intellij IDEA搭建源码阅读环境
1、Intellij IDEA新建java项目
打开Intellij IDEA,菜单栏File > Project,新建简单java项目,例如下图:
在点Next, 填写项目名称和保存项目目录,例如下图:
点击finish后,项目创建完成,例如下图:
2、把源码src.zip解压到该工程下的src目录下,并新建Main类
例如下图:
3、将原来关联的jdk安装目录下的源码src.zip替换成jdk1.8.0_202-source-analysis的src
为了避免修改原来的SDKs,另外再添加一个名为“jdk1.8.0_202-source-analysis”的jdk,并移除src.zip
File ->Project Structure->Platform Settings ->SDKs
将jdk源码项目src添加到sourcepath中
修改项目(jdk1.8.0_202-source-analysis)使用的jdk为新建的“jdk1.8.0_202-source-analysis” jdk
三、运行Main类的main方法
如果运行不成功,会出现以下错误提示:
Error:java: OutOfMemoryError: insufficient memory(系统资源不足)
程序包com.sun.tools.javac.api不存在
找不到sun.awt.UNIXToolkit
找不到sun.font.FontConfigManager
1、Error:java: OutOfMemoryError: insufficient memory
错误原因
导致这个错误的原因主要是因为jdk版本问题,此处有两个原因,一个是编译版本不匹配,一个是当前项目jdk版本不支持。
查看项目的jdk
File ->Project Structure->Project Settings ->Project
或使用快捷键Ctrl+Alt+shift+S打开项目的jdk配置:
查看此两处是否与目标jdk一致
查看工程的jdk
点击上图中Modules查看对应jdk版本:
查看java编译器版本
File–>Setting…–>Build,Execution,Deployment–>Compiler–>Java Compiler
设置相应Module的target bytecode version与jkd一致版本
针对此问题,重新打开或修改pom文件(maven项目)中的内容很可能导致jdk版本重新变为1.5。如果是maven项目,可在pom文件中指定jdk相关信息:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
查看构建过程编译器所需的堆大小
File–>Setting…–>Build,Execution,Deployment–>Compiler
堆大小可由原来的700改为1000
2、程序包com.sun.tools.javac.api不存在
手动将jdk安装目录下lib包中tools.jar添加到项目中
File ->Project Structure->Project Settings ->Libraries
将tools.jar lib添加到jdk1.8.0_202-source-analysis项目中
点击ok后。解决Error:(40, 31) java: 程序包com.sun.tools.javac.api不存在问题
3、找不到sun.awt.UNIXToolkit、找不到sun.font.FontConfigManager
解决缺少的这两个类,可以去OpenJDK(传送门)拷贝
找到UNIXToolkit类,将UNIXToolkit类中的内容( 传送门)拷贝到项目的src\main\java\下,新建“sun\awt”包中的新建“UNIXToolkit.java”中
到此已解决sun.awt.UNIXToolkit找不到的错误
送佛送到西,对应本项目中jdk中的UNIXToolkit类内容如下,注意:其他jdk版本自行拷贝
/*
* Copyright (c) 2004, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.awt;
import java.awt.RenderingHints;
import static java.awt.RenderingHints.*;
import java.awt.color.ColorSpace;
import java.awt.image.*;
import java.security.AccessController;
import java.security.PrivilegedAction;
import sun.security.action.GetIntegerAction;
import com.sun.java.swing.plaf.gtk.GTKConstants.TextDirection;
import sun.java2d.opengl.OGLRenderQueue;
import sun.security.action.GetPropertyAction;
public abstract class UNIXToolkit extends SunToolkit
{
/** All calls into GTK should be synchronized on this lock */
public static final Object GTK_LOCK = new Object();
private static final int[] BAND_OFFSETS = { 0, 1, 2 };
private static final int[] BAND_OFFSETS_ALPHA = { 0, 1, 2, 3 };
private static final int DEFAULT_DATATRANSFER_TIMEOUT = 10000;