Embedded Java with GCJ

You don't always need a Java Virtual Machine to run Java in an embedded system.

This article discusses how to use GCJ, part of the GCC compiler suite, in an embedded Linux project. Like all tools, GCJ has benefits, namely the ability to code in a high-level language like Java, and its share of drawbacks as well. The notion of getting GCJ running on an embedded target may be daunting at first, but you'll see that doing so requires less effort than you may think.

After reading the article, you should be inspired to try this out on a target to see whether GCJ can fit into your next project. The Java language has all sorts of nifty features, like automatic garbage collection, an extensive, robust run-time library and expressive object-oriented constructs that help you quickly develop reliable code.

Why Use GCJ in the First Place?

The native code compiler for Java does what is says: compiles your Java source down to the machine code for the target. This means you won't have to get a JVM (Java Virtual Machine) on your target. When you run the program's code, it won't start a VM, it will simply load and run like any other program. This doesn't necessarily mean your code will run faster. Sometimes you get better performance numbers for byte code running on a hot-spot VM versus GCJ-compiled code.

One advantage of using GCJ is that you save space by not needing the JVM. You may save money in royalties as well. Furthermore, using GCJ lets you deliver a solution using all open-source software, and that's usually a good thing.

Pitfalls

The first thing embedded engineers reach for when creating a root filesystem for a target is trusty uClibc, a compact implementation of the glibc library. For those new to using Linux on an embedded target, the standard C library can be a bit on the large side when working with targets that may have only 8MB (for example) for a root filesystem. To conserve space on an embedded system's root filesystem, engineers will switch from the standard C library to something smaller, like uClibc. GCJ requires unicode support, which is not supported by uClibc, so glibc is a requirement.

The standard library for GCJ weighs in at 16MB, so even if you could conserve space by switching to a smaller standard C library, it wouldn't make that much difference overall. The standard GCJ library can be trimmed by removing support for executing Java byte code, but the loss of that feature would reduce the overall value of GCJ.

The Host and Target Configuration

Because this article is about using GCJ in an embedded environment, it shows you how to build a cross compiler and a simple root filesystem for the target system. For those new to embedded development, a cross compiler builds code that runs on a processor different from the machine where the compilation occurred. The machine that runs the compiler is called the host and the one where the code runs is called the target.

In this article, the target system is a PPC 745/755-based system running at 350MHz. This particular board comes wrapped in a translucent case with a monitor and hard drive and is otherwise known as an iMac. Okay, this is hardly a prime example of an embedded system, but it does present some of the same challenges you'll encounter with a true embedded system. The things you learn here should apply well to embedded systems using other processors.

The host system is a run-of-the-mill IBM ThinkPad notebook running a Pentium III processor. Yellow Dog Linux is already running on the host system, but with a little sleight of hand, we'll make it use the root filesystem created in the article for testing.

Getting GCJ Ready

First, we need a cross compiler that runs on our Pentium machine that creates code for a PowerPC 750-based processor. Building a cross compiler for a target system can be a very tedious process; a working compiler is more than GCC, it also contains some extra tools (affectionately named binutils) and the standard libraries for the language.

To get a cross compiler up and running quickly, try using the crosstool package, compliments of Dan Kegel. Crosstool does all of the hard work necessary to get a cross compiler built: it fetches the source and patches, applies the patches, configures the packages and kicks off the build. After obtaining and unpacking crosstool, here are the steps for building your GCJ cross compiler:


