35

Can you have Java compiled straight into machine code?

I want to do this so I have control over what platforms it's used on, and don't know C,C++ etc.

Dan McGrath
  • 11,161
Russell
  • 453
  • 1
  • 6
  • 10

4 Answers4

39

It appears that the GNU Compiler for Java can convert Java source code into either Java bytecode or machine code. It can also convert existing Java bytecode into machine code. However, the last news is from 2009, so I'm not sure how current it is and if it can handle the latest features of the Java language.

Thomas Owens
  • 82,739
  • 2
    If Java bytecode hasn't changed since 2009, this should still work without developers waving "Hey! I'm still here" flags over the software. – Robert Harvey Feb 03 '12 at 00:49
  • @RobertHarvey I believe Java 7 introduced some new language concepts, so it might fail at converting source files into machine code. If the bytecode has also changed with these new features, then that would fail as well – Thomas Owens Feb 03 '12 at 00:58
  • 1
    Excelsior JET seems to be still active (commercial). There was Fujitsu TowerJ, but that seems to have died a decade ago and was pretty pointless back then. – Tom Hawtin - tackline Feb 03 '12 at 02:30
  • I'm using java 6 so java 7 is no problem in my case – Russell Feb 04 '12 at 05:06
  • Java 8 can emit new bytecodes that will break the GNU compiler, whilst Java 7 (the language) couldn't, you could get say JRuby to generate an invokedynamic, which also would have broken things.. – Martijn Verburg Nov 28 '14 at 11:13
  • 1
    The website of GCJ says it hadn't fully support even Java 1.5. See this thread: https://stackoverflow.com/a/4040404/1257384 – GregT Sep 10 '18 at 07:45
13

Not quite directly answering the OP, but an perhaps an interesting aside. Java can be run in three modes:

  1. Mixed (default) - A combination of Interpreted and Machine compiled code (machine compiled == compiled by JIT at runtime)
  2. With -Xint flag - Interpreted - Byte code only
  3. With -Xcomp flag - Compiled - machine compiled
  • 1
    @Martjin Are this for the HotSpot? By chance do you have reference for the -Xcomp because I could not find that one in the JDK 7 documentation or the HotSpot options documentation and not sure if you have some hidden mailing list secrets that we, the mere mortals, are not aware of :-) – edalorzo Oct 30 '12 at 05:45
  • 2
    It is a deliberately hidden option yes :-). There are various openjdk (openjdk.java.net) mailing lists you can glean this sort of info from - or read the source code :-) – Martijn Verburg Oct 30 '12 at 15:50
  • Please note that according to my tests, -Xcomp can have poorer performance (by a factor of two) in some instances. – Daniel Lemire Dec 17 '14 at 18:40
  • Well yeah. Just in time optimizations are hard to do when the JIT isn't even running. Can have != will have. – candied_orange Mar 24 '16 at 01:30
  • ummm so these are options to javac? when using the -Xcomp does it by default output a single binary file? – Alexander Mills Apr 19 '19 at 00:04
  • This is an option to java, that is how the JIT compiler operates during runtime – Martijn Verburg Apr 20 '19 at 15:47
8

It might be better to detect the operating system using System.getProperty(“os.name”). That would let you choose to support more than one OS, but exclude others.

Karl Bielefeldt
  • 147,435
4

It is now: GraalVM allows you to compile your programs ahead-of-time into a native executable. Take a look at native-image feature:

Related question: 'Can I compile Java to native code?' https://stackoverflow.com/questions/2991799/can-i-compile-java-to-native-code/50555050#50555050


P. S. GCJ isn’t maintained anymore.
Source: 'The Deletion of gcj' http://tromey.com/blog/?p=911

vladZams
  • 141