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.
|Non-Linux FOSS: Screenshotting for Fun and Profit!||Oct 20, 2016|
|Nasdaq Selects Drupal 8||Oct 19, 2016|
|Canonical Ltd.'s Ubuntu Core||Oct 19, 2016|
|Build Your Own Raspberry Pi Camera||Oct 18, 2016|
|Netlist, Inc.'s HybriDIMM Storage Class Memory||Oct 17, 2016|
|Secure Desktops with Qubes: Compartmentalization||Oct 13, 2016|
- Non-Linux FOSS: Screenshotting for Fun and Profit!
- Nasdaq Selects Drupal 8
- Build Your Own Raspberry Pi Camera
- Canonical Ltd.'s Ubuntu Core
- Secure Desktops with Qubes: Compartmentalization
- Tech Tip: Really Simple HTTP Server with Python
- Netlist, Inc.'s HybriDIMM Storage Class Memory
- Returning Values from Bash Functions
- The Peculiar Case of Email in the Cloud
- Linux Journal October 2016