views:

150

answers:

3

Is there any way to compile from Java to machine code?

+8  A: 

I've never used this functionality, but gcc comes with that ability:

http://gcc.gnu.org/java/

James Kingsbery
+1, cool, I didn't know about that!
FrustratedWithFormsDesigner
Yeah, 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
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.
EJP
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
+3  A: 

Yes, the JIT in the JVM does exactly that for you.

In fact it can be produce faster code than compiling the code in advance as it can generate code optimised for the specific platofrm based on how the code is rused at runtime.

Peter Lawrey
Yes, 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
+3  A: 

Excelsior JET is a commercial Java to native code compiler.

Mark
Just in case, there are free licenses for non-commercial projects.
Dmitry Leskov