Skip Headers
Oracle® Java ME Embedded Getting Started Guide for the Reference Platform (Raspberry Pi)
Release 8
E48512-03
  Go To Table Of Contents
Contents

Previous
Previous
 
Next
Next
 

2 Installing and Running Applications on the Raspberry Pi Board

Developers can run and debug IMlets on the Raspberry Pi board directly from the NetBeans IDE 8.0 Patch 1 or using the Oracle Java ME SDK 8.0. This chapter describes how to add the board to the Device Connections Manager in the Oracle Java ME SDK 8.0 and how to debug an IMlet on the board from the NetBeans IDE 8.0.

Using the Java Runtime on the Raspberry Pi

There are several ways to use the Oracle Java ME Embedded platform on the Raspberry Pi board.

  1. Directly run commands using an LXTerminal command-line shell interface or logging in using the ssh protocol.

  2. Manually start a Developer Agent program on the desktop host and run commands using the Application Management System (AMS).

  3. Run NetBeans IDE 8.0.

Running IMlets on Raspberry Pi Using the Command Shell

If you want to run IMlets directly on the Raspberry Pi board without a Developer Agent program, then you can use the commands shown in Table 2-1.

Table 2-1 Raspberry Pi Shell Commands

Syntax Description

listMidlets.sh [SUITE_ID or NAME]

List all installed IMlet suites and their status or show the detail of a single suite.

installMidlet.sh <URL> [<URL label>]

Install an IMlet using the specified JAR file.

removeMidlet.sh <SUITE_ID>

Remove an installed IMlet.

sudo runSuite.sh <SUITE_ID or NAME> [IMLET_ID or classname]

Run the specified IMlet or the default if none is specified. All logging information from the IMlet appears in the standard output of this command.



Note:

The term IMlet, in the context of the Oracle Java ME Embedded command-line interface (CLI) and references in this chapter, is synonymous with MIDlet.


The following is a typical example of using the commands to install, list, run, and remove an Oracle Java ME Embedded application on the Raspberry Pi board. Note that the runSuite.sh shell command must be preceded by the sudo request to ensure that the command can run with superuser privileges and access all the peripherals on the board. Note also that either all commands must be preceded by the sudo request or none of them. Most commands can be terminated with the Ctrl-C key combination if they become unresponsive.

First, install the application using the installMidlet.sh command, specifying its location on the local file system.

pi@raspberrypi ~/bin $ sudo ./installMidlet.sh /home/pi/EmbeddedTestProject.jar
Java is starting. Press Ctrl+C to exit
The suite was successfully installed, ID: 2

After an IMlet is installed, note its ID: in this case, it is 2. Next, verify it using the listMidlets.sh command.

pi@raspberrypi ~/bin $ sudo ./listMidlets.sh 
Java is starting. Press Ctrl-C to exit
Suite: 2
  Name: EmbeddedTestProject
  Version: 1.0
  Vendor: Vendor
  MIDlets:
    MIDlet: GPIODemo

You can run any installed IMlet using the sudo runSuite.sh command. This command runs the IMlet that was just installed, passing any logging information to the standard output of this command. Note that you can press the Ctrl-C key to exit from this command, which will terminate the application.

pi@raspberrypi ~/bin $ sudo ./runSuite.sh 2 
Java is starting. Press Ctrl-C to exit
Starting - GPIODemo

You can use the removeMidlet.sh command to remove any installed IMlet.

pi@raspberrypi ~/bin $ sudo ./removeMidlet.sh 2
Java is starting. Press Ctrl-C to exit
Suite removed
pi@raspberrypi ~/bin $ 

You can verify the results by using the listMidlets.sh command.

pi@raspberrypi ~/bin $ sudo ./listMidlets.sh 
Java is starting. Press Ctrl-C to exit
No suites installed

Starting the Developer Agent Program on the Desktop

