You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用GraalVM构建Java11原生镜像后运行报错:sun.awt.X11.XToolkit未找到

GraalVM构建Java 11原生镜像运行报错:Toolkit not found: sun.awt.X11.XToolkit

问题详情

我正在为一个Java 11项目使用GraalVM(java11-linux-gluon-22.1.0.1-Final)构建native-image。该项目原本基于Java 1.8,适配GraalVM转为Java 11后构建成功,但运行程序时出现如下错误:

Exception in thread "main" java.awt.AWTError: Toolkit not found: sun.awt.X11.XToolkit
    at java.awt.Toolkit$2.run(Toolkit.java:595)
    at java.awt.Toolkit$2.run(Toolkit.java:583)
    at java.security.AccessController.executePrivileged(AccessController.java:169)
    at java.security.AccessController.doPrivileged(AccessController.java:83)
    at java.awt.Toolkit.getDefaultToolkit(Toolkit.java:582)
    at java.awt.Toolkit.getEventQueue(Toolkit.java:1494)
    at java.awt.EventQueue.invokeLater(EventQueue.java:1312)
    at javax.swing.SwingUtilities.invokeLater(SwingUtilities.java:1421)
    at com.asd.pos.main.MainClass.main(MainClass.java:28)

项目pom.xml配置

<properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>11.0.2</version>
        </dependency>
        <dependency>
            <groupId>groupId</groupId>
            <artifactId>artifactId</artifactId>
            <version>1.0</version>
            <scope>system</scope>
            <systemPath>/home/yousef66/Desktop/pos2.jar</systemPath>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>com.gluonhq</groupId>
                <artifactId>gluonfx-maven-plugin</artifactId>
                <version>1.0.15</version>
                <configuration>
                    <mainClass>com.asd.pos.main.MainClass</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>

成功的构建日志

[INFO] Scanning for projects...
[WARNING] 
[WARNING] Some problems were encountered while building the effective model for org.example:demo12:jar:1.0-SNAPSHOT
[WARNING] 'dependencies.dependency.systemPath' for groupId:artifactId:jar should use a variable instead of a hard-coded path /home/yousef66/Desktop/pos2.jar @ line 27, column 25
[WARNING] 
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING] 
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING] 
[INFO] 
[INFO] -------------------------< org.example:demo12 >-------------------------
[INFO] Building demo12 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- gluonfx-maven-plugin:1.0.15:build (default-cli) @ demo12 ---
[INFO] Scanning for projects...
[WARNING] 
[WARNING] Some problems were encountered while building the effective model for org.example:demo12:jar:1.0-SNAPSHOT
[WARNING] 'dependencies.dependency.systemPath' for groupId:artifactId:jar should use a variable instead of a hard-coded path /home/yousef66/Desktop/pos2.jar @ line 27, column 25
[WARNING] 
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING] 
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING] 
[INFO] 
[INFO] -------------------------< org.example:demo12 >-------------------------
[INFO] Building demo12 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] >>> gluonfx-maven-plugin:1.0.15:compile (default-cli) > process-classes @ demo12 >>>
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ demo12 ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ demo12 ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 710 source files to /home/yousef66/Desktop/demo12/target/classes
[WARNING] /home/yousef66/Desktop/demo12/src/main/java/com/asd/pos/ui/dialog/StockAddDialog.java: Some input files use or override a deprecated API.
[WARNING] /home/yousef66/Desktop/demo12/src/main/java/com/asd/pos/ui/dialog/StockAddDialog.java: Recompile with -Xlint:deprecation for details.
[WARNING] /home/yousef66/Desktop/demo12/src/main/java/com/asd/pos/ui/frame/InvoiceWorkListFrame.java: Some input files use unchecked or unsafe operations.
[WARNING] /home/yousef66/Desktop/demo12/src/main/java/com/asd/pos/ui/frame/InvoiceWorkListFrame.java: Recompile with -Xlint:unchecked for details.
[INFO] 
[INFO] <<< gluonfx-maven-plugin:1.0.15:compile (default-cli) < process-classes @ demo12 <<<
[INFO] 
[INFO] 
[INFO] --- gluonfx-maven-plugin:1.0.15:compile (default-cli) @ demo12 ---
[Wed Sep 14 10:25:16 EEST 2022][INFO] ==================== COMPILE TASK ====================
             _______  ___      __   __  _______  __    _
            |       ||   |    |  | |  ||       ||  |  | |
            |    ___||   |    |  | |  ||   _   ||   |_| |
            |   | __ |   |    |  |_|  ||  | |  ||       |
            |   ||  ||   |___ |       ||  |_|  ||  _    |
            |   |_| ||       ||       ||       || | |   |
            |_______||_______||_______||_______||_|  |__|

    Access to the latest docs, tips and tricks and more info on
    how to get support? Register your usage of Gluon Substrate now at

    https://gluonhq.com/activate



