How to Compile Java to Native Code

Compiling java source code to native exe

The only to-native-code Java compiler that I'm aware of is The GNU Compiler for the Javatm Programming Language.

But it's extremely unlikely you really need a to-native-code compiler. Packers like the ones you've dismissed can make your program entirely self contained, including installing a private JVM, seamlessly. I strongly recommend you check out the options in this question and its answers.

Performance gain in compiling java to native code?

There was a similar discussion here recently, for the question What are advantages of bytecode over native code?. You can find interesting answers in that thread.

Is it possible to compile to machine code in Java without an external program?

Seeing as the HotSpot JVM can compile Java bytecode to machine code, I assume there is some mechanism available to compile source code of type A to machine code.

This does not follow.

The JIT compiler compiles Java bytecodes to native code. It does not understand anything other than Java bytescodes. And bytecodes are not "source code". (They are actually a form of machine code ... for an abstract computer ... a Java virtual machine.)

In short, there is no mechanism available as part of the JVM for compiling source code to machine code.

And, as it turns out, the JIT compiler is not designed for generating native code in files that something else could use. The native code is in the form of raw machine instructions in blocks of memory. No symbol tables. No relocation information. Probably full of hard-wired calls into other parts of the JVM. Basically it is designed for execution in the currently running JVM, not for anything else.

Is there any way to use this in a compiler written in Java?

The JIT compiler is not applicable to your problem ... unless you write your compiler to generate valid Java bytecodes. And if you did that, then the JVM could run your code, and the JIT compiler would at some point compile your bytecodes to native code.


Bottom line: if your goal is to generate native code that can be run as or linked to a separate executable,

  • the JIT compiler is of no use to you, but
  • you could use the JVM including the JIT compiler as your execution platform, by generating bytecodes, and
  • you could use also ordinary Java programming to implement your compiler or assembler, including a component that generates and emits native code in a format that is appropriate to your needs.

How To Build Java Native Executable Files For Linux

as @Slaw mentioned in a comment
for java 14+ this is the best solution

it uses the jlink command internally
and supports Mac, Windows, and Linux


The official Java-based solution is mentioned above
but its highly recommended to use GraalVM
in case you want extra performance and a better approach for microservices

installation steps are mentioned below in @Kevin Hooke's answer



I think ```jlink``` is useable with older Java versions too

if anyone has experience with jlink on older versions please comment below



Related Topics



Leave a reply



Submit