Compiling Java Programs into Machine Code
With GCJ, the GNU Compiler for Java, you can compile Java source code into native machine code and eliminate the need for JVM, the Java Virtual Machine. Here's what you need.
1. Install GCC, the GNU Compiler Collection.
2. Install GCJ, the GNU Compiler for Java.
3. Install KDevelop, the IDE. (Not essential but it's a useful and convenient program for writing source code and compiling.)
4. Install GDB, the GNU debugger. (Also, not essential but useful.)
(Dependency problems with previous versions, especially release candidates, were resolved with the release of Kanotix-2005-04.)
Now, you should be able to compile Java source code into native machine code. Here's an example.
I opened KDevelop multilanguage IDE and created a new file that I called "kanotix.java". Here's what I typed:
:code:public class kanotix {
- static void main(String args[]) {
- out.println("Kanotix is the BEST!");
}
}:code:
At the bottom of the screen, click the Konsole button and a Konsole window will appear at the bottom of the screen. Now you can issue commands from the command line and view your code at the same time.
If I wanted to use JVM and compile the program to java bytecode and run it, I would type these commands:
:code:$ javac kanotix.java
$ java kanotix:code:
And the output is: Kanotix is the BEST!
To compile this program into native machine code and run it, however, I type these commands:
:code:$ gcj --main=kanotix -o kanotix kanotix.java
$ ./kanotix
(or simply $ kanotix):code:
Again the output is: Kanotix is the BEST!
There are many websites with information about GCJ, but two I found very helpful were:
http://www.redhat.com/support/wpapers/redhat/gcj/gcj.pdf∞
http://www.linuxjournal.com/article/4860∞
Website that describes various methods to install a Java Runtime Environment (JRE/J2RE) or a Java Development Kit (JDK/J2SDK) on Debian GNU/Linux:
http://wiki.serios.net/wiki/Debian_Java_JRE/JDK_installation∞
revised-- 6 Jan 06.