IDEA 2019搭建jdk 1.8源码阅读调试环境

本文介绍了在IntelliJ IDEA 2019中搭建JDK 1.8源码阅读和调试环境的步骤。首先创建Java项目JavaSourceLearn,将src.zip解压后的源码放入source目录,然后解决可能遇到的问题,如增大构建内存、允许进入源码调试、重新关联源码路径。在遇到缺少tools.jar和特定平台类文件时,添加tools.jar依赖并手动添加缺失的UNIXToolkit.java和FontConfigManager.java。通过这些步骤,成功建立了源码阅读和调试环境。
摘要由CSDN通过智能技术生成

搭建源码阅读调试项目

  1. 创建Java项目,命名为JavaSourceLearn
  2. 在src下创建两个目录,分别为source和test,其中source用于存放jdk源码,test用于存放测试代码。
  3. 在JDK安装目录(默认是在C盘)下,jdk文件夹下找到src.zip压缩包。(比如我电脑上的目录是:C:\Program Files\Java\jdk1.8.0_212)
    在这里插入图片描述
  4. 将src.zip解压后复制到JavaSourceLearn项目的source文件夹下,
    在这里插入图片描述
  5. 由于一开始就是看的大佬的视频及博客,这里先把可能出现的问题先解决一下:
  • 问题1:启动调试时Build报错,提示系统资源不足?
    解决方法:
    File-->Settings-->Build,Execution,Deployment-->Compiler,将“Build process heap size(Mbytes)”的值设置得大一点,比如设置为1700(原本是700)。
    在这里插入图片描述
  • 问题2:想从外层代码F7单步调试进入JDK源码,因源码受保护,无法进入?
    解决方法:
    File-->Settings-->Build,Execution,Deployment-->Debugger-->Stepping,取消勾选“Do not step into the classes”。
    在这里插入图片描述
  • 问题3:如何对JDK源码进行编辑?
    分析及解决:当前项目关联的源码仍然是JDK安装目录下的src.zip只读压缩包,需要重新关联到本项目路径下的source目录。打开File-->Project Structure-->SDKs。。。
    在这里插入图片描述
  1. 在test目录下new一个包,取名为hashmap,新建一个Test.java:
package hashmap;
import java.util.HashMap;
import java.util.Map;

public class Test {
   
    public static void main( String[] args ) {
   
        System.out.println("Hell java!");
        Map<String,Double> hashMap = new HashMap<>();
        hashMap.put( "k1", 0.1 );
        hashMap.put( "k2", 0.2 );
        hashMap.put( "k3", 0.3 );
        hashMap.put( "k4", 0.4 );
        for ( Map.Entry<String,Double> entry : hashMap.entrySet() ) {
   
            System.out.println( entry.getKey() +":" + entry.getValue());
        }
    }
}

运行Test.java,报以下几个错误:

  • 问题1:程序包com.sun.tools.javac.不存在。
    分析及解决:
    缺少tools.jar,需要添加tools.jar依赖库。点击**File-->Project Structure-->Libraries**,选择JDK安装目录,在lib目录下找到tools.jar,点击OK,导入依赖包。
    在这里插入图片描述
  • 问题2:找不到符号UNIXToolkit、FontConfigManager。
    **分析及解决:**这是由于Windows平台下缺少了两个java类文件(UNIXToolkit.java和FontConfigManager.java),可以在OpenJDK网站上找到。
    在这里插入图片描述
    找到自己需要的jdk版本,我是用jdk-8u212-windows-x64.exe安装的,版本为jdk1.8.0_212
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    找到UNIXToolkit.java,点击打开,复制下来。
    在这里插入图片描述
    找到FontConfigManager.java,点击打开查看,并将文本内容复制下来。
    在这里插入图片描述
    UNIXToolkit.java:
/*
 * 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;

    // Allowed GTK versions
    public enum GtkVersions {
   
        ANY(0),
        GTK2(Constants.GTK2_MAJOR_NUMBER),
        GTK3(Constants.GTK3_MAJOR_NUMBER);

        static class Constants {
   
            static final int GTK2_MAJOR_NUMBER = 2;
            static final int GTK3_MAJOR_NUMBER = 3;
        }

        final int number;

        GtkVersions(int number) {
   
            this.number = number;
        }

        public static GtkVersions getVersion(int number) {
   
            switch (number) {
   
                case Constants.GTK2_MAJOR_NUMBER:
                    return GTK2;
                case Constants.GTK3_MAJOR_NUMBER:
                    return GTK3;
                default:
                    return ANY;
            }
        }

        // major GTK version number
        public int getNumber() {
   
            return number;
        }
    };

    private Boolean nativeGTKAvailable;
    private Boolean nativeGTKLoaded;
    private BufferedImage tmpImage = null;

    public static int getDatatransferTimeout() {
   
        Integer dt = (Integer)AccessController.doPrivileged(
                new GetIntegerAction("sun.awt.datatransfer.timeout"));
        if (dt == null || dt <= 0) {
   
            return DEFAULT_DATATRANSFER_TIMEOUT;
        } else {
   
            return dt;
        }
    }

    /**
     * Returns true if the native GTK libraries are capable of being
     * loaded and are expected to work properly, false otherwise.  Note
     * that this method will not leave the native GTK libraries loaded if
     * they haven't already been loaded.  This allows, for example, Swing's
     * GTK L&F to test for the presence of native GTK support without
     * leaving the native libraries loaded.  To attempt long-term loading
     * of the native GTK libraries, use the loadGTK() method instead.
     */
    @Override
    public boolean isNativeGTKAvailable() {
   
        synchronized (GTK_LOCK) {
   
            if (nativeGTKLoaded != null) {
   
                // We've already attempted to load GTK, so just return the
                // status of that attempt.
                return nativeGTKLoaded;

            } else if (nativeGTKAvailable != null) {
   
                // We've already checked the availability of the native GTK
                // libraries, so just return the status of that attempt.
                return nativeGTKAvailable;

            } else {
   
                boolean success = check_gtk(getEnabledGtkVersion().getNumber());
                nativeGTKAvailable = success;
                return success;
            }
        }
    }

    /**
     * Loads the GTK libraries, if necessary.  The first time this method
     * is called, it will attempt to load the native GTK library.  If
     * successful, it leaves the library open and returns true; otherwise,
     * the library is left closed and returns false.  On future calls to
     * this method, the status of the first attempt is returned (a simple
     * lightweight boolean check, no native calls required).
     */
    public boolean loadGTK() {
   
        synchronized (GTK_LOCK) {
   
            if (nativeGTKLoaded == null) {
   
                nativeGTKLoaded = load_gtk(getEnabledGtkVersion().getNumber(),
                        isGtkVerbose());
            }
        }
        return nativeGTKLoaded;
    }

    /**
     * Overridden to handle GTK icon loading
     */
    protected Object lazilyLoadDesktopProperty
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梦里逆天

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值