Tuesday, June 09, 2015

How to create embedded Java 8

What is embedded java? Embedded Java is not J2ME (Java Micro Edition), is a normal java runtime in smaller size, suitable to be installed in embedded system.

Normal Java runtime is about 80-100Mb in size. The compact embedded java can be as small as 11Mb.

Started with Java 8 (or 1.8), Java use jrecreate to generate embedded java for specific target platform. The targeting platform is for ARM Linux and x86 Linux. It can be generated on a host with Linux or Windows (prefer to have 1Gb or memory?).

My target platform is DNS-320 with fun_plug hack. It is an ARM cpu (ARMv5TE with soft floating point), 128Mb ram (Yes, is 128Mb). You can create target for Raspberry Pi (with hard floating point).

You need a Java (JDK or JRE) on the host system. After generated, you can just move over the embedded JRE to the target platform.

Download Embedded Java from Oracle technical network.

For my case here, I use:

Embedded Java 1.8, ARM v5 with soft floating point (ejdk-8u33-fcs-linux-arm-sflt.gz).
OpenJDK 1.7 on Debian (32-bits), and build it on Debian 7 runs on Virtualbox VM (on Mac OS X).

Debian$ sudo apt-get install openjdk-7-jre



Extract embedded java 1.8

Debian $ export JAVA_HOME=/usr/lib/jvm/java-1.7.0-openjdk-i386
Debian $ export EJDK=<target directory>
Debian $ cd ejdk1.8.0_33
Debian ejdk1.8.0_33 $ bin/jrecreate.sh --profile compact1 --dest compact1-minimal --vm minimal
Building JRE using Options {
...
target: linux_arm_sflt
vm: minimal
runtime: compact1 profile
debug: false
keep-debug-info: false
no-compression: false
dry-run: false
verbose: false
extension: []
}

Target JRE Size is 10,837 KB (on disk usage may be greater).
Embedded JRE created successfully

The size is 11Mb.

Debian$ du -sh compact1-minimal/.
11M compact1-minimal/.



I write a HelloWorld java to test.

$ cat > HelloWorld.java <<-EOF

public class HelloWorld {
public static void main(String args[]) {
System.out.println("Hello, world!");
}
}
EOF


$ javac HelloWorld.java
⇒ HelloWorld.class
$ java HelloWorld
Hello, world!


I move the hello world program and compact1-minimal to my target platform (the ARM Linux) /opt/java/ejre-1.8.

arm$ export JAVA_HOME=/opt/java/ejre-1.8
arm$ export PATH=$JAVA_HOME/bin:$PATH

arm$ file HelloWorld.class
HelloWorld.class: compiled Java class data, version 49.0 (Java 1.5)

arm$ java -version
java version "1.8.0_33"
Java(TM) SE Embedded Runtime Environment (build 1.8.0_33-b05, profile compact1, headless)
Java HotSpot(TM) Embedded Minimal VM (build 25.33-b05, mixed mode)

arm$ java HelloWorld
Hello, world!

Now you can create an embedded java 8 for Raspberry Pi.

No comments:

Post a Comment