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

Previous
Previous
 
Next
Next
 

Beta Draft: 2014-03-25

2 Using NetBeans with the Raspberry Pi Board

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

Using NetBeans with the Raspberry Pi Board

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

For complete instructions on installing Oracle Java ME SDK 8.0 EA 2, the NetBeans IDE 8.0 Beta, and Oracle Java ME SDK 8.0 EA 2 plugins for NetBeans, see the Oracle Java ME Embedded Getting Started Guide for the Windows Platform.


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 EA 2 and NetBeans IDE 8.0 Beta, and that NetBeans IDE 8.0 Beta has already been started.


Adding the Raspberry Pi Board to the Device Manager

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

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

  2. Open a Device Manager window shown in Figure 2-1 by clicking the Oracle Java ME SDK 8.0 EA Device Manager icon on the taskbar.

    Figure 2-1 Device Manager Window

    Description of Figure 2-1 follows
    Description of "Figure 2-1 Device Manager Window"

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

    Figure 2-2 Device Manager Window With Raspberry Pi Connected

    Description of Figure 2-2 follows
    Description of "Figure 2-2 Device Manager Window With Raspberry Pi Connected"

  4. Once 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-2.

Assigning the Raspberry Pi Board to Your Project

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

Once you assign the board to your project, clicking on Run Project in the NetBeans IDE 8.0 Beta 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 on your project and choose Properties.

  2. Select the Platform category on the properties window.

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

  4. Select EmbeddedExternalDevice1 from the Device list, as shown in Figure 2-3. Check (or uncheck) Optional Packages as needed for your project, and press OK.

    Figure 2-3 Adding a Device to Your Project

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

Creating a New NetBeans Project

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

  1. Select File -> New Project.

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

  3. Provide a project name, for example, ME8EmbeddedApplication1. Be sure that the Java ME Platform is Oracle Java(TM) Platform Micro Edition SDK 8.0 EA and Create MIDlet option is checked.

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

    Figure 2-4 Creating a New Project

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

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

Sample Source Code

Once the new project is created, use the source code in Example 2-1 to create a sample IMlet.java source file.

Example 2-1 Sample Code to Access a GPIO Port with NetBeans

package me8embeddedapplication1;
 
import com.oracle.deviceaccess.PeripheralManager;
import com.oracle.deviceaccess.UnavailablePeripheralException;
import com.oracle.deviceaccess.PeripheralNotFoundException;
import com.oracle.deviceaccess.gpio.GPIOPin;
import java.io.IOException;
import javax.microedition.midlet.*;
 
 
public class IMlet extends MIDlet {
 
    public void startApp() {
        
        try {
            GPIOPin pin = (GPIOPin)PeripheralManager.open(2);
            boolean b = pin.getValue();
        } catch (UnavailablePeripheralException ex) {
            ex.printStackTrace();
        } catch (PeripheralNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        
    }
    
    public void pauseApp() {
    }
    
    public void destroyApp(boolean unconditional) {
    }
}

This sample application will obtain an object representing GPIO pin 2 from the PeripheralManager, and attempt to obtain its high/low value.

Including Your Sample IMlet Code

Now you can update the generic project you created with your sample code. In the NetBeans Projects window, you see the sample project with file ME8EmbeddedApplication1.java. Do the following

  1. Right click to select ME8EmbeddedApplication1.java in the Projects window and select Properties.

  2. This displays the ME8EmbeddedApplication1.java Properties window.

    To see the path to the location of the ME8EmbeddedApplication1.java file, look in the All Files line. Make a note of this path.

  3. Take the IMlet.java file you created and copy it into the me8embeddedapplication1 directory in the file path you noted.

  4. Delete the file ME8EmbeddedApplication1.java or remove it from the directory.

  5. This changes the content of the NetBeans Project window to the content in the IMlet.java file. (If it does not double-click IMlet.java in the Projects window.)

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

  7. Run the newly cleaned and built ME8EmbeddedApplication1 project by selecting the green right-arrow icon in the NetBeans toolbar or by selecting Run > 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 -> Debug Project or use the Debug button on the toolbar.

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

Accessing Peripherals

Applications that require access to Device Access APIs must request appropriate permissions in JAD files. For more information on using the Device Access APIs, please see the Device Access API - Version 8 specification and the associated Javadocs at the following site:

http://docs.oracle.com/javame/embedded/embedded.html

Signing the Application with API Permissions

First, the JAD file must have the proper API permissions. Here is how to sign the application both in NetBeans and without an IDE.

  • In NetBeans, right-click the project name (ME8EmbeddedApplication1 in this example) and choose Properties. Select Application Descriptor, then in the resulting pane, select API Permissions. Click the Add... button, and add the com.oracle.deviceaccess.gpio API, as shown in Figure 2-5. Click OK to close the project properties dialog.

    Figure 2-5 Adding API Permissions with NetBeans

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

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

    MIDlet-Permission-1: com.oracle.deviceaccess.PeripheralMgmtPermission "*:*" "open"
    MIDlet-Permission-2: com.oracle.deviceaccess.gpio.GPIOPinPermission "*:*" "open"
    Microedition-Profile: MEEP
    

Method #1: Signing Application with a Local Certificate

The first method is more complex, but is the preferred route for applications that are widely distributed. Here are the instructions on how to setup 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 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 will generate 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. Feel free to change both passwords as you see fit.

  2. Copy the appdb/_main.ks keystore file from the Raspberry Pi over to the desktop using an sftp client or scp command 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 will import the information in mykeystore.ks you just created to the _main.ks keystore. Once this is completed, copy the certs directory back to the Raspberry Pi an sftp client or scp command.

Use the following command to sign your application before deploying to the Raspberry Pi board.

> jarsigner -keystore mykeystore.ks -storepass spass app.jad myalias

Method #2: Using NullAuthenticationProvider

This method allows to bypass a certificate check and execute unsigned applications as if they were signed and given all requested permissions. This method should be used only for development and debugging. Final testing must be done using a real certificate as described in method #1.

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

and restart the Java runtime.