February 19, 2008

HowTo - GCJ: A native Java Compiler

MinGW
MinGW[1] allows you to create native Windows programs. In other words, you can make an exe out of a Java program. The files required for the GCC Java complier are: gcc-java-3.4.2-20040916-1.tar.gz, mingw-runtime-3.9.tar.gz, w32api-3.6.tar.gz, binutils-2.15.91-20040904-1.tar.gz, gcc-core-3.4.2-20040916-1.tar.gz,

These files can be downloaded from the MinGW download page[2] . Additionally, linking requres the libiconv[3] library file libiconv-1.9.1.bin.woe32.zip. This file can be downloaded from its SourceForge.net page[4].

Unzip all the above files in a folder, say D:/mingw. The file gcj.exe will be in D:/mingw/bin. You can add this to the system PATH environment variable.

Compiling a program:
gcj -c -g -O Hello.java

Creating an exe file:
gcj --main=Hello -o Hello Hello.o

This will create a file called Hello.exe

Consider the following program Hello.java
class Hello {
public static void main(String args[]) {
System.out.println("Hello, World!");
}
}


This is a 117 bytes file that will normally require a 15MB JRE from Sun to execute. On the other hand, by using GCJ, the same program compiled into a 3.32MB Hello.exe file.

micro-libgcj
This size can be reduced if we use the minimal micro-libgcj[5] runtime. Note that this runtime supports only the bare minimim features in Java. So, most advanced features including Reflection are not supported.

References
1. http://www.mingw.org/
2. http://www.mingw.org/download.shtml
3. http://www.gnu.org/software/libiconv/
4. http://sourceforge.net/project/showfiles.php?group_id=25167&package_id=51458
5. http://ulibgcj.sourceforge.net/

No comments: