oracle / graal

GraalVM compiles Java applications into native executables that start instantly, scale fast, and use fewer compute resources 🚀
https://www.graalvm.org
Other
20.39k stars 1.64k forks source link

java.lang.NegativeArraySizeException when executing nativeCompile command #6549

Open streamingdv opened 1 year ago

streamingdv commented 1 year ago

Describe the issue Tried to follow your tutorial here and I used the community edition 22.3.2 Java 17 downloaded from here. I navigate to my project in and executed via .\gradlew nativeCompile the Native Tools Command Prompt. I get a java.lang.NegativeArraySizeException exception when I execute the nativeCompile command. I have no idea what could be the problem. The project is a standard JavaFX gradle project.

Steps to reproduce the issue I use the id 'org.graalvm.buildtools.native' version '0.9.21' gradle plugin then I execute the .\gradlew nativeCompile the Native Tools Command Prompt

Describe GraalVM and your environment:

More details The build.gradle of my test project looks like this

plugins {
    id 'application'
    id 'org.openjfx.javafxplugin' version '0.0.13'
    id 'org.graalvm.buildtools.native' version '0.9.21'
}

group 'com.grill.example'
version '1.0.0'

repositories {
    mavenCentral()
    mavenLocal()
    maven { url 'https://jitpack.io' }
    gradlePluginPortal()
}

dependencies {

}
javafx {
    version = '17.0.6'
    modules = ['javafx.graphics', 'javafx.controls', 'javafx.fxml', 'javafx.base']
}

graalvmNative {
    binaries {
        main {
            // Main options
            imageName = 'application' // The name of the native image, defaults to the project name
            mainClass = 'com.grill.example.App' // The main class to use, defaults to the application.mainClass

            jvmArgs.add('--add-opens=javafx.base/com.sun.javafx=ALL-UNNAMED')
            jvmArgs.add('--add-opens=javafx.graphics/com.sun.prism=ALL-UNNAMED')
            jvmArgs.add('--add-opens=javafx.graphics/com.sun.prism.d3d=ALL-UNNAMED')
            jvmArgs.add('--add-opens=javafx.graphics/com.sun.javafx.scene.layout=ALL-UNNAMED')
            jvmArgs.add('--add-opens=javafx.graphics/com.sun.javafx.scene=ALL-UNNAMED')
            jvmArgs.add('--add-opens=javafx.graphics/com.sun.javafx.sg.prism=ALL-UNNAMED')
            jvmArgs.add('--add-opens=javafx.graphics/com.sun.javafx.tk=ALL-UNNAMED')
            jvmArgs.add('--add-opens=javafx.graphics/com.sun.javafx.geom=ALL-UNNAMED')
            jvmArgs.add('--add-opens=javafx.graphics/javafx.scene.image=ALL-UNNAMED')

        }
    }
}

mainClassName = 'com.grill.example.App'

The main class like this

package com.grill.example;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;

import javafx.stage.Stage;

public class App extends Application {

    public static void main(final String[] args) {
        //System.setProperty("prism.order", "d3d,sw");
        System.setProperty("prism.vsync", "false");
        Application.launch(App.class);
    }

    @Override
    public void start(final Stage primaryStage) {
        final Pane dummyPane = new Pane();
        dummyPane.setStyle("-fx-background-color: black");
        final Scene scene = new Scene(dummyPane, 700, 400);
        primaryStage.setScene(scene);
        primaryStage.show();
        primaryStage.setMinWidth(480);
        primaryStage.setMinHeight(360);
    }
}

Command line output

    > Configure project :
Project : => no module-info.java found

> Task :generateResourcesConfigFile
[native-image-plugin] Resources configuration written into D:\GIT\Example_PC\Example\Example\build\native\generated\generateResourcesConfigFile\resource-config.json

