站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Netbeans 专题Deploy and Run Java ME Applications using Sun Java Application Server

Deploy and Run Java ME Applications using Sun Java Application Server

摘要: This Tech tip describes how to deploy and run a Java ME application using Sun Java Application server and over-the-air (OTA) provisioning. We will create a web application that's wrapped around the mobile application that we want to make accessible over the air. Then we will download the mobile application from the webserver and install the application on the mobile device.

Setting up the Software

Before you begin, you have to make sure you have all of the necessary software installed, and that your project is set up correctly.

Installing the Software

Make sure the following software is installed on your computer:

  • NetBeans IDE 5.0 with Mobility Pack (download).
  • Java Standard Development Kit (JDKTM) version 1.4.2 (download) or 5.0 (download)
  • Sun Java System (SJS) Application Server (download)
  • Java Wireless Toolkit (download)

Registering the Web Server

Before you can deploy to the SJS Application Server, you have to register a local instance with the IDE. Note that if you installed the NetBeans IDE 5.0/SJS Application Server bundle, the local SJS Application Server is already registered automatically.

  1. Choose Tools > Server Manager from the main window.
  2. Click Add Server. Select your server and give a name to the instance. Then click Next.
  3. Specify the server information, the location of the local instance of the application server, and the domain to which you want to deploy.

Building the Mobile Application

In this tech tip, I will use one of the sample Java ME application included in the IDE. You can use any of your own mobile applications instead.

  1. To open the sample project, select File > New Project from the menu.
  2. In Categories, select Samples > Mobile > MIDP 2.0 Sample > Various Games for MIDP 2.0. Click Next.
  3. In the Name field, enter VariousGames. Click Finish. The sample application VariousGames opens in the Projects Window.
  4. Build the VariousGames project.

When you built the project, the IDE created two new directories to store the executables in, dist and build.

Prepare your Mobile Application to be Deployed Remotely

To prepare the VariousGames application to be deployed remotely, you need to change the JAD file's MIDlet-Jar-URL property to specify the URL of the remote location of the MIDlet's JAR file.

  1. Select Window > Files from the menu to open the Files window. Open the VariousGames node. Note the dist directory contains two executables, VariousGames.jar and VariousGames.jad.

  2. Double-click /dist/VariousGames.jad to open it in the Editor. Find the MIDlet-Jar-URL line:

    MIDlet-Jar-URL: VariousGames.jar
  3. Change this line to reflect your MIDlet's JAR file's remote location. The location is a URL consisting of your web server's address, the SJS port, and your web application's root context path: http://yourWebServerAddress:port/yourAppRootContextPath/yourApplicationName.jar

    In my example, I want to name my web application MyGames, and my application server's port is 8164. So the correct line looks like this:

    MIDlet-Jar-URL: http://127.0.0.1:8164/MyGames/VariousGames.jar

Create the Web Application

  1. Create a new Web Application using the NetBeans New Project wizard.

    1. Select File > New Project from the menu. In the New Project wizard's Web category, select Web Application. Click Next.
    2. Enter a name for the project in the Project Name field. In this example, I will use the name MyGames. Note: This name is also the default context root for your web application on the server.
    3. Select Sun Java System Application Server from the Server pop-up menu.
    4. Leave all other settings at their default values and click Finish to create the new web application project.

    Open the Files Window in the IDE and open the MyGames node. The web directory you see here is your web application's root directory.

  2. Next we want copy the mobile application's JAD and JAR files to the web application's root directory. Locate the files VariousGames/dist/VariousGames.jad and VariousGames/dist/VariousGames.jar and copy both of them into MyGames/web/ directory.

  3. In MyGames/web, create or edit the existing index page to contain a link to either the JAD file or JAR file depending on which version of MIDP you use:

    • MIDP 1.0 OTA only supports the use of JAD files.
    • MIDP 2.0 supports both JAD and JAR files.

    If the URL refers to a JAR file, the emulator's Application Management Software (AMS) will use that URL to download the JAR. If the URL refers to a JAD file, the AMS downloads the JAD, which contains the JAR's URL, and then uses that URL to download the JAR. Either way, the AMS begins installation as soon as download is complete.

    It is important to mention that, even though the MIDP 2.0 specification does not require developers to provide a JAD file (the URL can point to the MIDlet suite directly), it does recommend providing one: This is because the JAD descriptor helps the AMS determine whether the device has sufficient resources for the application to be installed successfully, even before the download takes place.

