How to compile Java to Native using GraalVM (SubstrateVM) in Eclipse

Jayadevan Vijayan :

I have a research project (implemented in in Java 8) will several classes, several main functions and well over 10000 lines of code, in Eclipse. The program runs out of memory when the input is not small.

I wanted to see if it will use less Stack memory, if compiled as a native application.

I have found no way to do this in Eclipse.

I did
$GRAALVM_HOME/bin/javac /home/appu/Downloads/2019/June/20/HelloWorld.java
It worked. I got a working binary.

I tried
/home/appu/Downloads/Apps/GraalVM/2019-06-20/graalvm-ee-19.0.2/bin/native-image /home/appu/eclipse-nimi/NimishaGraalEE19/bin/nimi/decimate/Decimate.class
I got Main entry point class '/home/appu/eclipse-nimi/NimishaGraalEE19/bin/nimi/decimate/Decimate.class' not found.

I tried
/home/appu/Downloads/Apps/GraalVM/2019-06-20/graalvm-ee-19.0.2/bin/native-image /home/appu/eclipse-nimi/NimishaGraalEE19/bin/*
I got Main entry point class '/home/appu/eclipse-nimi/NimishaGraalEE19/bin/nimi' not found.

The classic

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

is compiled into "HelloWorld.class"

which gives an executable "helloworld" which is executable (application/x-executable).

Can I have the same, from Eclipse? Can I have the same from command line, for multiclass files?

When I change the above code into

public class HelloWorld
{
    public static void hello()
    {
        System.out.println("Hello, World!");
    }
}

and add another class

public class Main
{
    public static void main(String[] args)
    {
        HelloWorld.hello();
    }
}

They compile correctly, but I get

appu[23]/home/appu/Downloads/Apps/GraalVM/2019-06-20/graalvm-ee-19.0.2/bin/native-image /home/appu/Downloads/2019/June/23/HelloWorld.class 
Build on Server(pid: 17223, port: 36631)                                                          
[/home/appu/downloads/2019/june/23/helloworld.class:17223]    classlist:     415.66 ms            
Error: Main entry point class '/home/appu/Downloads/2019/June/23/HelloWorld.class' not found.     
Error: Use -H:+ReportExceptionStackTraces to print stacktrace of underlying exception             
Error: Image build request failed with exit status 1                                              
appu[23]ls                                                                                        
HelloWorld.class  HelloWorld.java  Main.class  Main.java

What I want is an executable (in my case, a Gnu/Linux executable) file which can be used as a native executable.

I prefer a solution from Eclipse, but Command line, Netbeans, VS Code, ... or any other technique is welcome.


In response to answer by BoriS:

I tried making a jar a few hours ago, and made a file called Main. It did not work.

jar cfe Main.jar Main Main.class
/home/appu/Downloads/Apps/GraalVM/2019-06-20/graalvm-ee-19.0.2/bin/native-image -jar /home/appu/Downloads/2019/June/23/Main.jar
./Main

When I ran Main which is executable (application/x-executable) type, I Got

Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld
        at Main.main(Main.java:5)
Caused by: java.lang.ClassNotFoundException: HelloWorld
        at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        ... 1 more
BoriS :

Two options from the docs (https://www.graalvm.org/docs/reference-manual/aot-compilation/):

1) Build a jar of your project and build a native image out of that

native-image [options] -jar jarfile to build an image for a jar file.

2) Set the native image classpath correctly and give native image the main you want

You may provide additional options to the native image building: -cp and --class-path help to search for class files through separated list of directories, JAR archives, and ZIP archives;

native-image [options] class to build an executable file for a class in the current working directory. Invoking it executes the native-compiled code of that class.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=123262&siteId=1