> Task :nativeCompile
[native-image-plugin] GraalVM Toolchain detection is disabled
[native-image-plugin] GraalVM location read from environment variable: GRAALVM_HOME
[native-image-plugin] Native Image executable path: C:\Program Files\Java\graalvm-17\bin\native-image.cmd
WARNING: Unknown module: javafx.graphics specified to --add-opens
WARNING: Unknown module: javafx.graphics specified to --add-opens
WARNING: Unknown module: javafx.base specified to --add-opens
WARNING: Unknown module: javafx.graphics specified to --add-opens
WARNING: Unknown module: javafx.graphics specified to --add-opens
WARNING: Unknown module: javafx.graphics specified to --add-opens
WARNING: Unknown module: javafx.graphics specified to --add-opens
WARNING: Unknown module: javafx.graphics specified to --add-opens
WARNING: Unknown module: javafx.graphics specified to --add-opens
=======================================================================================================================
GraalVM Native Image: Generating 'application' (executable)...
========================================================================================================================
[1/7] Initializing...                                                                                   (19.8s @ 0.33GB
 Version info: 'GraalVM 22.3.2 Java 17 CE'
 Java version info: '17.0.7+7-jvmci-22.3-b18'
 C compiler: cl.exe (microsoft, x64, 19.28.29914)
 Garbage collector: Serial GC
 1 user-specific feature(s)
 - com.oracle.svm.thirdparty.gson.GsonFeature
[2/7] Performing analysis...  [*******]                                                                 (79.2s @ 3.85GB
   6,895 (83.05%) of  8,302 classes reachable
   6,955 (50.80%) of 13,691 fields reachable
  30,955 (29.67%) of 104,317 methods reachable
   1,914 classes,    71 fields, and 2,242 methods registered for reflection
   1,780 classes,   125 fields, and 1,589 methods registered for JNI access
       4 native libraries: crypt32, ncrypt, psapi, version
[3/7] Building universe...                                                                               (2.7s @ 3.98GB [4/7] Parsing methods...      [*]                                                                        (1.4s @ 4.38GB [5/7] Inlining methods...     [***]                                                                      (0.7s @ 3.85GB [6/7] Compiling methods...    [***]                                                                     (10.0s @ 3.70GB 
[7/7] Creating image...                                                                                  (0.0s @ 4.58GB
Fatal error: com.oracle.svm.core.util.VMError$HostedError: java.lang.NegativeArraySizeException: -1355857920
        at org.graalvm.nativeimage.builder/com.oracle.svm.core.util.VMError.shouldNotReachHere(VMError.java:72)
        at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.NativeImageGenerator.doRun(NativeImageGenerator.java:696)
        at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.NativeImageGenerator.run(NativeImageGenerator.java:535)
        at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.NativeImageGeneratorRunner.buildImage(NativeImageGeneratorRunner.java:403)
        at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.NativeImageGeneratorRunner.build(NativeImageGeneratorRunner.java:580)
        at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.NativeImageGeneratorRunner.main(NativeImageGeneratorRunner.java:128)
Caused by: java.lang.NegativeArraySizeException: -1355857920
        at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.image.RelocatableBuffer.<init>(RelocatableBuffer.java:51)
        at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.image.NativeImage.build(NativeImage.java:484)
        at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.NativeImageGenerator.doRun(NativeImageGenerator.java:684)
        ... 4 more
------------------------------------------------------------------------------------------------------------------------
                        4.4s (3.6% of total time) in 71 GCs | Peak RSS: 7.25GB | CPU load: 2.76
========================================================================================================================
Failed generating 'application' after 2m 1s.
Error: Image build request failed with exit status 1

> Task :nativeCompile FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':nativeCompile'.
> Process 'command 'C:\Program Files\Java\graalvm-17\bin\native-image.cmd'' finished with non-zero exit value 1

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 2m 7s
5 actionable tasks: 2 executed, 3 up-to-date
brahimhaddou commented 1 year ago

Hi @streamingdv, Thanks for reporting this issue, we will try to reproduce it and get back to you.

brahimhaddou commented 1 year ago

@streamingdv We have not been able to reproduce this issue. image

grill2010 commented 1 year ago

Strange, it still doesn't work on my PC though with the same setup I posted above.

brahimhaddou commented 1 year ago

Can you please try to add -H:TempDirectory flag to the native-image command, to point to dev directory C:\\Users\\<USER>\\dev\\svmtmp, and try again.

graalvmNative {
    binaries {
        main {
            buildArgs.add('-H:TempDirectory=C:\\Users\\<USER>\\dev\\svmtmp')
        }
    }
alawasoft commented 7 months ago

@brahimhaddou @marwan-hallaoui

same issue on my windows machine

`======================================================================================================================== GraalVM Native Image: Generating 'Native-Image' (executable)...

For detailed information and explanations on the build output, visit: https://github.com/oracle/graal/blob/master/docs/reference-manual/native-image/BuildOutput.md

[1/8] Initializing... (10.3s @ 0.36GB) Java version: 22+36, vendor version: Oracle GraalVM 22+36.1 Graal compiler: optimization level: 2, target machine: x86-64-v3, PGO: ML-inferred C compiler: cl.exe (microsoft, x64, 19.39.33523) Garbage collector: Serial GC (max heap size: 80% of RAM) 2 user-specific feature(s):

[8/8] Creating image... [**] (0.0s @ 7.24GB)


## Stack Trace

```java
java.lang.NegativeArraySizeException: -1619337216
    at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.image.RelocatableBuffer.<init>(RelocatableBuffer.java:56)
    at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.image.NativeImage.build(NativeImage.java:468)
    at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.NativeImageGenerator.doRun(NativeImageGenerator.java:699)
    at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.NativeImageGenerator.run(NativeImageGenerator.java:537)
    at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.NativeImageGeneratorRunner.buildImage(NativeImageGeneratorRunner.java:526)
    at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.NativeImageGeneratorRunner.build(NativeImageGeneratorRunner.java:701)
    at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.NativeImageGeneratorRunner.start(NativeImageGeneratorRunner.java:140)
    at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.NativeImageGeneratorRunner.main(NativeImageGeneratorRunner.java:95)

GraalVM Setup

Name Value
Java version 22+36
Vendor version Oracle GraalVM 22+36.1
Runtime version 22+36-jvmci-b02
C compiler cl.exe (microsoft, x64, 19.39.33523)
GraalVM release file IMPLEMENTOR="Oracle Corporation" JAVA_RUNTIME_VERSION="22+36-jvmci-b02" JAVA_VERSION="22" JAVA_VERSION_DATE="2024-03-19" LIBC="default" MODULES="java.base java.logging java.management jdk.internal.vm.ci jdk.unsupported org.graalvm. `
aliiy commented 6 months ago

@brahimhaddou I encountered the same error as you did. During troubleshooting, I found that it was due to several jar packages. Removing them from Maven allowed the exe to be packaged normally. Unfortunately, my main logic relies on these jar packages. The packages I imported are the following three:


       <dependency>
            <groupId>org.bytedeco</groupId>
            <artifactId>javacv-platform</artifactId>
            <version>1.5.10</version>
        </dependency>

        <dependency>
            <groupId>org.bytedeco</groupId>
            <artifactId>ffmpeg-platform</artifactId>
            <version>6.1.1-1.5.10</version>
        </dependency>

        <dependency>
            <groupId>org.bytedeco</groupId>
            <artifactId>javacpp</artifactId>
            <version>1.5.10</version>
        </dependency>

GraalVM Setup

Name Value
Java version 17.0.9+9
Vendor version GraalVM CE 17.0.9+9.1
Runtime version 17.0.9+9-jvmci-23.0-b22
C compiler cl.exe (microsoft, x64, 19.38.33133)
GraalVM release file
IMPLEMENTOR="GraalVM Community"
JAVA_RUNTIME_VERSION="17.0.9+9-jvmci-23.0-b22"
JAVA_VERSION="17.0.9"
JAVA_VERSION_DATE="2023-10-17"
LIBC="default"
MODULES="java.base java.compiler java.instrument java.logging java.management jdk.internal.vm.ci jdk.jfr jdk.unsupported org.graalvm.sdk java.datatransfer java.xml java.prefs java.desktop java.transaction.xa java.sql jdk.management org.graalvm.truffle jdk.internal.vm.compiler com.oracle.graal.graal_enterprise com.oracle.svm.extraimage_enterprise com.oracle.svm.svm_enterprise com.oracle.svm_enterprise.ml_dataset java.security.sasl java.naming java.rmi java.management.rmi java.net.http java.scripting java.security.jgss java.sql.rowset java.xml.crypto java.se java.smartcardio jdk.accessibility jdk.internal.jvmstat jdk.attach jdk.charsets jdk.compiler jdk.crypto.ec jdk.crypto.cryptoki jdk.crypto.mscapi jdk.dynalink jdk.internal.ed jdk.editpad jdk.hotspot.agent jdk.httpserver jdk.incubator.foreign jdk.incubator.vector jdk.internal.le jdk.internal.opt jdk.internal.vm.compiler.management jdk.internal.vm.compiler.truffle.jfr jdk.jartool jdk.javadoc jdk.jcmd jdk.management.agent jdk.jconsole jdk.jdeps jdk.jdwp.agent jdk.jdi jdk.jlink jdk.jpackage jdk.jshell jdk.jsobject jdk.jstatd jdk.localedata jdk.management.jfr jdk.naming.dns jdk.naming.rmi jdk.net jdk.nio.mapmode jdk.random jdk.sctp jdk.security.auth jdk.security.jgss jdk.unsupported.desktop jdk.xml.dom jdk.zipfs org.graalvm.extraimage.builder org.graalvm.js.scriptengine org.graalvm.locator org.graalvm.nativeimage.llvm"
OS_ARCH="x86_64"
OS_NAME="Windows"
SOURCE=".:git:1fcfe930ebdb+ labsjdk-builder:39179710e6adc4516c2d9e8b108e11584578e659 compiler:aeab112b50799393fd379ac14057a09fb518a6e1 graal-js:e2b74f45ce0eb8ae3f9bd24b068f128e91d607e8 java-benchmarks:aeab112b50799393fd379ac14057a09fb518a6e1 regex:aeab112b50799393fd379ac14057a09fb518a6e1 sdk:aeab112b50799393fd379ac14057a09fb518a6e1 substratevm:aeab112b50799393fd379ac14057a09fb518a6e1 tools:aeab112b50799393fd379ac14057a09fb518a6e1 truffle:aeab112b50799393fd379ac14057a09fb518a6e1 vm:aeab112b50799393fd379ac14057a09fb518a6e1"
GRAALVM_VERSION="23.0.2"
COMMIT_INFO={"compiler": {"commit.committer": "Marouane El Hallaoui <marouane.el.hallaoui@oracle.com>", "commit.committer-ts": 1697732165, "commit.rev": "aeab112b50799393fd379ac14057a09fb518a6e1"}, "graal-js": {"commit.committer": "Marouane El Hallaoui <marouane.el.hallaoui@oracle.com>", "commit.committer-ts": 1696419001, "commit.rev": "e2b74f45ce0eb8ae3f9bd24b068f128e91d607e8"}, "java-benchmarks": {"commit.committer": "Marouane El Hallaoui <marouane.el.hallaoui@oracle.com>", "commit.committer-ts": 1697732165, "commit.rev": "aeab112b50799393fd379ac14057a09fb518a6e1"}, "regex": {"commit.committer": "Marouane El Hallaoui <marouane.el.hallaoui@oracle.com>", "commit.committer-ts": 1697732165, "commit.rev": "aeab112b50799393fd379ac14057a09fb518a6e1"}, "sdk": {"commit.committer": "Marouane El Hallaoui <marouane.el.hallaoui@oracle.com>", "commit.committer-ts": 1697732165, "commit.rev": "aeab112b50799393fd379ac14057a09fb518a6e1"}, "substratevm": {"commit.committer": "Marouane El Hallaoui <marouane.el.hallaoui@oracle.com>", "commit.committer-ts": 1697732165, "commit.rev": "aeab112b50799393fd379ac14057a09fb518a6e1"}, "tools": {"commit.committer": "Marouane El Hallaoui <marouane.el.hallaoui@oracle.com>", "commit.committer-ts": 1697732165, "commit.rev": "aeab112b50799393fd379ac14057a09fb518a6e1"}, "truffle": {"commit.committer": "Marouane El Hallaoui <marouane.el.hallaoui@oracle.com>", "commit.committer-ts": 1697732165, "commit.rev": "aeab112b50799393fd379ac14057a09fb518a6e1"}, "vm": {"commit.committer": "Marouane El Hallaoui <marouane.el.hallaoui@oracle.com>", "commit.committer-ts": 1697732165, "commit.rev": "aeab112b50799393fd379ac14057a09fb518a6e1"}}
component_catalog="https://www.graalvm.org/component-catalog/v2/graal-updater-component-catalog-java17.properties|{ee=GraalVM Enterprise Edition}rest://gds.oracle.com/api/20220101/"
GDS_PRODUCT_ID="D53FAE8052773FFAE0530F15000AA6C6"
alawasoft commented 6 months ago

I got javacv to work on mine and was able to compile into native-image. The Negative error you guys encounter is because you're packing more than 2GB into the native image. Try separating the native libraries from the native-image.

aliiy commented 6 months ago

I got javacv to work on mine and was able to compile into native-image. The Negative error you guys encounter is because you're packing more than 2GB into the native image. Try separating the native libraries from the native-image.

You're right. Thank you very much

yyyyyyyysssss commented 1 month ago

I got javacv to work on mine and was able to compile into native-image. The Negative error you guys encounter is because you're packing more than 2GB into the native image. Try separating the native libraries from the native-image.

How to separate? Can you say something? Currently it seems Graalvm can only package it into a native executable file

aliiy commented 1 month ago

I got javacv to work on mine and was able to compile into native-image. The Negative error you guys encounter is because you're packing more than 2GB into the native image. Try separating the native libraries from the native-image.

You need to dynamically load the corresponding platform's DLLs at program startup and declare the dependency on JavaCV in the module-info.java file

RedOneKH commented 3 weeks ago

@aliiy can you please share with us a sample how you made it work?