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

Create the JAD File

Create a new file called MIDlets.jad and create a file with the following contents:

MIDlet-Name: MIDlet Examples
MIDlet-Version: 1.0
MIDlet-Vendor: My Corporation Inc.
MIDlet-Description: Two simple examples to show how to compile and run a MIDlet
MIDlet-Jar-URL: http://localhost/MIDlets.jar
MIDlet-Jar-Size: 2604
MIDlet-1: MIDlet1, /spin.png, MIDlet1
MIDlet-2: MIDlet2, /spin.png, MIDlet2

Notice the reference to both the MIDlets:

MIDlet-1: MIDlet1, /spin.png, MIDlet1
MIDlet-2: MIDlet2, /spin.png, MIDlet2

Each line above contains the name of the MIDlet to display on the device (MIDlet1 and MIDlet2); an (optional) image file that the application manager will show on the display next to the MIDlet name; and the class file to load to start the MIDlet (MIDlet1 and MIDlet2).

Running the MIDlets

You can run a MIDlet from either a local file system or a web server.

From the File System

Type the following at the command prompt to preview your MIDlet:

midp -transient file://MIDlets.jad

The -transient option notifies the emulator you want to run a descriptor file found at a specified url. In this case: file://MIDlets.jad, which looks for MIDlets.jad in the current directory. You could also give a full path to the JAD file if necessary. For example, the full path to my JAD file is

file://OnJava/MIDlets/welcome.jad

You should see output similar to figures below, showing the main screen of the application manager and the output of MIDlet1 and MIDlet2.

Main screen and output.
Figure 2

From a Web server

You can also run MIDlets from a web server. After uploading the JAR and JAD file, access the JAD file by changing the url:

midp -transient http://localhost/MIDlets.jad

This accesses MIDlets.jad file on the web server running on my computer. If you upload your files to a web server on the Internet, simply change the reference to the appropriate domain name:

midp -transient http://www.yourwebserver.com/path/MIDlets.jad

Project Management using Java Packages

Follow along with the steps below to create one last MIDlet.

  1. Create a directory for the new project. I used the name MIDletPackage, which I created as a subdirectory of f:\onjava.
  2. Within the new directory, create three new sub-directories:
        jclasses
        pclasses
        resources
    The additional directories make it clear where to find specific files. For example, all images and other resources will be in /resources. All class files created by the Java compiler will be in /jclasses. The pre-verified classes will be written in /pclasses. Here's the directory structure on my system:
    
    f:\onjava
      |
      MIDletPackage => Java source code and manifest.txt file
          |
          jclasses  => Output from the Java compiler
          pclasses  => Output from pre-verifier
          resources => Resource files (images, etc)
  3. Update the files: copy the Java source files (MIDLet1.java and MIDlet2.java) into the MIDletPackage directory, and add the package statement shown below to the top of each file. Also, if you have an image file you've been using, copy that file into the resources directory.

    MIDlet1.java:
    package simpleMIDlets;

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

    public class MIDlet1 extends MIDlet implements CommandListener
    {
    ...
    }

    MIDlet2.java:
    package simpleMIDlets;

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

    public class MIDlet2 extends MIDlet implements CommandListener
    {
    ...
    }
  4. Update manifest.txt and the JAD file to reference the new location of the images and the class files.

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


    MIDlets.jad:
    MIDlet-Name: MIDlet Examples
    MIDlet-Version: 1.0
    MIDlet-Vendor: My Corporation Inc.
    MIDlet-Description: Packaging multiple MIDlets
    MIDlet-Jar-URL: http://localhost/MIDlets.jar
    MIDlet-Jar-Size: 2884
    MIDlet-1: MIDlet1, /resources/spin.png, simpleMIDlets.MIDlet1
    MIDlet-2: MIDlet2, /resources/spin.png, simpleMIDlets.MIDlet2


    Note the changes to MIDlet-1 and MIDlet-2. The class files are now referenced using the package name simpleMIDlets.MIDlet1 and simpleMIDlets.MIDlet2.
  5. Compile: Compile the source:
    javac -bootclasspath c:\j2me\midp-fcs\classes -d jclasses *.java
    The -d option tells the compiler to write the classes files into the directory jclasses
    * Note: the path c:\j2me\midp-fcs may vary depending on your installation"-d" option tells the compiler to write the classes files into the directory jclasses
  6. Pre-verify:
    preverify -classpath c:\j2me\midp-fcs\classes; -d pclasses jclasses
    The -d option tells the pre-verifier to store the pre-verified classes in the directory pclasses. The last entry -- jclasses -- tells the preverify where to look for classes to pre-verify.
    * Note: the path c:\j2me\midp-fcs may vary depending on your installation
  7. Create the JAR: Run the jar program:
    jar cvfm MIDlets.jar manifest.txt -C pclasses . resources

    Creates a JAR file with the name MIDlets.jar, using the file manifest.txt as the contents of the manifest file (a file called manifest.mf will be stored in the JAR).

    -C pclasses . tells the jar program to change to the pclasses directory and archive all (".") the files
    resources informs the jar program to add all the files located in the directory named resources
    At this point, you have two files:
    MIDlets.jad -- Application descriptor file
    MIDlets.jar -- JAR file containing the MIDlets, a manifest and the image files
    Launch the MIDlets: To view your new files, once again, you have two options:
    From the File System:
    midp -transient file://MIDlets.jad
    From a web server: Upload the JAR and JAD files to a web server. Enter the following to view the MIDlets:
    midp -transient http://localhost/MIDlets.jad
    Change the path as in the previous example to reference the MIDlets on a different web server.

With our new hierarchy, we can now quickly determine the contents of each directory; that is, the location of the source code, Java classes files, pre-verified class file, and resources.

Screen shot.

F:\onjava
  |
  MIDletPackage => Java source code and manifest.txt file
    |
    jclasses => Output from the Java compiler
    pclasses => Output from pre-verifier
    resources => Images (and other resource files)

The directory simpleMIDlets (below jclasses and pclasses) was created by the Java compiler and the pre-verifier, respectively. Because we added the statement package simpleMIDlets to our source files, each program created the directory matching the package name and placed its output in that directory. That is, the Java compiler class files are written to

F:\onjava\MIDletPackage\jclasses\simpleMIDlets

The pre-verified classes file are written to:

F:\onjava\MIDletPackage\pclasses\simpleMIDlets

Summary

In the previous and current articles we've covered all the basics of MIDlet development. You should have all the necessary software installed and configured, and you should be comfortable with the complete development cycle.

We've also shown how you can use a JAR and JAD file to package one or more MIDlets. Finally, we've shown that running a MIDlet on the cellular phone emulator varies little, regardless of where the MIDlet reside, on a local file system or a web server.

You can download the source code for this article from the Core J2ME site.

John W. Muchow is an expert Java Developer and Trainer specializing in J2ME.


Return to ONJava.com.