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.
- Two Pi R
- Readers' Choice Awards 2013
- A Handy U-Boot Trick
- The Geek's Guide to the Coolest 2013 Holiday Gifts
- Sublime Text: One Editor to Rule Them All?
- RSS Feeds
- GIMP Shmimp, Give Me a Browser
- Raspberry Pi: the Perfect Home Server
- Tinker with Molecular Dynamics for Fun and Profit
- Linux Systems Administrator
- Máy sấy quần áo
58 min 18 sec ago
- Services on GlusterFS
1 hour 7 min ago
- máy lọc nước
1 hour 58 min ago
- Máy lọc nước
2 hours 1 min ago
- Reply to comment | Linux Journal
2 hours 48 min ago
- Definitely cool stuff here
3 hours 49 min ago
- thanks for the information
5 hours 40 sec ago
- nice information thanks
5 hours 39 min ago
8 hours 34 min ago
- The lost opportunity of security
20 hours 25 min ago