If you want to use the Developer Agent program, then extract files from the copy of the Oracle Java ME Embedded ZIP archive on the Windows desktop and delete the /appdb and the /bin directories. The Developer Agent program is a JAR file inside the util directory of the Oracle Java ME Embedded distribution, and is named proxy.jar. You can start the Developer Agent program on the desktop host computer either in a server or a client mode. After the Developer Agent program starts, use the AMS CLI.

Server Mode Connection

A server mode is used by default. In this mode, start the Java runtime on the Raspberry Pi board to allow access to the AMS, then start the Developer Agent program. You must start the Java runtime on the Raspberry Pi board with the usertest.sh command in the command-line shell interface. Run the command using the sudo program to obtain superuser privileges to access all the peripherals on the board.

  1. Change to the bin directory on the Raspberry Pi board and run the sudo ./usertest.sh command:

    pi@raspberry ~ /pi/bin $ sudo ./usertest.sh
    
  2. Change to the util directory on your desktop host and enter the following command. You should see an output similar to the following:

    C:\mydir\util> java -jar proxy.jar -socket <RPI IP ADDRESS>
    
    Trying to open socket connection with device: <IP Address>:2201
    Connected to the socket Socket[addr=/<IP address>, port=2201, localport=54784]
    Open channel 8 with hash 0x390df07e
    notifyResponse AVAILABLE_RESPONSE on channel 8
    Channel 8 CLOSED -> AVAILABLE
    Open channel 9 with hash 0x0
    

Client Mode Connection

To switch to a client mode connection, perform the following steps.

  1. Edit the jwc_properties.ini file in the bin directory on the Raspberry Pi board as follows:

    • Set the proxy_connection_mode property to the client value.

    • Set the proxy.client_connection_address property to the IP address of the host running the Developer Agent program.

  2. Run the sudo usertest.sh command in the /bin directory:

    pi@raspberry ~ /bin $ sudo ./usertest.sh
    
  3. Change to the util directory on your desktop host and enter the following command. You should see an output similar to the following:

    C:\mydir\util> java -jar proxy.jar
    Starting with default parameters: -ServerSocketPort 2200 -jdbport 2801
    Channel 8 CLOSED -> AVAILABLE
    Waiting for device connections on port 2200
    

Running IMlets on Raspberry Pi Using the AMS CLI

The next step is to make a raw connection to the AMS CLI. Note, however, you must first run the Developer Agent program on the desktop host and the Java runtime - on the Raspberry Pi board as described in Starting the Developer Agent Program on the Desktop unless the Developer Agent was started automatically by Java ME SDK.

At this point, you can start a PuTTY executable file on your desktop computer. Use this to create raw socket connections to the IP address of the host running the Developer Agent, and port 65002. For example, a connection to localhost and the port 65002 is shown in Figure 2-1.

Figure 2-1 Using PuTTY to Connect to the Command-Line Interface

Description of Figure 2-1 follows
Description of "Figure 2-1 Using PuTTY to Connect to the Command-Line Interface"

The window from port 65002 provides a CLI as shown in Figure 2-2.

Figure 2-2 Command-Line Interface to Raspberry Pi

Description of Figure 2-2 follows
Description of "Figure 2-2 Command-Line Interface to Raspberry Pi"


Caution:

The CLI feature in this Oracle Java ME Embedded software release is provided only as a concept for your reference. It uses connections that are not secure, without encryption, authentication, or authorization.


You can use the command-line interface to run the AMS commands shown in Table 2-2.

Table 2-2 AMS CLI Commands

Syntax Description

ams-list [INDEX or NAME|VENDOR]

List all installed IMlet suites and their status or show the detail of a single suite.

ams-install <URL> [username:password][hostdownload]

Install an IMlet using the specified JAR or JAD file, specified as a URL. An optional user name and password can be supplied for login information either in the URL or by the auth parameter. When run without the hostdownload option, only http:// URLs must be specified. The hostdownload option enables you to install an IMlet using the JAR file specified by the file:// URL. Note that the JAR file must be located on the host.

