I just spent two hours trying to build a Hello World for the Java ME (Micro Edition) platform. A fate I don’t wish you, gentle reader, to suffer, I humbly present this step-by-step guide to starting Java ME development on Ubuntu 9.10. I specifically target Ubuntu (or other Linuxes) because all the guides I could find online assumed Windows, which appears to have some different requirements (what happened to Write Once, Run Anywhere™?)
Firstly, I hate IDEs. I couldn’t get the command-line compilers (javac and the like) working — sure I could have if I spent another few hours fiddling. But since most of the advice online refers to NetBeans IDE, I used that.
OK, here we go.
Step 1: Install Debian packages (I always use Debian packages where possible).
sudo apt-get install openjdk-6-jdk netbeans
This installs the full Java 6 development kit, as well as the NetBeans IDE.
Now you will need to install the NetBeans Mobility plugin, which provides support for J2ME development, and the Sun Wireless Toolkit (WTK), which provides the actual libraries to compile against.
Step 2: Install the NetBeans Mobility plugin.
If you choose File -> New Project, you will find no way to create a J2ME project! You need to install the Mobility plugin.
Close that, and choose Tools -> Plugins. Under Available Plugins, select Mobility (Category: Java ME). Install that. Now under File -> New Project, you will find Java ME. Choose to create a Mobile Application.
Step 3: Install the Sun Wireless Toolkit.
Having chosen Mobile Application, you will be presented with a dialogue with a red error at the bottom: “No J2ME compatible platform is installed in the IDE. You need to have at least one J2ME compatible platform installed in the IDE.” So you’re still missing the actual library files required to compile J2ME apps.
So after much searching, I found what I think is the correct download (but there are lots of similar ones with different versions): Sun Java Wireless Toolkit 2.5.1 for CLDC. You should pick the Linux version, and download it (it’s called sun_java_wireless_toolkit-2_5_1-linux.bin).
Download it to your desktop, then run:
$ chmod +x sun_java_wireless_toolkit-2_5_1-linux.bin
Now this is really annoying in itself. You will be asked lots of silly questions:
- Hold down SPACE to skip the license agreement (I think this is just the GPL, so you shouldn’t even have to agree to it). Type “yes”.
- It will likely say “No suitable Java interpreter was detected”. Type “0” to “Specify a path to a Java interpreter directory.”
- If you installed openjdk-6-jdk like I said above, it should be installed in /usr/lib/jvm/java-6-openjdk/bin/.
- You might have many Java bin directories installed. To find one, type ‘which jar’. This might give you a symlink (such as /usr/bin/jar). Use ‘stat /usr/bin/jar’ to follow the symlinks until you find the real location, such as the one above.
- You are asked to enter a directory to install WTK. Just pick somewhere out of the way, probably in your home directory unless you want to share it with other users.
Step 4: Add the WTK platform to NetBeans.
Now that WTK is installed, we can fix the above issue in NetBeans.
In the New Mobile Application dialogue (heading should be Install Platform), click Install SDK/Platform/Emulator. (You can access this from the Tools -> Java Platforms menu also).
Click the Add Platform button, choose Java ME MIDP Platform Emulator, and then you will be asked to “choose a directory to search for platforms”. Choose the directory where you installed WTK. It should have a special icon. Now it should do some detection magic, and install a platform with a checked checkbox in the Add Java Platform window.
Click Next a few times, and you will see the Java Platform Manager screen with a J2ME folder, and the “Sun Java(TM) Wireless Toolkit 2.5.1 for CLDC” under it.
Now you can complete the wizard to create a mobile application with MIDP! It even has a checkbox to create a little “Hello World” app. Now to get the damn thing running …
Update: Hmm .. can’t seem to figure out how to get the emulator running, but compiling and transferring the .jar file to my phone works. (I had to change the setting from MIDP 2.1 to MIDP 2.0 to get it to work, YMMV.)