ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

MIDlet Packaging with J2ME
Pages: 1, 2, 3, 4, 5

Packaging Multiple MIDlets in the MIDlet Suite



Before we can talk about how to package more than one MIDlet, we'll need to have more than one MIDlet available. What follows is code for two simple MIDlets. The first displays a TextBox component with a short message.

Contents of MIDlet1.java:

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class MIDlet1 extends MIDlet implements CommandListener
{
  private Display display;  // Reference to Display object
  private TextBox tbxMain;      // A Textbox to display a message
  private Command cmdExit;      // A Command to exit the MIDlet

  // The constructor
  public MIDlet1()
  {
    display = Display.getDisplay(this);
    
    cmdExit = new Command("Exit", Command.SCREEN, 1);
    
    tbxMain = new TextBox("MIDlet 1", "Welcome", 50, 0);
    tbxMain.addCommand(cmdExit);
    tbxMain.setCommandListener(this);
  }

  // Called by application manager to start the MIDlet.
  public void startApp()
  {
    display.setCurrent(tbxMain);
  }

  // A required method
  public void pauseApp()
  { }

  // A required method
  public void destroyApp(boolean unconditional)
  { }

  // Check to see if our Exit command was selected
  public void commandAction(Command c, Displayable s)
  {
    if (c == cmdExit)
    {
      destroyApp(false);
      notifyDestroyed();
    }
  }
}

Here is the second MIDlet; it shows a message on the display using a List component (MIDlet2.java):

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class MIDlet2 extends MIDlet implements CommandListener
{
  private Display display;   // Reference to Display object
  private List lstMain;         // A List of items
  private Command cmdExit;      // A Command to exit the MIDlet

  // The constructor
  public MIDlet2()
  {
    display = Display.getDisplay(this);
    
    cmdExit = new Command("Exit", Command.SCREEN, 1);

    lstMain = new List("MIDlet 2", Choice.IMPLICIT);
    lstMain.append("Welcome Back", null);
    lstMain.addCommand(cmdExit);
    lstMain.setCommandListener(this);
  }

  // Called by application manager to start the MIDlet.
  public void startApp()
  {
    display.setCurrent(lstMain);
  }

  // A required method
  public void pauseApp()
  { }

  // A required method
  public void destroyApp(boolean unconditional)
  { }

  // Check to see if our Exit command was selected
  public void commandAction(Command c, Displayable s)
  {
    if (c == cmdExit)
    {
      destroyApp(false);
      notifyDestroyed();
    }
  }
}

Call the first MIDlet1.java. Call the second MIDlet2.java. In the directory where you save the files, run the following to compile and pre-verify the files.

javac -bootclasspath c:\j2me\midp-fcs\classes *.java

preverify -classpath c:\j2me\midp-fcs\classes;. -d . MIDlet1 MIDlet2

jar cvfm MIDlets.jar manifest.txt MIDlet1.class MIDlet2.class spin.png

* Note: the path c:\j2me\midp-fcs may vary depending on your installation

Create the JAR File

Create a new file called manifest.txt and then create a file with the following contents:

MIDlet-Name: MIDlet Examples
MIDlet-Version: 1.0
MIDlet-Vendor: My Corporation Inc.
MIDlet-1: MIDlet1, /spin.png, MIDlet1
MIDlet-2: MIDlet2, /spin.png, MIDlet2
MicroEdition-Profile: MIDP-1.0
MicroEdition-Configuration: CLDC-1.0

Create a JAR file by running the following command:

jar cvfm MIDlets.jar manifest.txt MIDlet1.class MIDlet2.class spin.png

This will create a JAR file called MIDlets.jar that contains the contents of the manifest.txt file and both class files.

Note: The name of the manifest file inside the JAR will be manifest.mf. The reference above to manifest.txt informs the JAR program to create manifest.mf using the contents of the file manifest.txt

Pages: 1, 2, 3, 4, 5

Next Pagearrow