Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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.

share|improve this question

3 Answers 3

up vote 28 down vote accepted

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.

share|improve this answer
    
I'm sorry that I can't up vote because I don't have 15 rep so I'll only be able to accept your answer. –  Russell Feb 2 '12 at 15:02
7  
@Russel: Hey, now you have > 15 rep! –  FrustratedWithFormsDesigner Feb 2 '12 at 15:10
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

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
share|improve this answer
    
@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
1  
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

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.

share|improve this answer
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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