Java byte code embedding and compiling facts


Java Runtime Environment

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.

Embedding Java: Using GCJ

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

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.

Legal Disclaimer

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

Related Articles

1. Why is Linux such an incredible piece of sowtware
Embedded Linux Linux is an incredible piece of software. It’s an operating system that’s just as at home running on IBM’s zSeries supercompute...

2. Explanation of the Embedded Linux development process
Embedded Linux is a topic with many interdependencies; this article lays out the big points and purposely lacks detail so you can see the big picture without getting dist...

3. Basics to understanding the structure of an embedded Linux system
Anatomy of an Embedded Linux System At runtime, an embedded Linux system contains the following software components: • Boot loader: What gets the ope...

4. The resemblance between the GCC compiler and the kernel in Linux
The GNU Compiler Collection The GCC compiler, like the kernel, is designed for portability. Like all open source programs, GCC is available in source form, and ...

5. Automake and Autoconf discover the state of the target environment
Automake/Autoconf Open source software is designed to be distributed in source code form so that it can be compiled for the target platform. When target platfor...

6. How and where does a software developer get help
Where to Get Help All software developers depend on little helpers, whether visible or invisible. Open Source developers tend to call upon a large number of res...

7. Necessary additional steps to get Linux running
Host Services After the software is installed, some additional configuration steps are necessary to get the packages in running order. This part goes through co...