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.
Software Engineering Stack Exchange is a question and answer site for professionals, academics, and students working within the systems development life cycle. It only takes a minute to sign up.
Sign up to join this communityIt 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.
Not quite directly answering the OP, but an perhaps an interesting aside. Java can be run in three modes:
-Xint
flag - Interpreted - Byte code only-Xcomp
flag - Compiled - machine compiled-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 :-)
javac
? when using the -Xcomp does it by default output a single binary file?
Apr 19, 2019 at 0:04
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.
java -Dos.name=MacOS
.
Sep 18, 2012 at 18:51
It is now: GraalVM allows you to compile your programs ahead-of-time into a native executable. Take a look at native-image feature:
Summary
https://www.graalvm.org/docs/getting-started/#native-images
Demos: Native images for faster startup
https://www.graalvm.org/docs/examples/native-list-dir/
Detailed: 'Ahead-of-time Compilation'
https://www.graalvm.org/docs/reference-manual/aot-compilation/
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