ams-update <INDEX or NAME|VENDOR>

Update the installed IMlet.

ams-remove <INDEX or NAME|VENDOR>

Remove an installed IMlet.

ams-run <INDEX or NAME|VENDOR> [IMLET_ID] [-debug]

Run the specified IMlet or the default if none is specified. You can specify optional debug parameter to run the IMlet in debug mode.

ams-stop <INDEX or NAME|VENDOR> [IMLET_ID]

Stop the specified IMlet or the default if none is specified.

ams-info <INDEX or NAME|VENDOR>

Show information about the installed IMlet.


Here is a typical example of using the AMS to install, list, run, and remove an Oracle Java ME Embedded application on the board:

oracle>> ams-install file:///C:/some/directory/hello.jar hostdownload
<<ams-install,start install,file:///C:/some/directory/hello.jar
<<ams-install,install status: stage DONE, 0%
<<ams-install,install status: stage DONE, 100%
<<ams-install,OK,Install success

oracle>> ams-install http://www.example.com/netdemo.jar
<<ams-install,start install,http://www.example.com/netdemo.jar
<<ams-install,install status: stage DONE, 0%
<<ams-install,install status: stage DONE, 100%
<<ams-install,OK,Install success

oracle>> ams-install http://www.example.com/notthere.jar
<<ams-install,start install,http://www.example.com/notthere.jar
<<ams-install,FAIL,errorCode=103 (OTHER_ERROR)

Note that the final installation example failed with an error code and matching description.

Similarly, install an additional IMlet: rs232dem. After an IMlet is installed, verify it using the ams-list command. Each IMlet has been assigned a number by the AMS for convenience.

oracle>> ams-list
<<ams-list,0.hello|Oracle,STOPPED
<<ams-list,1.netdemo|Oracle,STOPPED
<<ams-list,2.rs232dem|Oracle,RUNNING
<<ams-list,OK,3 suites are installed

You can use the ams-remove command to remove any installed IMlet.

oracle>> ams-remove 0
<<ams-remove,OK,hello removed

The results can again be verified with the ams-list command.

oracle>> ams-list
<<ams-list,1.netdemo|Oracle,STOPPED
<<ams-list,2.rs232dem|Oracle,RUNNING
<<ams-list,OK,2 suites are installed

Finally, start the IMlet using the ams-run command. The application can be terminated with the ams-stop command.

oracle>> ams-run 1
<<ams-run,OK,started
oracle>> ams-list
<<ams-list,1.netdemo|Oracle,RUNNING
<<ams-list,2.rs232dem|Oracle,RUNNING
<<ams-list,OK,2 suites are installed

You can use the properties commands summarized in Table 2-3 and file system commands summarized in Table 2-4.

Table 2-3 Security and Properties Commands

Syntax Description

help [command name]

List the available commands or detailed usages for a single command.

properties-list

Show the list of names of properties which control the Java ME runtime (properties that are set in the properties.ini file).

get-property <NAME> [-i]

Return a value of the property identified by <NAME>.

set-property <NAME> <VALUE>

Set a property identified by <NAME> with the value <VALUE>.

save-properties

Save properties to an internal storage.

blacklist -client <NAME>

blacklist -app <NAME|VENDOR>

Blacklist clients and applications.


Table 2-4 File System Commands

Syntax Description

cd <deviceDirectoryName>

Change the working directory on the device.

delete <deviceFileName>

Delete a file on the device.

get <deviceFileName> <hostFileName>

Copy a file from the device to the host.

ls [<deviceDirectoryName>]

Display a list of files and subdirectories in a device directory. In a result listing, subdirectories are marked with a trailing file separator symbol, such as "\" on Windows and "/" on the Raspberry Pi board.

mkdir <deviceDirectoryName>

Create a directory on the device.

pwd

Write the current working directory on the device.

put <hostFileName> <deviceFileName>

Copy a local host file to the device.


