Saturday, December 2, 2006

How to make windows native executable files for Java applications

There are several ways to make a native executable file for a java application. There are generally two approaches in making executable files. The first approach is to make a .exe files which does NOT rely on a JRE on the host machine. In this method, all necessary java library files (jar files, zip file, class files, dlls, and so on) are put in a single installer file, which can then be installed on a machine without a JRE. This is a nice solution, since you can be sure that end-user of your java application does not need to know a word about JRE and Java, but for a small cost. The cost is the size of your final binary installer which may be up to 10MB for a small Hello World application. In fact it relies on the JRE version and the size of runtime library and some other jar files bundled with a standard JDK distribution.

There are several ways to make your executable java applications by this manner. You can use commercial products, such as "Zero G InstallAnywhere", "Advanced Installer" and "Excelsior JET". The last one claims that have a powerful optimizer which makes java applications run faster than running on a normal JRE. Here is a benchmark by Excelsior. "Zero G InstallAnywhere" is widely used by companies who release their software, or developer's packages in Java language.

This is the most easy (but not free) solution. There is also a free solution, which is to use GCJ (GNU Compiler for Java). NativeJ, is a free fron-end for GCJ (since GCJ is hard to deal with in Windows). NativeJ's website says:
NativeJ is a Java to native compiler. It allows AOT (ahead of time) compilation of your Java applications.With NativeJ, you can create real standalone native binaries (.exe on Windows) which will no longer depend on a JRE.

GCJ is an incomplete Java compiler and runtime environment. It is absolutely open source, and runs on platforms supported by GCC (Linux/UNIX, Window, Solaris). By the time of this article, the latest (non-stable) version of GCJ was 4.1. I'm, not sure but I think that 4 should be compatible with Sun JDK 1.4. This is a nice idea to use GCJ as a free tool to make your Java applications into Windows (or other supported platforms) native executable, but there are some problems with that. Since it is incomplete, you can not be sure that all your java files can be compiled and linked with GCJ. The other problem is that there is not an IDE which supports GCJ as a JRE and Java compiler and linker. Eclipse, NetBeans, IntelliJ IDEA, JBuilder, JDeveloper, WSAD, JCreator, etc.; none of them does support GCJ. The best one for this mean may be Eclipse, since there are hundreds of plugins written for it. There is only one plugin for Eclpise which auto generates a GNU Makefile for a java project. This Makefile can only be run under Linux platforms! Here is a nice article about "Compiling Java with GCJ" by Per Bothner.

The second approach is to make .exe files which still relies on JRE. It means that one should have JRE installed on his/her computer in order to be able to run the binary executable file. In fact it works as a simple batch (.bat) file script: checking if JRE is available on user's system and run the main class with class path properly set. This way is nice for those who want to give their application to IT professionals, developers, or someone who may use the application on a machine with proper JRE. The size of .exe file will not be an issue, since it doesn't put library files in the exe file. There are a lot of solutions for this mean:

No comments: