Java byte code runs on a JVM, which is fairly small-just a few hundred KB on a Linux system. Built on the Java language is a library of code for higher-level activities, like creating and handling files, thread synchronization, lists, and other utilities. This code is rather large-close to 100MB for the Java 1.5 library. You can reduce this when deploying Java by leaving out the locale data and only including the jar files (the equivalent of libraries for a compiled system) necessary to run the program on the target device.
Because Java is a compiled language, why not change the compilation output from Java byte code to machine language? GCJ, part of the GCC compiler set, does exactly that by being a front end for the GCC compiler. This means development can be done and tested in Java and then deployed on any machine that has a GCJ compiler, not just machines that have a JVM. You can have the development conveniences of Java and the performance of a natively compiled language. Inside GCC, the process for compilation has steps that involve converting the input into a data structure that GCC can then use for optimization and eventual conversion into machine code for the target platform. So, GCJ parses the Java input for processing the same way the C compiler handles C code. GCJ is part of the GCC compiler suite and can be built at the same time as the toolchain, but adding Java to the list of enabled languages when building the final compiler. Crosstool-ng has an option to build GCJ as well, and this is the recommended option as the GCJ front end; it usually requires patches to cross-build, and cross-tooling does an excellent job of keeping track of the source patches necessary for the target machine.
Compiling Java code with GCJ is like compiling any other code with GCC. Consider the following simple Java program:
class simpleProgram
{
void hello() {
System.out.println("Hello");
}
public static void main(String args[])
{
simpleProgram program = new simpleProgram();
program.hello();
}
}
To compile it, do the following:
<cross-compiler>-gcj simpleProgram.java -o test-java -main=simpleProgram
The main parameter indicates what class contains the entry point. Java allows for multiple classes to have this function, so the compiler can't guess what class's main should be used as the entry-point for the program. The programmers have made compiling with GCJ similar compiling a C program. Embedded GCJ After you compile the program in to a binary, it looks like any other binary that needs to be placed on a target system. Any libraries used by the file must be put in the target's /lib directory. All programs compiled with GCJ link to the libgcj.so library by default, so that must be copied to the target. If you want to link statically, use the following command for the previous example:
<cross-compiler>-gcj simpleProgram.java \ -static-libgcj -o test-java -main=simpleProgram
Depending on the size of the program, static linking may make sense. For example, the simple example program, when statically linked, is about 12KB in size, which is a much smaller footprint than the size of libgcj.so.
Our website is not responsible for the information contained by this article. Articleinput.com is a free articles resource thus practically any visitor can submit an article. However if you notice any copyrighted material, please contact us and we will remove the article(s) in discussion right away.
Note: This article was sent to us by: John F. Ninger at 02012010
1. Why is Linux such an incredible piece of sowtware
All articles are property of their respective authors. Please read our Privacy Policy!
© 2009 ArticleInput.com.