Oracle® Java ME Embedded Getting Started Guide for the Reference Platform (Raspberry Pi) Release 8 E48512-03 |
|
Previous |
Next |
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.
There are several ways to use the Oracle Java ME Embedded platform on the Raspberry Pi board.
Directly run commands using an LXTerminal command-line shell interface or logging in using the ssh
protocol.
Manually start a Developer Agent program on the desktop host and run commands using the Application Management System (AMS).
Run NetBeans IDE 8.0.
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 |
---|---|
|
List all installed IMlet suites and their status or show the detail of a single suite. |
|
Install an IMlet using the specified JAR file. |
|
Remove an installed IMlet. |
|
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
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.
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.
Change to the bin
directory on the Raspberry Pi board and run the sudo ./usertest.sh
command:
pi@raspberry ~ /pi/bin $ sudo ./usertest.sh
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
To switch to a client mode connection, perform the following steps.
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.
Run the sudo usertest.sh
command in the /bin
directory:
pi@raspberry ~ /bin $ sudo ./usertest.sh
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
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
The window from port 65002 provides a CLI as shown in Figure 2-2.
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 |
---|---|
|
List all installed IMlet suites and their status or show the detail of a single suite. |
|
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 |
|
Update the installed IMlet. |
|
Remove an installed IMlet. |
|
Run the specified IMlet or the default if none is specified. You can specify optional debug parameter to run the IMlet in debug mode. |
|
Stop the specified IMlet or the default if none is specified. |
|
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 |
---|---|
|
List the available commands or detailed usages for a single command. |
|
Show the list of names of properties which control the Java ME runtime (properties that are set in the |
|
Return a value of the property identified by |
|
Set a property identified by |
|
Save properties to an internal storage. |
|
Blacklist clients and applications. |
Table 2-4 File System Commands
Syntax | Description |
---|---|
|
Change the working directory on the device. |
|
Delete a file on the device. |
|
Copy a file from the device to the host. |
|
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. |
|
Create a directory on the device. |
|
Write the current working directory on the device. |
|
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 |
---|---|
|
List all connected devices. |
|
Make the specified device current. |
|
Perform either a shutdown of the board or a reboot if the |
|
Terminate the current CLI session. |
You can use the keystore commands summarized in Table 2-6.
Table 2-6 Keystore Commands
Syntax | Description |
---|---|
|
Delete a key from a ME store. |
|
Export a key from a device keystore by index. |
|
Import a public key from a JCE keystore into a ME keystore. |
|
List the owner and validity period of each key in a ME keystore. |
Running and debugging IMlet projects on the Raspberry Pi board using the NetBeans IDE 8.0 requires the following software:
NetBeans IDE 8.0 with Java ME 8 support
Oracle Java ME SDK 8.0
Oracle Java ME SDK 8.0 plugins
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. |
Follow these steps to add the Raspberry Pi board to the Device Connections Manager in Oracle Java ME SDK 8.0:
Ensure that the sudo. /usertest.sh
script in the /bin
directory is running on the Raspberry Pi board.
Ensure that the Developer Agent program does not run on the desktop computer.
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
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.
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
There are two ways to assign the Raspberry Pi board to your project:
Using an existing NetBeans Project with an IMlet you want to run or debug.
Creating a new NetBeans 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.
If you already have an existing NetBeans project with an IMlet that you want to run or debug on the board, follow these steps:
Right-click your project and select Properties.
Select the Platform category on the properties window.
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.
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
If you are creating a new NetBeans project, follow these steps:
Select File then New Project.
Select the Java ME Embedded category and Java ME 8 Embedded Application in Projects pane. Click Next.
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.
Select EmbeddedExternalDevice1 from the Device drop-down list and click Finish, as shown in Figure 2-6.
When the new project is created, it is displayed in NetBeans IDE with the name ME8EmbeddedApplication1
.
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:
Double-click the ME8EmbeddedApplication1.java
file in the Projects window.
Copy the sample code and paste it in the Source window.
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).
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.
Follow these steps to debug an IMlet using NetBeans:
Open your IMlet class on the NetBeans editor.
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.
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.
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
The JAD file must have the proper API permissions. Follow these steps to sign the application both in NetBeans and without an IDE:
In NetBeans, right-click the project name (ME8EmbeddedApplication1 in this example) and select Properties.
Click Application Descriptor, then in the resulting pane, click API Permissions.
Click the Add button, and add the jdk.dio.DeviceMgmtPermission
API, as shown in Figure 2-7.
Click OK to close the project properties dialog.
Figure 2-7 Adding API Permissions with NetBeans
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"
The NetBeans IDE enables developers both to sign the applications with a local certificate and upload the certificate on the device. Follow these steps:
Right-click the project name and select Properties.
Under the Build category, click Signing.
Select Sign JAR and specify a certificate to sign with as shown in Figure 2-8.
Figure 2-8 Signing Application JAR with NetBeans
Note: The selected certificate must be uploaded on the device and associated with the security client. |
Click Open Keystores Manager.
Select the key and click Export as shown in Figure 2-9.
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.
Select Tools then Java ME and then Device Selector. The Device Selector window opens.
In the Device Selector window, right-click EmbeddedExternalDevice1 and select Edit Security Policy. This opens the Security Policy window shown in Figure 2-11.
Ensure that the certificate is associated with the security client. Select the security client or click Add to add the security client.
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.
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:
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.
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
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.
Stop the Java runtime.
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
Restart the Java runtime.