Create a native Java executable, revisited

You can create a native executable -- but do you really want to?

Q
If I know the operating system on which my program will run, can I compile my Java code into native machine code and run it without the VM?

A

URLs for various native compilers and C translators can be found in the Resources section below.

Be warned, though: you may lose certain features of the Java platform if you compile to native code. Further, some of the older compilers will not allow the dynamic addition of new classes at runtime. When choosing a compiler, carefully compare the compiler's feature list against your software's requirements, as expected JVM behaviors and features may not carry over to the compiled code. Native compilers can also be expensive. If you are concerned about performance, adaptive compiling technologies such as HotSpot and JIT have improved Java performance greatly. Happily, they keep getting better. If your intention is to simplify the user experience by not requiring the user to type java <class name> at the command line, you can simply create a .bat file for Windows or a shell script for Unix that takes care of setting CLASSPATHs and running the program. A batch file could be as simple as:

run_that_program.bat
java program_class_name

Learn more about this topic

Join the discussion
Be the first to comment on this article. Our Commenting Policies