On the 31st issue of the magazine, we introduced the topic of the Internet of Things from the point of view of the Java platform. As we promised, we will continue this discussion presenting the details of creating a Java ME Embedded 8 project. This version represents an important step further, with the adoption of CLDC 8 - which is a subset of Java Standard Edition - and MEEP 8 - a specification which defines a powerful and flexible environment for small embedded systems. Along with these, we have to notice the alignment of the platform we're talking about to Java SE 8.
The Micro Edition applications can be developed both manually and with the help of a dedicated SDK, known as the Oracle Java ME SDK 8.1. The SDK, the Java ME distribution, the documentation and any other necessary resources for developing embedded applications can be found at Oracle's dedicated page
Before starting to develop embedded applications, Oracle recommends first and foremost the installation of the Oracle Java ME SDK 8.1 - a software package which is only available for Windows at the moment of writing this article. The next step is downloading the Java ME Embedded 8 distribution targeted to the device we want to run the applications on. It comes as a ZIP archive and it has to be transferred to the device - using a tool such as PuTTY - where it is about to be installed. Also, it is recommended to keep a copy of this distribution on the desktop system as well, where we will develop the applications. This step is not mandatory, but it is recommended for the case when the developer would like to execute commands on the device using the AMS CLI (Application Management System Command Line) through the Developer Agent.
Once we installed the SDK and Java ME, we can start developing embedded applications. In its simplest form, such an application has to contain a class which extends the `javax.microedition.midlet.MIDlet` abstract class, a manifest file and a JAD (Java Application Descriptor) file. We could write such an application manually, without the help of an IDE that would automate certain steps. This is a good exercise for the developer who makes contact for the first time with Java ME Embedded 8. Thus we intend to create a simple application which displays at the console one message at start-up and one at shut-down time.
First we create a class which extends javax.microedition.midlet.MIDlet, implementing the startApp() and destroyApp(boolean unconditional) methods:
public class Hello extends javax.microedition.midlet.MIDlet {
public void startApp() {
System.out.println("Hello MIDlet");
}
public void destroyApp(boolean
unconditional) {
System.out.println(
"Goodbye MIDlet");
}
}
To continue on the same note, we compile the class we just wrote manually using a command similar to the one listed here:
javac -cp %JAVA\_ME\_SDK%\lib\meep\_8.0.jar -d classes
src\\ro\\leje\\Hello.java
We notice that we need the meep\_8.0.jar
library at compile time, because it
defines the MIDlet
abstract class. This library can be found inside the
%JAVA\_ME\_SDK%\lib
directory, where %JAVA\_ME\_SDK%
represents the
path where the SDK was installed.
The next step is to create the manifest file, which has to contain at least the following information:
MIDlet-Name: Hello
MIDlet-Version: 1.0
MIDlet-Vendor: Today Software Magazine
MIDlet-1: Hello,,ro.leje.Hello
MicroEdition-Configuration: CLDC-1.8
MicroEdition-Profile: MEEP-8.0
Supposing we saved the manifest file using the `MANIFEST.MF` name, we can move forward to the next step, which is creating the JAR archive. This archive has to contain the compiled class and the manifest file. We do that launching the following command:
jar cfm build\\Hello.jar MANIFEST.MF -C classes .
Once we created the `Hello.jar` file, for this application we only need one more thing, and that is the application's descriptor, also known as a JAD (Java Application Descriptor) file. This one resembles the manifest file, containing the following mandatory lines:
MIDlet-Name: Hello
MIDlet-Version: 1.0
MIDlet-Vendor: Today Software Magazine
MIDlet-1: Hello,,ro.leje.Hello
MIDlet-Jar-Size: 1076
MIDlet-Jar-URL: Hello.jar
MicroEdition-Profile: MEEP-8.0
We notice that one of the mandatory attributes of the JAD file is the size in bytes of the archive. We save this file with the `Hello.jad` name within the same directory as the JAR file.
At this moment, we have a completed Java ME Embedded 8 application that we can launch. The simplest method for doing this is using the emulator provided by the SDK. Supposing that we added the `%JAVA_ME_SDK%\bin` path to the `PATH` environment variable and that the current directory is the `build` directory of our project, we can issue the following command:
emulator -Xdevice:EmbeddedDevice1 -Xdescriptor:Hello.jad
Using the `-Xdevice` option we specify the name of the device where we want to run the application. `EmbeddedDevice1` is an emulated device, automatically configured during the installation of the SDK. The second option present in the command we just issued is `-Xdescriptor`. By it we specify the location and the name of our application's descriptor.
We can notice the result of running the application in the next figure:
The Hello MIDlet message is displayed when the MIDlet is started and the Goodbye MIDlet message is displayed when it is destroyed, using the `Ctrl + C` key combination.
Another way to run the application is by installing it on the target device. For this article, we used a Raspberry PI Model B+ development board equipped with a Wi-Fi dongle. Also, we have to note that the platform has a Linux operating system already installed, which facilitates our work with this piece of hardware. Having this configuration at our disposal, we can remotely access the Raspberry PI system with the help of a program such as PuTTY.
We access the board by knowing its IP and a user with root rights. Once we are authenticated, we change the current directory so that we are located inside the `bin` directory of the Java ME Embedded 8 installed distribution. This is illustrated in the following screen shot:
Accessing the development board with PuTTY
The figure shows us the fact that the next step was to run the `usertest.sh` script using the `sudo ./usertest.sh` command. This script has launched the Java runtime which grants access to the AMS. The platform listens on the 2201 port and allows us to connect remotely to perform the management of ME applications.
Before moving on, we have to register the device using the Device Connections Manager application. Since the SDK is installed, we should be able to find an icon called Oracle Java(TM) ME SDK 8.1 Device Manager within the SysTray. Clicking on it, we open the connections manager where we can add our board which is identified by IP. Once we registered the device, we should see within the active window something similar to the following figure:
Now we can install on the Raspberry PI the application we created in a previous section. We can choose from a few methods for doing that. One of these methods is using the Device Selector utility which is available for us, of course, due to the fact that we previously installed the SDK. Starting this application, we can notice a list with all the devices installed at the current moment. By default, there are three emulated devices available: EmbeddedDevice1, EmbeddedDevice2 and Qualcomm_IoE_Device. Along with these, we can also see the Raspberry PI board that we just registered under the EmbeddedExternalDevice1 name. We want to run the Hello application on this device so we right-click on it and we choose Run JAR or JAD..., as it is depicted in the next figure:
Running the application on the device
By choosing the `Hello.jad` file from the disk with the help of the Open dialog that appeared, the platform launches the application manager which installs our software package on the board. This can be seen in the next screen shot:
The fact that our Java ME Embedded 8 application is truly running on the device is proved by the PuTTY console where we can see the output of starting and destroying the application:
The output of the application within the console
We notice that in the screen shot the message that should be displayed when the application is destroyed is also shown. That happened because, before taking the screen shot, we clicked the Remove button, which is available on the application manager's user interface.
Thus, whichever the chosen method for running Java ME Embedded 8 applications is, whether it is on devices or on an emulator, this is a fairly easy process. There is one more way of running embedded applications on devices, using the AMS command line, by the means of the Developer Agent utility. However, this method is still a concept in this version of the platform.
Throughout this article, we followed the steps of creating, installing and running a minimal application, performing each task manually. However, the platform offers us a multitude of tools that are meant to meet the needs of the developers and aim at increasing their productivity. In a future article we intend to present the process from a different angle, automated using these tools.
The development of Java ME Embedded 8 applications can be an interesting and fun process, but sometimes difficult and frustrating. Unfortunately, the study materials targeted for Java ME Embedded 8 are few. As of this writing, there is one published book that approaches the topic of developing Java ME Embedded 8 applications. Also, the majority of the existing articles on this subject take a general approach, presenting the features of this new platform in a general sense. For now, the forum threads are relatively few but we can notice an increasing interest from the users' side, eager to learn how to use this new version. The richest in information resources available for us are the official guides compiled by Oracle and they can be downloaded from the official site of the platform.