The CLI supports working with multiple devices. You can use the device commands summarized in Table 2-5.

Table 2-5 Device Commands

Syntax Description

device-list

List all connected devices.

device-change <INDEX>

Make the specified device current.

shutdown [-r]

Perform either a shutdown of the board or a reboot if the -r parameter has been passed.

exit

Terminate the current CLI session.


You can use the keystore commands summarized in Table 2-6.

Table 2-6 Keystore Commands

Syntax Description

ks-delete (-owner <owner name> | -number <key number>)

Delete a key from a ME store.

ks-export -number <key number> -out <full file name>

Export a key from a device keystore by index.

ks-import [-keystore <filename>] [-storepass <password>] [-keypass <password>] [-alias <key alias>]

Import a public key from a JCE keystore into a ME keystore.

ks-list

List the owner and validity period of each key in a ME keystore.


Using NetBeans with the Raspberry Pi Board

Running and debugging IMlet projects on the Raspberry Pi board using the NetBeans IDE 8.0 requires the following software:

For complete instructions about installing Oracle Java ME SDK 8.0, the NetBeans IDE 8.0, and Oracle Java ME SDK 8.0 plug-ins for NetBeans, see Oracle Java ME SDK Developer's Guide.


Note:

This chapter assumes that the Raspberry Pi board is already set up and connected to the Windows platform running Oracle Java ME SDK 8.0 and that NetBeans IDE 8.0 has already been started.


Adding the Raspberry Pi Board to the Device Connection Manager

Follow these steps to add the Raspberry Pi board to the Device Connections Manager in Oracle Java ME SDK 8.0:

  1. Ensure that the sudo. /usertest.sh script in the /bin directory is running on the Raspberry Pi board.

  2. Ensure that the Developer Agent program does not run on the desktop computer.

  3. Start the Oracle Java ME SDK 8.0 Device Connections Manager (located at <SDK Installation Folder>/bin/device-manager.exe) and click its icon in the taskbar. A Device Connections Manager window is shown in Figure 2-3.

    Figure 2-3 Device Connections Manager Window

    Description of Figure 2-3 follows
    Description of "Figure 2-3 Device Connections Manager Window"

  4. Click the Add button, ensure that the IP Address or Host Name list contains the correct IP address of the Raspberry Pi board, and click OK.

  5. After the Raspberry Pi board is registered, its IP address is listed on the Device Connections Manager list and its status is Connected as shown in Figure 2-4.

    Figure 2-4 Device Connections Manager Window with Raspberry Pi Connected

    Description of Figure 2-4 follows
    Description of "Figure 2-4 Device Connections Manager Window with Raspberry Pi Connected"

Assigning the Raspberry Pi Board to Your Project

There are two ways to assign the Raspberry Pi board to your project:

After you assign the board to your project, clicking Run Project in the NetBeans IDE 8.0 runs your IMlets on the board instead of on the emulator.

Using an Existing NetBeans Project

If you already have an existing NetBeans project with an IMlet that you want to run or debug on the board, follow these steps:

  1. Right-click your project and select Properties.

  2. Select the Platform category on the properties window.

  3. Select the Platform Type (CLDC) and ensure that Oracle Java(TM) Platform Micro Edition SDK 8.0 is selected in the Java ME Platform list.

  4. Select EmbeddedExternalDevice1 from the Device drop-down list, as shown in Figure 2-5. Select (or deselect) from the list of Optional Packages as needed for your project, and click OK.

    Figure 2-5 Adding a Device to Your Project

    Description of Figure 2-5 follows
    Description of "Figure 2-5 Adding a Device to Your Project"

Creating a New NetBeans Project

