文章目录
1、下载
文件下载:https://www.graalvm.org/downloads/
安装教程:https://www.graalvm.org/22.3/docs/getting-started/
官方github:https://github.com/graalvm/,https://github.com/oracle/graal
下载下边两个文件
graalvm-ce-java17-windows-amd64-22.3.0.zip # 包含openjdk的作用
native-image-installable-svm-java17-windows-amd64-22.3.0.jar # 把java编译成二进制工具
2、graalvm安装
graalvm-ce-java17-windows-amd64-22.3.0.zip解压即可用
3、native-image工具安装
3.1 安装native-image
native-image支持本地安装(native-image-installable-svm-java17-windows-amd64-22.3.0.jar)和在线安装。
本地安装
gu -L install ../../native-image-installable-svm-java17-windows-amd64-22.3.0.jar
在线安装命令
gu install native-image
安装完成后,C:\Program Files\Java\graalvm\graalvm-ce-java17-22.3.0\bin目录会有相关native-image的命令和文件
native-image查看版本
native-image --version
设置环境变量
GRAALVM_HOME=C:\Program Files\Java\graalvm\graalvm-ce-java17-22.3.0
# 把GRAALVM_HOME加到PATH变量后边
PATH=........;%GRAALVM_HOME%\bin
3.2 安装C++编译工具
native-image编译java为二进制,需要依赖C++编译工具,安装 vs2019, 选中 “使用 C++ 的桌面开发”,可选组件中选择 “VS 2019 C++ x64/x86” 生成工具和 “Windows 10 SDK”
配置VC环境变量1
MSVC=C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133
PATH=.........;%MSVC%\bin\HostX64\x64
如果不配置,可能会出现下边错误
# 出错1
Error: Default native-compiler executable 'cl.exe' not found via environment variable PATH
Error: To prevent native-toolchain checking provide command-line option -H:-CheckToolchain
Error: Use -H:+ReportExceptionStackTraces to print stacktrace of underlying exception
配置VC环境变量2
WK10_INCLUDE=C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0
WK10_LIB=C:\Program Files (x86)\Windows Kits\10\Lib\10.0.19041.0
INCLUDE=%WK10_INCLUDE%\ucrt;%WK10_INCLUDE%\um;%WK10_INCLUDE%\shared;%MSVC%\include;
LIB=%WK10_LIB%\um\x64;%WK10_LIB%\ucrt\x64;%MSVC%\lib\x64;
# 出错2
Error: Error compiling query code (in C:\Users\penngo\AppData\Local\Temp\SVM-1701580662269299485\AMD64LibCHelperDirectives.c). Compiler command ''C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\bin\Hostx64\x64\cl.exe' /WX /W4 /wd4244 /wd4245 /wd4800 /wd4804 /wd4214 '/FeC:\Users\penngo\AppData\Local\Temp\SVM-1701580662269299485\AMD64LibCHelperDirectives.exe' 'C:\Users\penngo\AppData\Local\Temp\SVM-1701580662269299485\AMD64LibCHelperDirectives.c'' output included error: [AMD64LibCHelperDirectives.c, C:\Users\penngo\AppData\Local\Temp\SVM-1701580662269299485\AMD64LibCHelperDirectives.c(1): fatal error C1034: stdio.h: 不包括路径集]
如果不使用上边的环境变量配置,也可以在构建前使用vs2019的命令来设置环境变量,可以在命令行执行下边的命令来初始化变量
call "D:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvars64.bat"
4、java编译成二进制exe
4.1、普通的java命令行应用
Main.java
package com.penngo.gralvm;
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>graalvm_test</artifactId>
<groupId>com.penngo.gralvm</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>console</artifactId>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
</project>
native_build.bat
SET class_path=target/console-1.0.jar;
native-image -classpath %class_path% com.penngo.gralvm.Main
编译结果
运行exe
4.2、Swing应用编译
MainSwing.java
package com.penngo.gralvm;
import javax.swing.*;
public class MainSwing {
public static void main(String[] args) {
JFrame jFrame = new JFrame("title");
JButton button = new JButton("Test button");
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.add(button);//把button添加到JFrame中
jFrame.setSize(300,300);//设置JFrame大小
jFrame.setVisible(true);//设置可见,不然的话看不到
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>graalvm_test</artifactId>
<groupId>com.penngo.gralvm</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>swing</artifactId>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
</dependencies>
</project>
直接用上边4.1的方式来编译二进制也是成功的,但是运行会报错
Exception in thread "main" java.lang.NoSuchMethodError: java.awt.Toolkit.getDefaultToolkit()Ljava/awt/Toolkit;
at org.graalvm.nativeimage.builder/com.oracle.svm.core.jni.functions.JNIFunctions$Support.getMethodID(JNIFunctions.java:1259)
at org.graalvm.nativeimage.builder/com.oracle.svm.core.jni.functions.JNIFunctions$Support.getMethodID(JNIFunctions.java:1244)
at org.graalvm.nativeim