javaspring-bootgraalvm

GraalVM Error: Main entry point class 'org.example.Main' neither found on the classpath


I have a problem. I want to build a native -image Jar file, for this I write this command

native-image --no-server -cp C:\Users\Andrey\IdeaProjects\example\target\example-0.0.1-SNAPSHOT.jar org.example.Main

I am writing all this in the program "x64 NATIVE Tools Command Prompt for VS 2022". It is running on behalf of my administrator. After entering the command, I get the error:

========================================================================================================================
GraalVM Native Image: Generating 'org.example.main' (executable)...
========================================================================================================================
[1/7] Initializing...                                                                                    (0,0s @ 0,13GB)
Error: Main entry point class 'org.example.Main' neither found on the classpath nor on the modulepath.
classpath: 'C:\Users\Andrey\IdeaProjects\example\target\example-0.0.1-SNAPSHOT.jar'
modulepath: 'C:\Program Files (x86)\graalvm-ce-java17-22.3.1\lib\svm\library-support.jar'
Error: Use -H:+ReportExceptionStackTraces to print stacktrace of underlying exception
Error: Image build request failed with exit status 1

But my directories look like this: enter image description here

Help me please. Ordinary non-SPRING applications are quietly building My pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>org</groupId>
    <artifactId>example</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>example</name>
    <description>example</description>
    <properties>
        <java.version>17</java.version>
        <start-class>org.example.Main</start-class>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.graalvm.buildtools</groupId>
                <artifactId>native-maven-plugin</artifactId>
            </plugin>

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.2.0</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>org.example.Main</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>

        </plugins>
    </build>

</project>

java - version:

openjdk version "17.0.6" 2023-01-17
OpenJDK Runtime Environment GraalVM CE 22.3.1 (build 17.0.6+10-jvmci-22.3-b13)
OpenJDK 64-Bit Server VM GraalVM CE 22.3.1 (build 17.0.6+10-jvmci-22.3-b13, mixed mode, sharing)

Solution

  • The error you are seeing is due to the fact that the executable fat jar generated by the spring-boot maven plugin doesn't have the main class in the path com/example/Main. The class ends up being located inside BOOT-INF/classes/com/example/Main which gets loaded using spring-boot's own main class org.springframework.boot.loader.JarLauncher. Instead of using the spring boot plugin to generate the fat jar, you can use the maven-shade plugin in which case the main class would be present in the expected path.

    Shade plugin usage:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.5.0</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    

    After using the shade plugin, instead of the spring-boot plugin, I didn't get this error and I was able to generate the native image successfully with a command like below:

    native-image --no-fallback -J-Xmx3072m -H:Name=springbootdemo -cp target/spring-boot-demo-0.0.1-SNAPSHOT.jar io.github.devatherock.Application
    

    The generated native image did run into an exception when executing, but that is probably related to missing reflection config. You can find the sample application on github