基础环境
- 准备graalvm-jdk-21.0.1+12.1(这个就是JDK) 下载网址
下载后配置JDK环境
// 环境变量配置
GRAALVM_HOME = E:\Java\graalvm-jdk-21.0.1+12.1
JAVA_HOME = %GRAALVM_HOME%
path 追加 %JAVA_HOME%\bin;%JAVA_HOME%\jre\bin
Visual Studio 2022
GRAALVM 要求 Window系统必须安装这个东西
相关要求打开这里跟着官方步骤去走Native Image
- graalvm-jdk-21.0.1+12.1无需安装Native Image
- 只需在cmd 敲 native-image --version 出现下图就说明已经安装好
- 如果步骤2是失败的,去看看JAVA_HOME是否未配置好
创建SpringBoot项目
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot 21</description>
<properties>
<java.version>21</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
HelloWorld.java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@GetMapping("/hello")
public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {
return String.format("Hello %s!", name);
}
}
打包
# 在idea控制台 或在 x64 Native Tools Command Prompt for VS 2022 执行下面
mvn -Pnative -DskipTests native:compile
问题分析:
- Executing: E:\Java\graalvm-jdk-21.0.1+12.1\bin\native-image.cmd @target\tmp\native-image-15861334717348583567.args returned non-zero result
- 出现这种问题可以翻开上面的日志,如果是 fatal error C1083: stdio.h xxx No such file or directory,说明是vs未配置好
在环境变量添加下列变量
# 根据你自己的vs路径
MSVC = C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.32.31326
# 随意命名
WK10_INCLUDE = C:\Program Files (x86)\Windows Kits\10\Include\10.0.20348.0
WK10_LIB = C:\Program Files (x86)\Windows Kits\10\Lib\10.0.20348.0
# INCLUDE和LIB,不能随意命名
INCLUDE = %WK10_INCLUDE%\ucrt;%WK10_INCLUDE%\um;%WK10_INCLUDE%\shared;%MSVC%\include
LIB = %WK10_LIB%\um\x64;%WK10_LIB%\ucrt\x64;%MSVC%\lib\x64
# 添加path中指定 MSVC cl.exe路径
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.32.31326\bin\Hostx64\x64
2.不想这么麻烦配置,可以直接在x64 Native Tools Command Prompt for VS 2022执行