Compiling Java with GCJ
Java bytecodes are a fairly direct encoding of Java programs not really designed for anything else. However, they have been used to encode programs written in other languages. See grunge.cs.tu-berlin.de/~tolk/vmlanguages.html for a list of other programming languages implemented on top of Java. Most of these are interpreters, but a few actually compile to bytecode. The former could use GCJ as is; the latter potentially can use GCJ to compile to native code.
One such compiler is Kawa, which I have been developing since 1996. Kawa is both a toolkit for implementing languages using Java and an implementation of the Scheme programming language. You can build and run Kawa using GCJ without needing any non-free software. The Kawa home page (www.gnu.org/software/kawa) has instructions for downloading and building Kawa with GCJ.
You can use Kawa in interactive mode. Here, we first define the factorial function and then call it:
$ kawa #|kawa:1|# (define (factorial x) #|(---:2|# (if (< x 2) x (* x (factorial (- x 1))))) #|kawa:3|# (factorial 30) 265252859812191058636308480000000
An interesting thing to note is the factorial function actually gets compiled by Kawa to bytecode and is immediately loaded as a new class. This process uses Java's ClassLoader mechanism to define a new class at runtime for a byte array containing the bytecodes for the class. The methods of the new class are interpreted by GCJ's bytecode interpreter.
Of course, it is usually more convenient to put the code in a file:
$ cat > factorial.scm (define (factorial x) (if (< x 2) x (* x (factorial (- x 1))))) (format #t "Factorial ~d is ~d.~%~!" 30 (factorial 30)) ^D $ kawa -f factorial.scm Factorial 30 is 265252859812191058636308480000000.
You can increase the performance of Scheme code by using Kawa to compile it ahead of time, creating one or more .class files:
$ kawa --main -C factorial.scm (compiling factorial.scm)You can then load the compiled file:
$ kawa -f factorial.class Factorial 30 is 265252859812191058636308480000000.To compile the class file to native code, you can use gckawa, a script that sets up appropriate environment variables (LD_LIBRARY_PATH and CLASSPATH) and calls gcj:
$ gckawa -o factorial --main=factorial -g -O factorial*.classUsing the wildcard in factorial*.class is not needed in this case, but it is a good idea in case Kawa needs to generate multiple .class files.
Then, you can execute the resulting factorial program, which is a normal GNU/Linux ELF executable. It links with the shared libraries libgcj.so (the GCJ runtime library) and libkawa.so (the Kawa runtime library).
The same approach can be used for other languages. For example, I am currently working on implementing XQuery, W3C's new XML-query language, using Kawa.
Other applications that have been built with GCJ include Apache modules, GNU-Paperclips and Jigsaw.
GCJ has seen a lot of activity recently and is a solid platform for many tasks. We hope that you consider Java for your free software project, using GCJ as your preferred Java implementation and that some of you will help make GCJ even better.
Special Reports: DevOps
Have projects in development that need help? Have a great development operation in place that can ALWAYS be better? Regardless of where you are in your DevOps process, Linux Journal can help!
With deep focus on Collaborative Development, Continuous Testing and Release & Deployment, we offer here the DEFINITIVE DevOps for Dummies, a mobile Application Development Primer, advice & help from the experts, plus a host of other books, videos, podcasts and more. All free with a quick, one-time registration. Start browsing now...
- Vigilante Malware
- Non-Linux FOSS: Code Your Way To Victory!
- Disney's Linux Light Bulbs (Not a "Luxo Jr." Reboot)
- Vagrant Simplified
- Libreboot on an X60, Part I: the Setup
- Dealing with Boundary Issues
- Bluetooth Hacks
- System Status as SMS Text Messages
- New Products
- October 2015 Issue of Linux Journal: Raspberry Pi