$ export TARBALLS_DIR=~/crosstool-download
$ export RESULT_TOP=/opt/crosstool
$ export GCC_LANGUAGES="c,c++,Java"
$ eval `cat powerpc-750.dat gcc-4.0.1-glibc-2.2.2.dat' sh.all --notest

While waiting for the compilation to finish, let's take a look at what we just did to start our crosstool build. TARBALLS_DIR is the location where crosstool downloads its files. Crosstool fetches all of the files it needs for a build by default. RESULT_TOP is the installation directory of the cross compiler. Lastly, GCC_LANGUAGES controls which language front ends will be enabled for the compiler. GCC supports many different language front ends and each front end adds a considerable amount of time to the compilation process, so only the necessary ones were selected for this toolchain build.

The last line, for those lacking their bash script-foo license, dumps the two dat files on the command line and executes the all.sh script with the parameter --notest. To make building a cross compiler easy, crosstool includes configuration files with the correct environment variables set for the target processor and the gcc/glibc combination. In this case, crosstool builds a gcc 4.0.1 with glibc 2.2.2 targeting a PPC 750 processor. Crosstool includes scripts for all major processor architectures and glib/gcc combinations.

When the build finishes, the cross compiler will be installed at $RESULT_TOP/gcc-4.0.1-glibc-2.2.2/powerpc-750-linux-gnu/bin. Add this to your path to make invoking the cross compiler easier.

______________________

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Commission Blueprint

John Maven's picture

I compiled it and it didn't work. I repeat: it did NOT work.

Rather unfortunate.

Typographical Error

lumkichi's picture

While everyone noted the sample "hello" program does not compile as-is, I'm surprised no one expressed why but instead offered alternatives. The reason the sample program does not compile is because there are two typographical errors in it (I'm sure it was unintended - an artifact as a result of using a Word Processor such as MS Word).


Class hello {
  Static public void main(String argc[]) {
    System.out.println("hello from GCJ");
  }
}

The keywords "Class" and "Static" should not be capitalized. It should look like this:


class hello {
  static public void main(String argc[]) {
    System.out.println("hello from GCJ");
  }
}

The capitalization changed the keywords "class" and "static" into Object names. The compiler was trying to find a "Class.class" to make a "Class" object named "hello" and couldn't. The same with "Static."

This is a great article, and the author did a great job writing it. Unfortunately formatting the document (and the code, especially) is a bear. Just to get the code snippet above look the way is does was a lot of work - way more work than just typing.

~lum

Won't compile with gcj-4.1 on my Debian

Olivier Berger's picture

Hmmm... maybe this is not related to the specifics of embedded environment at all, but I noticed that the hello world example won't compile with "gcj-4.1 hello.class --main=hello -o hello-java" on my Debian testing system :

hello.class:1: error: Class or interface declaration expected.
Class hello {
^
hello.class:2: error: Class or interface declaration expected.
Static public void main(String argc[]) {
^
hello.class:3: error: Class or interface declaration expected.
System.out.println("hello from GCJ");
^
hello.class:4: error: Class or interface declaration expected.
}
^
hello.class:5: error: Class or interface declaration expected.
}
^
5 errors

Dunno what's wrong

After a quick search on the

Olivier Berger's picture

After a quick search on the net, I found that writing this way, it compiles :


public class hello {
static public void main(String argc[]) {
System.out.println("hello from GCJ");
}
}

This worked for me:class

Anonymous's picture

This worked for me:

class hello
{
static public void main( String argc[] )
{
System.out.println( "hello from GCJ" );
}
}

Notice that none of the keywords like "class" are capitalized. Also, typical naming convention for java is to call the source file hello.java and using the javac command turns the byte code into hello.class We're not using javac, so there won't be a hello.class file. So, if you name your file this way, the compile step would be:

gcj hello.java --main=hello -o hello-java

Hope this helps.

After reading this article, I

mathews's picture

After reading this article, I just realized how we can simplify our seemingly complicated problems which in fact can actually be solved very easily! It actually helps us to use GCJ which is really a part of the GCC compiler suite, in a Linux project! The advantage is that it can be code with a high-level language like Java! It gives us a detailed tutorial about its advantages and pitfalls, the host and target configuration and lastly if you are happy with what you have read, the step by step instructions to build your GCJ cross compiler! Very cool and informative read indeed!!RFID Tags

Statically linking

Dan's picture

I tried statically linking a simple "Hello, World!" program but received the following error:

/usr/bin/ld: cannot find -lgcj

Here's the command I used to compile:

gcj -static-libgcj -o hello --main=hello hello.java

Any ideas?

White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState