UPDATE: This post has been superseded by my more recent cross-platform guide to setting up J2ME.
Unfortunately Apple OS X was overlooked by the J2ME development groups at SUN and the handset manufacturers. The result is that the Wireless Tool Kit (WTK), the open source version PhoneME and the various manufacturer-specific emulators do not run on a mac.
However, it is still possible to develop for J2ME devices on Apple OS X without having to resort to running Windows in a virtual machine… right from Eclipse development through to deployment to the device. I’ll explain how in this article…
This guide is equally relevant for less popular setups such as sparc Solaris/Linux or ARM Linux.
UPDATE: This post has been superseded by my more recent cross-platform guide to setting up J2ME.
Development MIDP, CLDC and JSRs
J2ME does not use the Java SE class library. It has a very limited subset of Java 1.3 (unfortunately, pre-Collections and pre-generics) which comes as a 2 jar download, the MIDP and CLDC. Optional runtime libraries come as JSR libraries. An incomplete list is available on the SUN J2ME APIs webpage.
UPDATE: This post has been superseded by my more recent cross-platform guide to setting up J2ME.
The best way to obtain the development jars is to download the Linux version of the WTK, run the installer script (sh sun_java_wireless_toolkit-*-linux.bin, using /usr/bin as the location of your Java 2 SDK) and grab the jar files that come with it (all the jar files in the lib folder).
UPDATE: This post has been superseded by my more recent cross-platform guide to setting up J2ME.
You may also wish to obtain the official Javadocs for these jar files… you can get some of them in the docs/api folder. Others you can get from the links on the Java Community Process.
UPDATE: This post has been superseded by my more recent cross-platform guide to setting up J2ME.
If your main target device is a modern phone, then CLDC1.1 and MIDP2.0 will be the libraries you should be using.
Emulation
There are some pure-Java emulators which are great for doing the initial development. Unfortunately they sometimes introduce their own quirks that you wouldn’t experience on a real device, there is no way to emulate an actual device and there is no profiling support (apart from J2SE profiling), but in the most part you will be able to live with it. The two most popular emulators are MPowerPlayer and MicroEmulator. MPowerPlayer does not have an active community… I strongly suggest using MicroEmulator (the primary author, Bartek Teodorczyk may be a guest author here at some point so let’s keep him sweet
).
UPDATE: This post has been superseded by my more recent cross-platform guide to setting up J2ME.
Preverification
J2ME introduced an additional stage to the build chain… called preverification. The idea is that a lot of class file verification can be done on the developer side, in order to reduce the computational overhead on the device. Fortunately, the source bundle of the “feature” release of PhoneME comes with PPC preverifier binaries. These work fine under an Intel mac (thank you Rosetta!). The relevant binary is hidden away in the cldc/build/share/bin/darwin_powerpc folder.
Eclipse Setup
I’m assuming you are already familiar with Eclipse. The only plugin that seems to exist is EclipseME. It automates package building and emulator setup. However, there are a lot of bugs that affect mac users and the author does not seem to have a lot of time to maintain the package… which is a shame, because it’s a really great plugin. I’ll describe and offer workarounds for all the bugs.
UPDATE: This post has been superseded by my more recent cross-platform guide to setting up J2ME.
EclipseME can automate MIDlet signing and obfuscating if you are interested. You will probably want to download ProGuard to enable obfuscation (makes your jars much smaller) and create a test signing key with the keytool command line tool.
You will want to add your emulators to EclipseME. Go to Preferences -> JavaME -> Device Management. By default, this will be empty. Click “Import…” and then point to the base directories of MicroEmu and MPowerPlayer. You may need to hit refresh, but you’ll eventually be able to import these devices. In the next few sections I’ll describe some workarounds to bugs in EclipseME, and request that you vote on the bugs in their bug tracker.
MPowerPlayer
MPowerPlayer comes with a preverify binary, but it’s a little out of date. Feel free to replace the binary in the osx/preverify directory with the one you got from PhoneME. You need to actually replace the bundled preverify binary or EclipseME will not see it, if you think this is inconvenient, please comment on feature request #1767931. Actually, the story is even worse because it appears EclipseME will ignore the preverifier altogether and attempt to use its own broken preverify tool, see bug #1783929.
UPDATE: This post has been superseded by my more recent cross-platform guide to setting up J2ME.
You should really be using the official CLDC/MIDP jar files instead of the (broken) ones that come packaged. But due to bug #1783929 this is not currently possible. I’ll warn you now that the MPP implementations of CLDC/MIDP are missing some methods, such as Random.nextInt(int).
If you want to add additional JSRs to your device, then you’re out of luck… you can add additional jars in the Edit -> Libraries pane, but don’t expect these settings to filter down to the runtime environment, please comment on feature request #1767962 to get that fixed. You will have to setup a “Run as…” launch configuration if you want to add additional libraries. You’ll have to read the MPP documentation to find out how to launch it this way… it’s really messy.
Again, unfortunately the preverification step is b0rk3n when using the “build package” command, see bug #1783935. The fix is to use the command line preverify tool that you downloaded in PhoneME and the jars that you downloaded as part of the SUN WTK. If you have an unverified package (such as the one EclipseME will spit out to the ‘deployed’ directory under this setup), then the following should perform the preverification
preverify -classpath /path/to/cldcapi11.jar:/path/to/midpapi20.jar package.jar
it’ll then spit out a preverified package.jar in a newly created output folder. This jar can be deployed to a J2ME device.
MicroEmulator
Unfortunately, EclipseME does not currently support MicroEmu properly. It can be added as a device, but unfortunately Eclipse will not hide the J2SE library from you and you will be tricked. Please comment on bug #1754829 to get this fixed.
UPDATE: This post has been superseded by my more recent cross-platform guide to setting up J2ME.
The workaround is to use MPowerPlayer as your EclipseME device and then set MicroEmu as your launch device. I really hope this will be fixed in the future so that I can remove any dependence I have on the un-maintained MPowerPlayer.
Deployment
You have two options for deployment on the mac… over the web, or over bluetooth. Not all phones support bluetooth but if they do then open up the standard “Bluetooth File Exchange” application and go to “File -> Send File”. You can then select your preverifed jar (and jad) and then the device you want to send it to. If it works then a successful send will be followed by your phone’s standard installation procedure.
The alternative is to put the jar/jad on a URL somewhere on the interwebz and point your phone at it. This will cost you because service providers are still charging for data transfers (and therefore assisting in the stunting of the mobile web).
Bluetooth development
Unfortunately this one will cost you $$. The official Java library for bluetooth development is JSR-82 and Apple do not ship with an implementation. There is a third-party commercial library available from Avetana, which is great… I have a license. If you want to help create an open source implementation… join in the fun at BlueCove. Read the MPowerPlayer docs on getting this setup (from memory it involves putting a copy of the Avetana jar in the root MPP directory), or simply add to the classpath for MicroEmu.
UPDATE: This post has been superseded by my more recent cross-platform guide to setting up J2ME.
Jon wrote:
November 7th, 2007 at 12:24 am