Develop with Java

From BUG Wiki

Jump to: navigation, search

Contents

Introduction

Java is a great language for the BUG. It's large set of available libraries and frameworks, developer interest, and machine independence make it a great platform for building apps. If you are looking to just get started coding, go here.

PhoneME Advanced

The BUG comes with Java built-in: PhoneME Advanced is a JVM from Sun designed for mobile and embedded systems. PhoneME Advanced (pMEA) is a CDC JVM which means it's classpath and language features fall in line roughly with Java 1.4. pMEA has great performance and utilizes a Just in time Compiler (JIT) internally to convert Java bytecode into native instructions. pMEA can be called from the command line on BUG with /usr/bin/java-cdc.

More information about pMEA and it's community can be found at https://phoneme.dev.java.net/.

BUG Java Libraries

The BUG comes with Java and OSGi libraries for base features and module features. For example, it is possible to set the button LEDs and write to the small LCD screen via Java using BUG libraries.

Resources

Javadocs

BUG javadocs are available here: http://bugcommunity.com/wiki/index.php/Javadoc

Source code

The BUG SVN server contains all of the Java code running on the bug. Most of the code livest in /bug/trunk/com.buglabs.bug.* projects. The SVN repository URL: http://snv.buglabs.net.

Tutorials and HOWTOs

Other JVMs

Other JVMs are available for the BUG as packages. Check the BUG package repository to find out what is available.

OpenJDK

OpenJDK represents the future of open source Java. An ongoing community project that aims to port OpenJDK to various ARM-based computers including BUG has produced a working JVM. Currently it is slow since it is entirely interpreted, but there is active work on LLVM support. This would mean a significant performance increase. If language and classpath features are more important than pure performance, you may want to try out OpenJDK on BUG.

JamVM

A JVM known for it's small size and efficiency, JamVM, is also available for use on BUG. JamVM uses the GNU Classpath project for the Java classpath.

CACAO

CACAO is another free JVM that uses GNU Classpath. It supports an advanced ARM JIT and has many other features.

Resources

The Jalimo project is the source from which most of the Java on ARM work has come from. For additional JVMs and libraries have a look at what Jalimo provides in it's repository.

OSGi

Dynamic hardware services and a component-based architecture at the heart of the BUG platform. The standards-based framework we use to make this happen is called Concierge, an OSGi framework implementation. Consult this guide to get started with OSGi applications on BUG. There are some additional OSGi resources listed on this page. Concierge system properties and BUG additions are documented here.

Writing Java Programs

Plain old Java programs

Since Java programs are machine independent, compiling and running them on a BUG are pretty simple. Assuming you are using the default JVM, pMEA, the restrictions for compiled Java programs are:

  1. Jars need to be compiled with 1.3 source compatibility.
  2. Jars need to be compiles with 1.2 class file compatibility.

Hello World

Compile and Jar this program with your preferred method of building Java programs:

public class HelloWorld { 

	/**
 	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println("Hello world!");
	}

}


Once you have your Java application in a Jar, you can copy it to the BUG and run it from a console. This example assumes there is a Linux-based pc connected to the bug via USB networking and the BUGs IP address is 10.10.10.10. This is the default setting for BUG.

pc$ scp hw.jar root@10.10.10.10:
pc$ ssh root@10.10.10.10
...
root@bug:~# java-cdc -jar /home/root/hw.jar 
Hello world!

OSGi applications

The BUG was designed from the begining to utilize OSGi for easy application development and integration with dynamic hardware services. Thus, building OSGi applications for BUG is very straight forward. As mentioned in many other places, BUG runs an OSGi framework implementation called Concierge. This framework is executed on boot. The home directory for Concierge and the BUG OSGi bundles that provide the base-level functionality of the device reside in /usr/share/java. User applications typically reside in /usr/share/java/apps and if an osgi bundled is copied into that directory, upon boot, BUG will try to start the bundle.

Dragonfly, the Eclipse-based BUG SDK, represents the easiest path to OSGi bundle development for BUG. Classpath management, application deployment, compiler settings, and other aspects of development are handled by Dragonfly. Because Concierge is an R3 OSGi runtime, some functionality does not exist. For this reason the native Eclipse OSGi tools, known as Plug-in Developer Environment or PDE, is not suitable for BUG app development with Concierge. If OSGi R4 functionality is desired, another OSGi framework can be used. From that point, other tooling besides Dragonfly SDK could also be used for application development.

Alternatives to Eclipse

We realize not everyone want's to write apps in Eclipse. We use it a lot at Bug Labs but also know that everyone has their own way of doing things. Any method of programming is supported as long as Java Jars can be made with the above-mentioned requirements. Copying OSGi Jars to /usr/share/java/apps is all that's required for deployment.

BUG Modules from Java

When creating a BUG application in the SDK, some interesting classpath containers get added to the project. Once called "BUG Libraries" contains the core OSGi bundles that provide access to BUGs modules. These bundles can be found in </tt>/usr/share/java</tt>. The pattern for hardware support is two bundles for each BUG module. One bundle is used to represent a module's high-level APIs. Typically the name for these bundles is com.buglabs.bug.module.[module name]. The second bundle is essentially the Java Native Interface (JNI) representation of the underlying Linux drivers. The naming for these bundles is com.buglabs.bug.jni. In most cases, these bundles can be called from regular Java programs as well as OSGi bundles. The high-level API bundles will have some OSGi requirements, but the JNI bundles were written specifically to not require an OSGi runtime.

Remote Debugging and Profiling

The PhoneME Advanced JVM supports remote debugging and profiling. These features are not enabled in the default build we ship with the BUG due to performance reasons. However by installing another version of pMEA these features can be used. This guide details the steps necessary to get this working.