5

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

1 Answers1

6

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.

BoriS
  • 907
  • 5
  • 13
  • Thanks for the answer. But, that did not work. Please see my update. Can you suggest the commands I have to type in the terminal? – Jayadevan Vijayan Jun 24 '19 at 07:46
  • Take a look at https://stackoverflow.com/questions/17981958/how-to-create-jar-file-using-eclipse From what I see in your edits, you created a jar with only one class. – BoriS Jun 24 '19 at 08:49
  • Thanks a lot. It worked. So the procedure is, export to jar through Eclipse IDE and then use "native-image -jar Main.jar" (in my case /home/appu/Downloads/Apps/GraalVM/2019-06-20/graalvm-ee-19.0.2/bin/native-image -jar Main.jar). This is the correct answer. Since I am a new user, I am not able to upvote it. I have chosen it as the correct answer. – Jayadevan Vijayan Jun 24 '19 at 18:21