Can I compile Java to native code?
Asked Answered
L

5

99

Is there any way to compile from Java to standalone (or library) machine code without requiring a JVM?

Lour answered 7/6, 2010 at 17:41 Comment(4)
If your interest is speed, don't bother unless you're looking at a platform that HotSpot (Sun's JVM) doesn't support. HotSpot compiles bytecode to native code on the fly wherever a "hotspot" of execution path (something that gets run a lot) shows up, and it's very good at it. But if you're looking to avoid requiring a JVM, yes, look at the gcj James pointed you to.Destructive
Yet another reason is protection against Java decompilers. As for HotSpot vs native compiler (vs JRockit vs IBM vs GCC vs Visual C++ vs hand-writing-CPU-instructions-in-hex) performance, it depends on the application, so YMMV.Ecumenicism
I found one interesting development by Oracle for there Oracle database called JServer Accelerator. It was also designed to be platform Dependent in some way by compiling not to bytecodes but to C code. docs.oracle.com/cd/A87860_01/doc/java.817/a83727/jtools5.htmLepus
@Igal: Reminder, the "moderators" are almost entirely 3000+ rep users. The close flags don't always reflect the close reason perfectly, but I can see why it was closed. This looks to be right on the border of "not a question" because of its length and wording, although I agree it's just on the right side of that border.Appanage
A
41

There used to be a tool called GCJ that was part of GCC, but it's been removed. Now, all the links in the GCC site re-direct to their non-GCJ equivalents.

NB: the comments all refered to my original answer saying you can compile Java to native code with GCJ.

Ariew answered 7/6, 2010 at 17:45 Comment(3)
Yeah, gcj is the most well-known one. @isola009: Keep in mind that when compiling to native code, you will probably be working with a subset (possibly a dramatically small subset) of the libraries that Java normally has by default. Gnu's is pretty good, by all accounts, but well behind the current JDK.Destructive
It is well behind all JDKS starting with 1.2. I've encountered many support problems with people accidentally running GNU classpath instead of Java, and they were all without exception cured by uninstalling it and using a Sun JDK.Sandhog
Excelsior JET includes a licensed implementation of the Java SE 6 standard library and has passed the official compliance tests (JCK). It is at the 6u18 level now, the next version is expected to support 6u20.Ecumenicism
S
41

Yes!

Oracle has been working on the GraalVm, which supports Native Images. Check here: https://www.graalvm.org/

Native Image The native image feature with the GraalVM SDK helps improve the startup time of Java applications and gives them a smaller footprint. Effectively, it's converting bytecode that runs on the JVM (on any platform) to native code for a specific OS/platform — which is where the performance comes from. It's using aggressive ahead-of-time (AOT) optimizations to achieve good performance.

See more:

The Micronaut platform uses GraalVM to make native microservices:

Simple example

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

Compiling java:

 javac HelloWorld.java

Compiling to native:

 native-image HelloWorld

Running (only the executable is needed):

 ./HelloWorld 

Output

 Hello, Native World!
Sexpot answered 27/5, 2018 at 18:6 Comment(4)
Sadly, even this project isn't stable for production and using the Swing API is still unsupported/buggy: github.com/oracle/graal/issues/1327Nebulize
"GraalVM 19.0 release: “GraalVM is finally mature and ready for production use”. Of course there will be bugs and some not-yet-complete functionality. But twitter uses it for their production systems, so I don't agree with your first claim.Sexpot
My personal experience is that on Windows only toy programs work but anything more advanced and/or when using a GUI isn't quite there yet. Twitter probably uses Linux without any GUI but they probably use the commercial version as well while I only tried the community one. It may work for some use cases but Java Windows desktop applications aren't one of them which still makes the project slightly disappointing. Excelsior JET could do this reliably but it is discontinued now.Nebulize
it still need the .jar fileMoil
A
19

Excelsior JET is a commercial Java to native code compiler. However, it was discontinued in May 2019.

Ashraf answered 7/6, 2010 at 20:38 Comment(3)
Just in case, there are free licenses for non-commercial projects.Ecumenicism
how does it compare to RoboVM?Cutty
On May 15, 2019, Excelsior JET was discontinued. See: en.wikipedia.org/wiki/Excelsior_JETCosmography
B
9

Yes, the JIT in the JVM does exactly that for you.

In fact it can produce faster code than compiling the code in advance as it can generate code optimised for the specific platform based on how the code is used at runtime.

The JVM is always involved even if a very high percentage is compiled to native code as you could load and run byte code dynamically.

Barthold answered 7/6, 2010 at 19:1 Comment(10)
Yes, you are correct that the JIT does it and does it very well. It does not make a stand-alone executable, but then the OP wasn't asking about that....Placatory
You seem to suggest that only JIT's can do platform-specific optimization. I think it is possible whenever you are shipping bytecode. The ART compiles bytecode to native code upon installation, and it is not a JIT. Peter, do you have any comments on this?Cutty
@JanusTroelsen Do you have a link to this compiler? Is it widely used? Was it added in the last 5 years? Without the JIT you can't do dynamic compilation which is likely to be more important.Barthold
I think he's referring to AOT compilation with the ART.Palmette
OP mentions, standalone, which just excudes any kind of JIT: While JIT produces native code on the fly, it is never standalone.Accrete
Negative. Minus one for inaccurate. JIT is not compiled. JIT is not native. JIT is a marketing term for various caching optimizations that make interpreting bytecode a little more efficient. It is still interpreted. And, the OP was asking about running without the JVM.Retirement
@KurtFitzner I think you have a very different idea of what the JIT Compiler does, it does compile to native code where the interpretation is not involved.Barthold
@KurtFitzner The tool JITWatch can show you the steps the code goes through to native code camo.githubusercontent.com/…Barthold
I don't believe a JIT compiler can afford as much deep optimisations as native AOT compilers/linkers do: compiling and linking with full optimisation should put any computer on its knees; if not, then the compiler is not doing the best effort to optimise.Mistook
@JohanBoulé I don't know of a compiler which can fully use all the cores on a server. There is some parallelism, possible but AFAIK the Oracle/OpenJDK is single threaded.Barthold
N
0

Another possibility would be RoboVM. However, it only seems to work on Linux, iOS and Mac OS X.

As of today, the project still seems somewhat alive contrary to some posts online claiming the project to be dead.

Nebulize answered 24/7, 2020 at 9:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.