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.

4 Answers 4

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.

6
  • 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. Feb 3, 2012 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, 2012 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. Feb 3, 2012 at 2:30
  • I'm using java 6 so java 7 is no problem in my case
    – Russell
    Feb 4, 2012 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, 2018 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
6
  • 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, 2012 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 :-) Oct 30, 2012 at 15:50
  • Please note that according to my tests, -Xcomp can have poorer performance (by a factor of two) in some instances. Dec 17, 2014 at 18:40
  • Well yeah. Just in time optimizations are hard to do when the JIT isn't even running. Can have != will have. Mar 24, 2016 at 1:30
  • ummm so these are options to javac? when using the -Xcomp does it by default output a single binary file? Apr 19, 2019 at 0:04
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.

4
  • Is there a way this could be fooled/gamed? Feb 2, 2012 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. Feb 2, 2012 at 15:24
  • It also might be that he wants to tie the program to specific hardware in an OS-independent sort of way. Feb 2, 2012 at 15:28
  • You can force "os.name" system property by using special arguments for JVM: java -Dos.name=MacOS. Sep 18, 2012 at 18:51
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

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