[Wed Sep 14 10:25:17 EEST 2022][INFO] We will now compile your code for x86_64-linux-linux. This may take some time.
[Wed Sep 14 10:25:20 EEST 2022][INFO] [SUB] Warning: Ignoring server-mode native-image argument --no-server.
[Wed Sep 14 10:25:25 EEST 2022][INFO] [SUB] ========================================================================================================================
[Wed Sep 14 10:25:25 EEST 2022][INFO] [SUB] GraalVM Native Image: Generating 'com.asd.pos.main.mainclass' (shared library)...
[Wed Sep 14 10:25:25 EEST 2022][INFO] [SUB] ========================================================================================================================
[Wed Sep 14 10:25:26 EEST 2022][INFO] [SUB] [GluonFeature] enabled for config com.oracle.svm.hosted.FeatureImpl$IsInConfigurationAccessImpl@544e3679
[Wed Sep 14 10:25:27 EEST 2022][INFO] [SUB] GluonFeature enabled in setup com.oracle.svm.hosted.FeatureImpl$DuringSetupAccessImpl@2a7bc16b
[Wed Sep 14 10:25:28 EEST 2022][INFO] [SUB] [1/7] Initializing...                                                                                    (7.9s @ 0.21GB)
[Wed Sep 14 10:25:28 EEST 2022][INFO] [SUB]  Version info: 'GraalVM 22.1.0.1 Java 11 CE'
[Wed Sep 14 10:25:28 EEST 2022][INFO] [SUB]  C compiler: gcc (linux, x86_64, 9.4.0)
[Wed Sep 14 10:25:28 EEST 2022][INFO] [SUB]  Garbage collector: Serial GC
[Wed Sep 14 10:25:28 EEST 2022][INFO] [SUB]  1 user-provided feature(s)
[Wed Sep 14 10:25:28 EEST 2022][INFO] [SUB]   - com.gluonhq.substrate.feature.GluonFeature
[Wed Sep 14 10:26:52 EEST 2022][INFO] [SUB] [2/7] Performing analysis...  [*********]                                                               (82.8s @ 4.77GB)
[Wed Sep 14 10:26:52 EEST 2022][INFO] [SUB]   20,249 (90.12%) of 22,468 classes reachable
[Wed Sep 14 10:26:52 EEST 2022][INFO] [SUB]   48,519 (72.63%) of 66,803 fields reachable
[Wed Sep 14 10:26:52 EEST 2022][INFO] [SUB]  109,347 (59.16%) of 184,835 methods reachable
[Wed Sep 14 10:26:52 EEST 2022][INFO] [SUB]      658 classes,    13 fields, and 2,380 methods registered for reflection
[Wed Sep 14 10:26:52 EEST 2022][INFO] [SUB]      119 classes,   159 fields, and   184 methods registered for JNI access
[Wed Sep 14 10:27:06 EEST 2022][INFO] [SUB] [3/7] Building universe...                                                                              (14.1s @ 4.20GB)
[Wed Sep 14 10:27:11 EEST 2022][INFO] [SUB] [4/7] Parsing methods...      [***]                                                                      (5.1s @ 5.20GB)
[Wed Sep 14 10:27:25 EEST 2022][INFO] [SUB] [5/7] Inlining methods...     [******]                                                                  (13.5s @ 2.31GB)
[Wed Sep 14 10:28:09 EEST 2022][INFO] [SUB] [6/7] Compiling methods...    [******]                                                                  (43.7s @ 4.15GB)
[Wed Sep 14 10:28:22 EEST 2022][INFO] [SUB] 
[Wed Sep 14 10:28:22 EEST 2022][INFO] [SUB] ------------------------------------------------------------------------------------------------------------------------
[Wed Sep 14 10:28:22 EEST 2022][INFO] [SUB]                        35.0s (19.2% of total time) in 88 GCs | Peak RSS: 9.44GB | CPU load: 6.15
[Wed Sep 14 10:28:22 EEST 2022][INFO] [SUB] ------------------------------------------------------------------------------------------------------------------------
[Wed Sep 14 10:28:22 EEST 2022][INFO] [SUB] Produced artifacts:
[Wed Sep 14 10:28:22 EEST 2022][INFO] [SUB]  /home/yousef66/Desktop/demo12/target/gluonfx/x86_64-linux/gvm/demo12/graal_isolate.h (header)
[Wed Sep 14 10:28:22 EEST 2022][INFO] [SUB]  /home/yousef66/Desktop/demo12/target/gluonfx/x86_64-linux/gvm/demo12/com.asd.pos.main.mainclass.h (header)
[Wed Sep 14 10:28:22 EEST 2022][INFO] [SUB]  /home/yousef66/Desktop/demo12/target/gluonfx/x86_64-linux/gvm/demo12/graal_isolate_dynamic.h (header)
[Wed Sep 14 10:28:22 EEST 2022][INFO] [SUB]  /home/yousef66/Desktop/demo12/target/gluonfx/x86_64-linux/gvm/demo12/com.asd.pos.main.mainclass_dynamic.h (header)
[Wed Sep 14 10:28:22 EEST 2022][INFO] [SUB]  /home/yousef66/Desktop/demo12/target/gluonfx/x86_64-linux/gvm/demo12/com.asd.pos.main.mainclass.build_artifacts.txt
[Wed Sep 14 10:28:22 EEST 2022][INFO] [SUB] ========================================================================================================================
[Wed Sep 14 10:28:22 EEST 2022][INFO] [SUB] Finished generating 'com.asd.pos.main.mainclass' in 3m 0s.
[INFO] 
[INFO] --- gluonfx-maven-plugin:1.0.15:link (default-cli) @ demo12 ---
[Wed Sep 14 10:28:22 EEST 2022][INFO] ==================== LINK TASK ====================
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  03:16 min
[INFO] Finished at: 2022-09-14T10:28:24+03:00
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  03:18 min
[INFO] Finished at: 2022-09-14T10:28:24+03:00
[INFO] ------------------------------------------------------------------------

解决办法

  1. 错误原因:GraalVM原生镜像构建时会做静态分析,未被检测到的类会被剔除。你的代码用到了Swing组件,但AWT相关的X11 Toolkit类没被自动包含进镜像。
  2. 修改插件配置:在pom.xml的gluonfx-maven-plugin的configuration节点中添加以下参数,强制GraalVM包含AWT/Swing相关类:
    <options>
        <option>--enable-all-security-services</option>
        <option>-H:IncludeResources=java.awt.*</option>
        <option>-H:ClassInitialization=sun.awt.X11.XToolkit=build-time</option>
        <option>--initialize-at-build-time=sun.awt,java.awt</option>
    </options>
    
  3. 检查系统依赖:确保Linux系统安装了X11相关开发包,比如libx11-dev、libxext-dev、libxtst-dev等,运行时需要这些本地库支持AWT/Swing界面。
  4. 修复pom依赖问题:把硬编码路径的pos2.jar安装到本地Maven仓库,替换掉system作用域的依赖,避免构建不稳定。

内容的提问来源于stack exchange,提问作者Yousef Hamarsheh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 04:45:41