If you are creating a new NetBeans project, follow these steps:

  1. Select File then New Project.

  2. Select the Java ME Embedded category and Java ME 8 Embedded Application in Projects pane. Click Next.

  3. Provide a project name, for example, ME8EmbeddedApplication1. Ensure that the Java ME Platform is Oracle Java(TM) Platform Micro Edition SDK 8.0 and the Create Midlet option is selected.

  4. Select EmbeddedExternalDevice1 from the Device drop-down list and click Finish, as shown in Figure 2-6.

    Figure 2-6 Creating a New Project

    Description of Figure 2-6 follows
    Description of "Figure 2-6 Creating a New Project"

When the new project is created, it is displayed in NetBeans IDE with the name ME8EmbeddedApplication1.

Sample Source Code

Now you can update the generic project that you created with the sample code shown in the following example. This sample application obtains an object representing GPIO pin 2 from the DeviceManager object, and tries to obtain its high/low value.

package me8embeddedapplication1;
 
import jdk.dio.DeviceManager;
import jdk.dio.UnavailableDeviceException;
import jdk.dio.gpio.GPIOPin;
import java.io.IOException;import java.util.logging.Level;import java.util.logging.Logger;
import javax.microedition.midlet.*;
 
 
public class ME8EmbeddedApplication1 extends MIDlet {
 
    @Override
    public void startApp() {
        
        try {
            GPIOPin pin = (GPIOPin)DeviceManager.open(2);
            boolean b = pin.getValue();
        } catch (UnavailableDeviceException ex) {
        } catch (IOException ex) {
            Logger.getLogger(ME8EmbeddedApplication1.class.getName()).log(
Level.SEVERE, null, ex);
        }
        
    }
    
    public void pauseApp() {
    }
    
    public void destroyApp(boolean unconditional) {
    }
}

In the NetBeans Projects window, you see the sample project with the file ME8EmbeddedApplication1.java. Follow these steps:

  1. Double-click the ME8EmbeddedApplication1.java file in the Projects window.

  2. Copy the sample code and paste it in the Source window.

  3. Clean and build the ME8EmbeddedApplication1 project by clicking on the hammer-and-broom icon in the NetBeans toolbar or by selecting Run then Clean and Build Project (ME8EmbeddedApplication1).

  4. Run the newly cleaned and built ME8EmbeddedApplication1 project by selecting the green right-arrow icon in the NetBeans toolbar or by selecting Run then Run Project (ME8EmbeddedApplication1).

    When the run is successful, the EmbeddedExternalDevice1 emulator starts with the ME8EmbeddedApplication1 suite running.

Debugging an IMlet on the Raspberry Pi Board

Follow these steps to debug an IMlet using NetBeans:

  1. Open your IMlet class on the NetBeans editor.

  2. Click once directly on the line number where you want to set a breakpoint. The line number is replaced by a red square and the line is highlighted in red.

  3. Select Debug then Debug Project or use the Debug button on the toolbar.

The debugger connects to the debug agent on the board and the program stops at your breakpoint.

Accessing Peripherals

Applications that require access to Device I/O APIs must request appropriate permissions in JAD files. For more information about using the Device I/O APIs, see the Device I/O API 1.0 specification at:

http://docs.oracle.com/javame/8.0/api/dio/api/index.html

Signing the Application with API Permissions

The JAD file must have the proper API permissions. Follow these steps to sign the application both in NetBeans and without an IDE:

  1. In NetBeans, right-click the project name (ME8EmbeddedApplication1 in this example) and select Properties.

  2. Click Application Descriptor, then in the resulting pane, click API Permissions.

  3. Click the Add button, and add the jdk.dio.DeviceMgmtPermission API, as shown in Figure 2-7.

  4. Click OK to close the project properties dialog.

    Figure 2-7 Adding API Permissions with NetBeans

    Description of Figure 2-7 follows
    Description of "Figure 2-7 Adding API Permissions with NetBeans"

  5. If you are not using an IDE, then manually modify the application descriptor file to contain the following permissions:

    MIDlet-Permission-1: jdk.dio.DeviceMgmtPermission "*:*" "open"
    

Method #1: Signing Application Using the NetBeans IDE

