Quick start to GraalVM

This post is a TLDR; of Getting started with GraalVM

Finally Java can go real native without the bloat of commerical “native” compilers like ExcelsiorJet or Launchers like Launch4J. Meet GraalVM. With native-image you can AOT (Ahead of time) compile your java code to native executable. When I mean native executable, I mean real native executable unlike Launch4j which embeds a jre or prompts you to get one.

Here is a quick HelloWorld to get started.
Since it is a quick HelloWorld, we dont want to rock the boat by messing up our existing JDK installation. So we will use docker in Windows.

docker run -it -v C:/graalexp:/home/graalexp oracle/graalvm-ce:19.2.0.1 bash
docker run -it -v C:/graalexp:/home/graalexp oracle/graalvm-ce:19.2.0.1 bash
docker run -it -v C:/graalexp:/home/graalexp oracle/graalvm-ce:19.2.0.1 bash

Enter fullscreen mode Exit fullscreen mode

C:/graalexp – is your windows mount. [ Remember to share your C drive with docker in settings]

With a notepad write a HelloWorld.java in C:/graalexp

public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, Graal!");
}
}
public class HelloWorld {
  public static void main(String[] args) {
    System.out.println("Hello, Graal!");
  }
}
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, Graal!"); } }

Enter fullscreen mode Exit fullscreen mode

Install native-image in the docker container as its not installed by default in graalvm.

gu install native-image
gu install native-image
gu install native-image

Enter fullscreen mode Exit fullscreen mode

Now compile it to native image. The native image will run on linux targets. If you want native executables to run on windows, its a bit different than the steps listed here.

javac HelloWorld.java
native-image HelloWorld
./helloworld
Hello, Graal!
javac HelloWorld.java
native-image HelloWorld
./helloworld
Hello, Graal!
javac HelloWorld.java native-image HelloWorld ./helloworld Hello, Graal!

Enter fullscreen mode Exit fullscreen mode

Thats it.
Note: Graal has commerical as well as community editions. We are using community edition here.

原文链接:Quick start to GraalVM

© 版权声明
THE END
喜欢就支持一下吧
点赞14 分享
No matter what happened in the past, you have to believe that the best is yet to come.
无论过去发生过什么,你都要相信,最好的尚未到来
评论 抢沙发

请登录后发表评论

    暂无评论内容