32

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.

36

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.

  • 1
    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 3 '12 at 0: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 3 '12 at 0: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 3 '12 at 2:30
  • I'm using java 6 so java 7 is no problem in my case – Russell Feb 4 '12 at 5:06
  • 1
    The website of GCJ says it hadn't fully support even Java 1.5. See this thread: stackoverflow.com/a/4040404/1257384 – GregT Sep 10 '18 at 7: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 5: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 1: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 at 0:04
7

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.

  • Is there a way this could be fooled/gamed? – FrustratedWithFormsDesigner Feb 2 '12 at 15:18
  • @Frustrated, probably, but I was assuming his intent was to keep from officially supporting operating systems he hasn't tested. It doesn't make sense otherwise to intentionally disable portability you get essentially free. – Karl Bielefeldt Feb 2 '12 at 15:24
  • It also might be that he wants to tie the program to specific hardware in an OS-independent sort of way. – FrustratedWithFormsDesigner Feb 2 '12 at 15:28
  • You can force "os.name" system property by using special arguments for JVM: java -Dos.name=MacOS. – SkyDan Sep 18 '12 at 18:51
2

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

protected by gnat Oct 9 '18 at 5:51

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.