24

I'm trying to build a native application using GraalVM and native-image. An error occures when starting the build process. It seems the cl.exe is missing in the classpath. Like mentioned on the GraalVM website, i've installed the "GRMSDKX_EN_DVD.iso" (Windows SDK for Windows 7 and .NET). I've also tried installing the Windows SDK for Windows 10.

C:\Program Files (x86)\Microsoft Visual Studio\2019\Community>native-image -jar C:\Users\tm\Desktop\DemoGraalVM\target\Demo-1.0-SNAPSHOT.jar
[Demo-1.0-SNAPSHOT:28776]    classlist:   2,435.83 ms,  1.16 GB
[Demo-1.0-SNAPSHOT:28776]        setup:     646.59 ms,  1.16 GB
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
Error: Image build request failed with exit status 1

I'm using windows 10, graalvm-ce-java8-windows-amd64-20.2.0. I'm also running Visual Studio Community Edition and use the Visual Studio 2019 Developer Command Prompt v16.3.1. The Java application is build in IntelliJ Community Edition using Maven.

How can I fix this? Where do I download the cl.exe or which installation package contains this file?

tmmls
  • 510
  • 1
  • 4
  • 16

5 Answers5

28

It looks like you're missing some dependencies, so I'll do my best to clear the issue up for you.

To build a native GraalVM image on Windows, you'll need Microsoft Visual C++ (MSVC). The version required depends on the JDK version that your GraalVM distribution is based on.

  • For a GraalVM distribution using Java 8, you'll need MSVC 2010 SP1

    The recommended installation method is using Microsoft Windows SDK 7.1:

    1. Download the SDK file GRMSDKX_EN_DVD.iso for from Microsoft.
    2. Mount the image by opening F:\Setup\SDKSetup.exe directly.

    According to the GraalVM native-image reference, you can get this easily via the Window SDK 7.1 image from Microsoft

  • For a GraalVM distribution using Java 11, you need MSVC 2017 15.5.5 or later.
    I would definitely recommend just going with the latest version (currently 2019) unless you already use another version in your workflow. You can get it here.


Once you've installed Visual Studio, all of your build commands should be run through the Native Tools Command Prompt.
If you keep the default Start Menu shortcuts while installing Visual Studio, this will be accessible at:
Start -> Visual Studio 2019 -> Tools -> x64 Native Tools Command Prompt

Given that your code is in order, using the proper toolchain will resolve your issues.

E_net4
  • 27,810
  • 13
  • 101
  • 139
Zach
  • 586
  • 3
  • 12
  • Hey @Zach. I followed all the instructions and I am getting a creepy error which I cannot find anywhere. Can you please help me? – Dragos Roban Feb 25 '21 at 11:44
  • 1
    Hi @DragosRoban I just saw this notification, I may be able to help. If you'd like to make a question and link to it, or DM me, either way I'll do what I can. I recommend doing the prior and mentioning you followed the instructions here, that way we add to the overall knowledge base for everyone by solving your problem! – Zach Feb 27 '21 at 19:09
  • Wow, this explanation is better than the original documentation. – SedJ601 Aug 26 '22 at 03:28
7

Quick Guide

  1. Install Visual Studio (Microsoft Visual C++ must be selected)
  2. Download GraalVM and extract.
  3. Set env GRAALVM_HOME as where the graalvm located.
  4. Edit %GRAALVM_HOME%\bin\native-image.cmd
  5. Add following line to the very top

call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvarsx86_amd64.bat"

Note: vcvarsx86_amd64.bat location may be different depending on your VS version.

Gnosis00
  • 309
  • 6
  • 19
  • Unfortunately calling vcvarsx86_amd64.bat from native-image.cmd messes up the response of native-image.cmd --version command. It returns a Visual Studio banner along with VM version and native-maven-plugin can't parse that version. You should therefore output the stdout to null device. call "C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Auxiliary\Build\vcvarsx86_amd64.bat" >nul 2>&1 – bluelurker Mar 17 '23 at 22:36
  • @Gnosis00 I do not have visual studio install on my windows but I am getting similat error while running the native build command execution? Any alternate solution? – Kunal Varpe Apr 01 '23 at 16:59
3

(Thanks fo @Gnosis00's and @Zach's solution. It almost worked for me. Here is an improved version that makes native builds also from within IDEA IntelliJ possible.)

Modify native-image.cmd (in your %JAVA_HOME%\bin directory) with a text editor such that it starts with

@echo off

call "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars64.bat" > nul

This automatically adds the needed build toolchain every time native-build is invoked. You don't have to start it from Visual Studio's Native Tools Command Prompt anymore.

Please be aware of the ending > nul to suppress any output of vcvars64.bat. Otherwise, the version detection of GraalVM by Spring Boot's Gradle build scripts will fail.

This solution is tested with Microsoft Visual Studio 2022 CE, IDEA IntelliJ 2022.1.3 and graalvm-ce-java17-windows-amd64-22.3.1.

2

After 2 years struggling I gave up on trying to use GraalVM to build native executables. I now use the jPackage command line tool. It comes by default with the latest versions of Java. Works like a charm. No hassle, no additional dependencies, no errors, free, ...

More info: https://youtu.be/ZGW9AalZLN4

tmmls
  • 510
  • 1
  • 4
  • 16
  • On Windows, this is not completely true. jPackage depends on the wixtoolset, while wix in turn depends on .Net. At the end, you can build executables with jPackage, but not native ones. This misses out on fast startup and low memory usage. I do agree building native executables is a struggle. – user2209562 Jun 01 '23 at 18:54
  • @user2209562 Can jPackage build a native app inluding WIX and/or NET components ? – tmmls Jun 15 '23 at 13:01
0

You'll need to set up the environment for Microsoft Visual C++.

I'm using Visual Studio 2017, so I have to use: C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvarsx86_amd64.bat

Zach
  • 586
  • 3
  • 12
jdevp2
  • 371
  • 5
  • 15
  • 1
    @jdevp2 When answering questions, you should provide a **clear overview** of the process or solution. Also, *try to avoid jargon and acronyms without elucidation* -- the goal here is to answer questions, not create new ones! Finally, it's best **not** to base your examples on legacy or niche workflows and tools unless directly required by the question. The best answers apply to as many people in addition to the original asker as possible. Remember, StackOverflow is as much a collaborative knowledge base as a Q&A platform! – Zach Oct 09 '20 at 18:45
  • @tmmls Yes, because he's using Visual Studio 2017 and you're using the current version. See my answer for instructions for VS2019. Good luck! – Zach Nov 03 '20 at 03:51