The NetBeans IDE enables developers both to sign the applications with a local certificate and upload the certificate on the device. Follow these steps:

  1. Right-click the project name and select Properties.

  2. Under the Build category, click Signing.

  3. Select Sign JAR and specify a certificate to sign with as shown in Figure 2-8.

    Figure 2-8 Signing Application JAR with NetBeans

    Description of Figure 2-8 follows
    Description of "Figure 2-8 Signing Application JAR with NetBeans"


    Note:

    The selected certificate must be uploaded on the device and associated with the security client.


  4. Click Open Keystores Manager.

  5. Select the key and click Export as shown in Figure 2-9.

    Figure 2-9 Keystores Manager Window

    Description of Figure 2-9 follows
    Description of "Figure 2-9 Keystores Manager Window"

  6. In the Export Key window, select the EmbeddedExternalDevice1 in the Emulator drop-down list, select the certificate in the Security Domain drop-down list, and click Export as shown in Figure 2-10.

    Figure 2-10 Exporting Key on a Device

    Description of Figure 2-10 follows
    Description of "Figure 2-10 Exporting Key on a Device"

  7. Select Tools then Java ME and then Device Selector. The Device Selector window opens.

  8. In the Device Selector window, right-click EmbeddedExternalDevice1 and select Edit Security Policy. This opens the Security Policy window shown in Figure 2-11.

    Figure 2-11 Editing Security Policy

    Description of Figure 2-11 follows
    Description of "Figure 2-11 Editing Security Policy"

  9. Ensure that the certificate is associated with the security client. Select the security client or click Add to add the security client.

  10. For the selected security client, ensure that the certificate with the specified common name (CN) is listed on the list of certificates. If not, click Add to add the certificate.

Method #2: Signing Application Using a Command Line

If you are not using the NetBeans IDE, then you can sign your application using the command line. Follow the instructions on how to set up a keystore with a local certificate that can be used to sign the applications:

  1. Generate a new self-signed certificate with the following command on the desktop, using the keytool that is shipped with the Oracle Java SE JDK.

    keytool -genkey -v -alias mycert -keystore mykeystore.ks -storepass spass -keypass kpass -validity 360 -keyalg rsa -keysize 2048 -dname "CN=thehost"

    This command generates a 2048-bit RSA key pair and a self-signed certificate, placing them in a new keystore with a keystore password of spass and a key password of kpass that is valid for 360 days. You can change both passwords as desired.

  2. Copy the certs directory from the Raspberry Pi board over to the desktop using an sftp client or scp command, change into the certs directory, and perform the following command using the mekeytool.exe command (or alternatively java -jar MEKeyTool.jar... if your distribution contains only that) that ships with the Oracle Java ME SDK 8 distribution.

    {mekeytool} -import -MEkeystore _main.ks -keystore mykeystore.ks -storepass spass -alias mycert -domain trusted

    This command imports the information in mykeystore.ks that you just created to the _main.ks keystore. After this is completed, copy the certs directory back to the Raspberry Pi board by using an sftp client or scp command.

Use the following commands to sign your application before deploying it to the Raspberry Pi board:

jadtool -addcert -chainnum 1 -alias myalias -keystore mykeystore.ks -storepass spass -inputkad myjad.jad -outputjad myjad.jad

jadtool -addjarsig -chainnum 1 -jarfile myjar.jar -alias myalias -keystore mykeystore.ks -storepass spass -keypass kpass -inputjad myjad.jad -outputjad myjad.jad

Method #3: Using NullAuthenticationProvider

This method allows you to bypass a certificate check and run unsigned applications as if they were signed and given all requested permissions. Use this method only for development and debugging. Perform final testing using a real certificate as described in method #1 or #2.

  1. Stop the Java runtime.

  2. To use NullAuthenticationProvider, set the following property in the jwc_properties.ini file on the Raspberry Pi board:

    [internal]
    authentication.provider = com.oracle.meep.security.NullAuthenticationProvider
    
  3. Restart the Java runtime.