Pages

Wednesday, May 30, 2012

Compiling SWT Applications with GCJ, Part 1

Using GCJ to compile a native Windows app using SWT. Can it be done?

The GCJ compiler from GNU's Compiler Collection generates native code from either the .java source files of a project or from the precompiled .class bytecode that has been generated from another compiler like Sun's (Oracle's) javac.

But it seems this piece of the GCC has not been maintained as well as the others. The MinGW project which releases binaries for gcc meant for use with Windows states the following on their most download page for gcc (version 4.6.2):

* The Java language is absent, pending resolution of build issues. The last mingw.org release offering Java was 4.4.0.

http://sourceforge.net/projects/mingw/files/MinGW/Base/gcc/Version4/gcc-4.6.2-1/
Okay, great... build issues. Well, if the MinGW people can't build it on Windows, I probably won't have much better luck. I'm currently using the binary offered by the thisiscool.com page on GCC MinGW.

I am using the archive with the filename gcc43ecj-20061207.tar.bz2. gcc reports
Thread model: win32
gcc version 4.3.0 20061108 (gcj-eclipse experimental)
Ok, so let's use it. I installed into C:\thisiscool-gcc and then extended my PATH to include the bin directory:
SET PATH=c:\thisiscool-gcc\gcc-ecj\bin;%PATH%
Now, let's move into the directory with my SWT distribution, org.eclipse.swt and try to compile it. I'm using the source available at the Eclipse Download Page for SWT. There you will find several links to download.

Thanks to the notes from this blog comment from Radu Racariu, I was able to get this to work. To compile a SWT program, you first compile the entire swt.jar into an .o file. Here is the result:

Image 1: Result of compiling swt.jar using the command gcj -c -fjni -g0 swt.jar
Wait a minute. The resulting file swt.o is over 20MB! How can I make it smaller? Well, I tried several things, but none of them worked:
  • Compiling with gcj -Os seems to cause my poor laptop to slowly die. After about 3 hours, gcj is hogging 600 MB of RAM and no sign that it will finish. Eventually it dies with a message about unable to allocate more memory.
  • In theory one can extract the JAR contents and compile each .class file separately. However, when it comes to linking (next step, below) I could not get the resulting program to work.
  • According to the GNU people, even better than compiling .class files is to compile the .java files. But this does not succeed. I might have something misconfigured, or there might be some incompatibilities in the Java language variant used to develop SWT.
Ok... so I will stick with this solution. But, how is the resulting executable? I compile a simple SWT test program with gcj like this:
set CLASSPATH=..\..\org.eclipse.swt\swt.jar
gcj -o MySWTTest.exe -s --main=MySWTTest MySWTTest.class ..\..\org.eclipse.swt\swt.o
It works, but the resulting .exe is over 27 megabytes in size... All it does is display a message, a button and a DateTime selector:

Image 2: Simple SWT Test program running to show that the GCJ-generated SWT app actually works
Ok... so it is possible to compile SWT programs into native Windows binaries, but how can we reduce the filesize? I try to tackle this problem in my next post.

No comments:

Post a Comment