Examples

Look at the following examples to see what the index page should be like.

  • Example index.html

    <html>
    <head>
    <title>My Games - Download</title>
    </head>

    <body>

    <a href="http://127.0.0.1:8164/MyGames/VariousGames.jad">My Games</a>

    </body>
    </html>
  • This is the equivalent of index.html in WML format for WAP pages:
    <?xml version="1.0"?>
    <!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML
    1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
    <wml>
    <head>
    <meta forua="true" http-equiv="Cache-Control"
    content="must-revalidate, no-store"/>
    </head>
    <template>
    <do type="prev" label="Back">
    <prev/>
    </do>/
    </template>
    <card title="My Games - Download" id="main">
    <p> Games:</p>
    <p align="left">
    <a href="http://127.0.0.1:8164/MyGames/VariousGames.jad">My Games</a></p>
    </card>
    </wml>

Notes

  • You do not need to add the MIME types for the JAD and the JAR because Sun Java Application Server includes them in its Default MIME Type Mappings.

  • In some devices, including those from Nokia and Siemens, the discovery application uses a WAP browser, but it can be a native application as well. In Sun Java Wireless Toolkit, the discovery application includes a basic HTML browser.

Build and Deploy the Web Application

  1. Return to the Projects Window. Build the web application and deploy the .war file to the the Sun Applications server.

    Right-click on the Web Project and select Deploy from the context menu. Wait for the IDE to compile the application, build the MyGames.war file, start the web server (in case it is not running yet) and deploy the application.

  2. Verify that the application was deployed correctly: In your browser, type in the URL to the JAD file, e.g.
    http://127.0.0.1:8164/MyGames/VariousGames.jad
    If everythings correct, the browser should display the JAR file or open a dialog suggesting to save it to your local disk. (You do not need to actually save the file to your disk.)

Execute the Application Using Sun Java Wireless Toolkit 2.2

The Sun Java Wireless Toolkit 2.2 implements the device behavior during OTA provisioning. You can use the emulator to test and demonstrate the full provisioning process of MIDlet suites from a server to the device. All you need to do is launch the emulator's Application Management Software (AMS).

    The emulator shows the welcome screen of its AMS.
  1. Start the emulator. To launch the emulator's Application Management Software (AMS), select Start > Programs > J2ME Wireless Toolkit 2.2 > OTA Provisioning from the Windows start menu. To use the emulator executable from the commandline, run

    [WTK2.2-intallpath]\bin\emulator.exe -Xjam
    The emulator window pops open, but instead of running your MIDlet classes directly, the emulator displays the welcome screen of its AMS as shown in the screenshot.

  2. In the emulator window, select Apps (visible at the bottom right of the display) to go to the main list of installed applications.

  3. In the list of installed applications, select Install Application and press the Select button on the emulator. The emulator now prompts you for the URL location of the application you want to install.

  4. In the URL field, enter the URL of your web application's index.html page. In our example, you would type: http://127.0.0.1:8164/MyGames/

  5. Select Go from the emulator's menu to begin the installation. The emulator connects to the URL, downloads and reads the HTML document (index.html), parses the hyperlinks, and displays a list of MIDlets you can download from this page. In our example, VariousGames.jad is displayed on the emulator.

  6. Select the JAD and select Install from the emulator's menu. When the emulator asks you for your confirmation, select Install again to confirm and complete the installation. The emulator will download and verify the application and tell you when it was successfully installed. You'll be returned to the emulator's list of installed applications, which now includes the application you just installed — games.

  7. Once the application is installed, running it is simple. Just click the application from the list and select Launch from the menu.

References:


↑返回目录
前一篇: Defining and Using Preprocessor Directives in the Mobility Pack
后一篇: Derby Tutorial