GraalVM Native Image Error: Visual Studio 2022 Requirement Not Met
Asked Answered
P

5

8

I'm trying to create a native image from a simple Java program using GraalVM on my Windows 10 machine. The Java program, Hello.java, is a basic "Hello World" print statement.

I followed these steps:

  1. Created Hello.java with a "Hello World" print statement.
  2. Compiled it using javac Hello.java, which resulted in a Hello.class file.
  3. Attempted to create a native image with the command:

native-image Hello

However, I encountered the following error:

Error: On Windows, GraalVM Native Image for JDK 21 requires Visual Studio 2022 version 17.1.0 or later (C/C++ Optimizing Compiler Version 19.31 or later).
Compiler info detected: cl.exe (microsoft, x64, 19.29.30152)
Error: To prevent native-toolchain checking provide command-line option -H:-CheckToolchain

error

I already have Visual Studio Community 2022 installed. What could be causing this error, and how can I resolve it? Are there any specific configurations or additional steps needed to make GraalVM recognize Visual Studio 2022 correctly?

Additional information:

  • System variebles/Path/ C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\
  • openjdk version "21" 2023-09-19
  • OpenJDK Runtime Environment GraalVM CE 21+35.1 (build 21+35-jvmci-23.1-b15)

visualStudio

Pongid answered 12/10, 2023 at 13:17 Comment(0)
G
6

I was just in a similar boat of attempting to run a simple "Hello World" app with GraalVM(graalvm-jdk-21.0.1+12.1) and even though I already had the latest Visual Studio 2022 installed, I was still running into the exact error you posted above.

I was eventually able to resolve the issue using this same solution in this older answer here.

TL;DR: A workaround to ensure the build toolchain is always added is to modify the beginning of your native-image.cmd file (found in in %JAVA_HOME%\bin) with a call to to VS 2022's vcvars64.bat, e.g.: call "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars64.bat" > nul

So the final version of my native-image.cmd file looked like this:

@echo off

call "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars64.bat" > nul
call :getScriptLocation location
set "GRAALVM_ARGUMENT_VECTOR_PROGRAM_NAME=%~0"
"%location%..\lib\svm\bin\native-image.exe" %*
exit /b %errorlevel%

:: If this script is in `%PATH%` and called quoted without a full path (e.g., `"js"`), `%~dp0` is expanded to `cwd`
:: rather than the path to the script.
:: This does not happen if `%~dp0` is accessed in a subroutine.
:getScriptLocation variableName
    set "%~1=%~dp0"
    exit /b 0
Garin answered 22/11, 2023 at 6:46 Comment(2)
I like the idea, but for some reason when I try to implement this, I get lots of Access is denied. complaints (specifically on the added call). Hopefully this works for others.Linkous
Nice. This worksLuciusluck
R
3

I saw similar errors reported by users of Chinese locale. So maybe your cl.exe reports some localized version string which native-image fails to parse. So one idea is to try using en_US locale.

Another thing to try is to skip VS detection, while making sure native-image can access the compiler:

  • Start Command Prompt
  • Run vsvars64.bat inside it to set up compiler paths and environment
  • Run native-image with the -H:-CheckToolchain option
Repeal answered 13/10, 2023 at 2:3 Comment(0)
H
2

I just wanted to add some helpful context around why it doesn't just work out the box.

Part of the complexity is that there are about 20+ environment variables that need to be set for the C++ build tool chain, and Microsoft specifically says:

Path and environment variables for command-line builds

The MSVC command-line tools use the PATH, TMP, INCLUDE, LIB, and LIBPATH environment variables, and also use other environment variables specific to your installed tools, platforms, and SDKs. Even a simple Visual Studio installation may set twenty or more environment variables. This complexity is why we strongly recommend that you use a developer command prompt shortcut or one of the customized command files. We don't recommend you set these variables in the Windows environment yourself.

https://learn.microsoft.com/en-us/cpp/build/building-on-the-command-line?view=msvc-170

Therefore, the suggested route is to launch your IDE from the specific command prompt that you want. "x64 Native Tools Command Prompt for VS 2022".

You could also just create a batch file that runs VCVars (which sets up the environment variables) and then runs the IDE:

I am using IntelliJ, so it might look something like this:

Launch-IntelliJ-with-Native-Build-Tools.bat:

call "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars64.bat"
cd "C:\Program Files\JetBrains\IntelliJ IDEA 2023.2.5\bin"
idea64.exe
Haire answered 18/8, 2024 at 12:15 Comment(0)
I
0

I had the same issue when using Visual Studio 2022

However, if I also installed the Visual Studio Build Tools 2022, the problem went away.

Interchange answered 19/6, 2024 at 11:15 Comment(0)
C
0

Sorry for resurrecting this topic, I was having this problem just now and I can solve it by following the steps below:

  1. I removed all old versions of visual studio (I had versions 2017 and 2019)
  2. I repaired visual studio 2022
  3. I restarted my computer (Windows 11 23H2 22631.4169)

These steps worked for me using GraalVM JDK 21.0.4 and Quarkus 3.14.2. I hope these steps help someone :)

Coldiron answered 23/9, 2024 at 13:0 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.