Run mobile Midlets on your PC
Thursday, February 14th, 2008
In anticipation of my new phone (a Sony Ericsson W910i), I decided to spend some time finding various Java Midlets which I need for the phone. I was looking for a decent calculator, translator, dictionary and a E-book reader. I wanted to try the applications, but since I don’t have the new phone yet, I thought “Why not find an emulator on which to run them?”. In this post I’ll detail my journey into the crazy world of J2EE, Midlets, CLDC, MIDP, J2RE and various other weird mobile Java stuff, and show you how I got the emulator and Midlets to run on my Unix (Ubuntu) machine.
Prerequisites
To be able to emulate anything at all, you’ll need to have the J2RE installed. That’s the Java 2 Runtime Environment; the basic stuff you need to run Java programs. Under Ubuntu, it’s as easy as:
sudo aptitude install sun-java6-jre
Get Sun Java Wireless Toolkit
Next you’ll need to fetch and install the Sun Java Wireless Toolkit (S2WTK2, previously known as J2ME: Java Micro Edition). You can download it from:
http://java.sun.com/products/sjwtoolkit/download.html.
(Registration not required, amazingly enough). Scroll down on the page until you see the ‘Download’ button. That’ll take you to the download page where you must accept the license and download the toolkit for your platform.
Automatic installation
To install it, you can try the installer:
sh ./sun_java_wireless_toolkit-2_5_2-linux.bin -- LICENSE -- Do you agree to the above license terms? [yes or no] yes Testing /usr/bin/X11/java... A suitable Java interpreter was detected 0) Use /usr/bin/X11/ 1) Specify a path to a Java interpreter directory. 2) Cancel this installation. Select a choice [0-2]:
The installer detected the wrong path, so you’ll have to find out where your actual JRE resides on your disk. Under Ubuntu, this is: /usr/lib/jvm/java-6-sun/jre/bin, so we enter that instead of the detected path:
0) Use /usr/bin/X11/ 1) Specify a path to a Java interpreter directory. 2) Cancel this installation. Select a choice [0-2]: 1 Enter a path to the Java 2 SDK: /usr/lib/jvm/java-6-sun/jre/bin/ /usr/lib/jvm/java-6-sun/jre/bin//java Testing /usr/lib/jvm/java-6-sun/jre/bin//java... Please enter a directory into which you would like to install the Sun Java(TM) Wireless Toolkit, 2.5.2 for CLDC. -- Blah blah blah -- follow the instructions -- Please choose one of the following options: 0) Begin copying files if you are satisfied with the settings . 1) Cancel the installation. Select a choice [0-1]: 0 Checksumming... Extracting the installation files... ./sun_java_wireless_toolkit-2_5_2-linux.bin: 466: /usr/lib/jvm/java-6-sun/jre/bin/jar: not found Failed to extract files. Installation will stop now. Please try to install Sun Java(TM) Wireless Toolkit again, or contact wtk-comments@sun.com for assistance.
Manual installation
Chances are, however, that it won’t work because it can’t find the jar binary:
./sun_java_wireless_toolkit-2_5_2-linux.bin: 466: /usr/lib/jvm/java-6-sun/jre/bin/jar: not found Failed to extract files. Installation will stop now.
If this happens with you, there’s another way to install it:
- The .bin file is nothing more than a shell script (the automatic installer) with an .zip file attached to it. We’ll have to remove the shell script header so we can unzip the file.
- Open the sun_java_wireless_toolkit-2_5_2-linux.bin in a text-editor which can edit binary files without screwing things up. Vim works nicely.
- Remove the shellscript in the beginning of the file. Keep all the binary stuff
- Save the file
- Create a new directory and cd into it: mkdir jwt; cd jwt;
- Unpack the .bin file: unzip ../sun_java_wireless_toolkit-2_5_2-linux.bin
- You can now run stuff from the Wiress Toolkit in the current dir or move it somewhere on your hard disk (i.e. /opt/j2me )
Now try the Wireless toolkit out to see if it works:
~$ /opt/j2me/bin/ktoolbar
You can try out an example Midlet game by running:
~$ /opt/j2me/bin/runmidlet /opt/j2me/apps/Games/bin/Games.jad
The Emulated Phone
The Phone emulator should pop up with some options for games which can be run. You can control the emulator with the mouse, or with the keyboard: F1, F2 are the Left and Right soft keys. Normal arrow keys on your keyboard correspond to the arrows of the phone. Enter emulates the middle button.
You can configure the emulated phone by with the /opt/j2me/bin/prefs program. Here you can change things such as the type of phone emulated, etc.
Put files on the phone
Sometimes you’ll want to put files on the phone. For instance, an mp3 or whatever. You can do this by copying files to the j2mewtk/2.5.2/appdb/DefaultColorPhone/filesystem/root1/ directory in your home directory. This directory is automatically created when you run the Phone emulator.
Running midlets
Running Midlets on the phone is harder then it should be. In order for the emulator to be able to run Midlets, it needs both a .jar and a .jad file. The Jar file contains the actual application and the Jad file contains a definition file so that the emulator knows how to run the Midlet. Most Midlets only come with a Jar file and not a Jad file however. Fortunately it’s quite easy to create one yourself. Here’s a script which you can use to automatically create a .jad file from a .jar file:
#!/bin/sh while [ -n "$1" ]; do JAR=$1 JAD="`basename \"$JAR\" .jar`.jad" JAR_SIZE=`du -b "$1" | cut -f1` echo -n "Generating \"$JAD\" from \"$JAR\".. " unzip -p -x "$1" META-INF/MANIFEST.MF > "$JAD" echo "" >> "$JAD" echo "MIDlet-Jar-URL: $1" >> "$JAD" echo "MIDlet-Jar-Size: $JAR_SIZE" >> "$JAD" echo "Done." shift done
Copy-paste the file above into a file named jar2jad.sh and make it executable: chmod 755 ./jar2jad.sh. You can now run:
~$ ./jar2jad.sh foo.jar
In order to create a corresponding .jad file. Now you can run the Midlet by executing:
~$ /opt/j2me/bin/runmidlet ./foo.jad
You can now download .jar midlets and test them out on your PC before actually running them on your phone. Not all midlets will work on the emulator though, since some of them depend on phone specific Java libraries. Some phone manufacturers give out SDK’s (System Development Kits) for their phones which you can download and install so you can test Midlets which are specific for certain phones. This will be left as an exercise for the reader though.