系统环境
版本:JDK1.8.0_211
创建Java项目
使用IDEA新建Java项目;
将源码文件src.zip解压到新建项目的src目录下;
移除项目与Java安装目录下源码压缩文件的关联,在Project Structure的SDKs中创建一个新的JDK,仍然使用Java安装目录的Classpath,但要将Sourecepath中的src.zip修改为项目中的source目录。
在Project Structure的Modules的Dependencies中,将Module SDK改为新创建的JDK;
创建Main类,包含main方法即可。
运行Main类中main方法
运行发现以下问题:
系统资源不足,抛出OutOfMemoryError;
程序包UNIXToolkit和FontConfigManager不存在;
找不到com.sun.tools.xx等类。
解决方案
针对资源不足问题,在Settings -> Build, Excution, Deployment -> Compiler中修改Build process heap size为1200即可。
程序包不存在,需要在OpenJDK找到与当前版本对应的这两个.java文件,在项目的源代码目录下创建sun.awt包和sun.font包,分别在包中创建这两个文件,将代码复制到文件中即可。jdk1.8u对应代码如下:
/*
* 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(String name) {
if (name.startsWith("gtk.icon.")) {
return lazilyLoadGTKIcon(name);
}
return super.lazilyLoadDesktopProperty(name);
}
/**
* Load a native Gtk stock icon.
*
* @param longname a desktop property name. This contains icon name, size
* and orientation