-
An advanced JDK with ahead-of-time Native Image compilation.
-
GraalVM compiles your Java applications ahead of time into standalone binaries. These binaries are smaller, start up to 100x faster, provide peak performance with no warmup, and use less memory and CPU than applications running on a Java Virtual Machine (JVM).
在编译期,将字节码转换为机器码。
GraalVM reduces the attack surface of your application. It excludes unused classes, methods, and fields from the application binary. It restricts reflection and other dynamic Java language features to build time only. It does not load any unknown code at run time.
在编译器,裁剪、消除掉应用程序在运行时不需要、不使用、不加载的类和代码,减少目标应用程序的大小。类似gcc/clang提供的编译选项
-ffunction-sections -fdata-sections -Wl,-gc-sections
。
Getting Started
- Getting Started
GraalVM compiles your Java applications ahead of time into standalone binaries that start instantly, provide peak performance with no warmup, and use fewer resources.
Native Image
-
创建验证项目,执行如下命令:
mvn archetype:generate -DgroupId=com.example -DartifactId=helloworld -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
构建可执行的jar,修改项目的
pom.xml
,增加如下配置:<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.12.1</version> <configuration> <fork>true</fork> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.3.0</version> <configuration> <archive> <manifest> <mainClass>com.example.App</mainClass> <addClasspath>true</addClasspath> </manifest> </archive> </configuration> </plugin> </plugins> </build>
使用graalvm转换为本地代码,修改
pom.xml
,增加如下配置:<profiles> <profile> <id>native</id> <build> <plugins> <plugin> <groupId>org.graalvm.buildtools</groupId> <artifactId>native-maven-plugin</artifactId> <version>${native.maven.plugin.version}</version> <extensions>true</extensions> <executions> <execution> <id>build-native</id> <goals> <goal>compile-no-fork</goal> </goals> <phase>package</phase> </execution> <execution> <id>test-native</id> <goals> <goal>test</goal> </goals> <phase>test</phase> </execution> </executions> </plugin> </plugins> </build> </profile> </profiles>
启动构建,执行如下命令:
mvn -Pnative package
Build
-
Build Overview
native-image
的常见命令行样例。 -
Build Output
执行native-image
构建时,命令行的输出信息。 -
Build Configuration
在jar中增加native-image
的配置文件,用于指示构建选项,配置文件的位置,样例如下:META-INF/ └── native-image └── groupID └── artifactID └── native-image.properties
groupID
/artifactID
如何确定?
通过学习官方提供的样例,快速上手native-image
的使用方法。 -
Bundles
对于使用maven管理、构建的项目,修改项目的pom.xml
,增加如下配置:<plugin> <groupId>org.graalvm.buildtools</groupId> <artifactId>native-maven-plugin</artifactId> <configuration> <buildArgs combine.children="append"> <buildArg>--bundle-create</buildArg> </buildArgs> </configuration> </plugin>
构建软件时,执行如下命令:
./mvnw package -Dpackaging=native-image
-
Command-line Options
native-image
的选项。
Reachability Metadata
Optimizations and Performance
Debugging and Diagnostics
Dynamic Features
Interoperability with Native Code
Reference Manuals
Java and JVM
Java on Truffle
- Java on Truffle
- Interoperability with Truffle Languages
- Implementation Details
Java on Truffle is a minified Java VM that implements all core components of JVM.
- Enhanced HotSwap Capabilities
- Running Demo Applications
- FAQ