Is there any way to compile from Java to standalone (or library) machine code without requiring a JVM?
-
8If 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. – T.J. Crowder Jun 07 '10 at 17:53
-
3Yet 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. – Dmitry Leskov Jun 08 '10 at 04:53
-
1I 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. http://docs.oracle.com/cd/A87860_01/doc/java.817/a83727/jtools5.htm – supernova Apr 20 '14 at 16:14
-
@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. – CodeMouse92 Apr 15 '16 at 22:32
5 Answers
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.

- 7,298
- 2
- 38
- 67
-
2Yeah, 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. – T.J. Crowder Jun 07 '10 at 17:51
-
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. – user207421 Jun 08 '10 at 03:09
-
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. – Dmitry Leskov Jun 08 '10 at 04:50
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:
Summary
https://www.graalvm.org/docs/getting-started/#native-imagesDemos: Native images for faster startup
https://www.graalvm.org/docs/examples/native-list-dir/Detailed: 'Ahead-of-time Compilation'
https://www.graalvm.org/docs/reference-manual/aot-compilation/
The Micronaut platform uses GraalVM to make native microservices:

- 19,195
- 10
- 76
- 121
-
Sadly, even this project isn't stable for production and using the Swing API is still unsupported/buggy: https://github.com/oracle/graal/issues/1327 – BullyWiiPlaza Jul 23 '20 at 20:33
-
1"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. – Rob Audenaerde Jul 24 '20 at 07:34
-
1My 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. – BullyWiiPlaza Jul 24 '20 at 09:32
Excelsior JET is a commercial Java to native code compiler. However, it was discontinued in May 2019.

- 28,783
- 8
- 63
- 92
-
3Just in case, there are free licenses for non-commercial projects. – Dmitry Leskov Jun 08 '10 at 04:57
-
1
-
On May 15, 2019, Excelsior JET was discontinued. See: https://en.wikipedia.org/wiki/Excelsior_JET – Stephan Jan 18 '20 at 13:27
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.

- 525,659
- 79
- 751
- 1,130
-
22Yes, 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.... – Bill K Jun 07 '10 at 21:19
-
2You 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? – Janus Troelsen Feb 14 '15 at 15:26
-
@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. – Peter Lawrey Feb 16 '15 at 17:56
-
-
2OP mentions, standalone, which just excudes any kind of JIT: While JIT produces native code on the fly, it is never standalone. – StormByte Apr 20 '17 at 14:13
-
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. – Kurt Fitzner Jan 08 '18 at 15:17
-
1@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. – Peter Lawrey Jan 08 '18 at 15:22
-
1@KurtFitzner The tool JITWatch can show you the steps the code goes through to native code https://camo.githubusercontent.com/432e2e9e742686128b2526084c9749faad1dcf4c/687474703a2f2f7777772e63687269736e65776c616e642e636f6d2f696d616765732f6a697477617463682f747269766965772e706e673f63623d30 – Peter Lawrey Jan 08 '18 at 15:27
-
3I 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. – Johan Boulé May 29 '19 at 22:25
-
@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. – Peter Lawrey Jun 03 '19 at 06:03
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.

- 17,329
- 10
- 113
- 185