Wednesday, August 15, 2007

Now You Can Start Porting All Your Killer J2ME Apps to .Net CF

Yes, that's a joke.

After discovering the J2ME implementation shipped with Yahoo! Go's Windows Smartphone implementation, I felt compelled to take one more little step and make sure that you could actually build and deploy your own (or someone else's) MIDlets on the framework.

So I grabbed ye olde Hello World MIDlet which -- at one point anyway -- was part of a Sun tutorial on MIDP 1.0 programming, and wanted to see if I could get it to run on the javaxnet10 layer. Purely for research purposes, of course, because deploying other apps against the Y!Go runtime is likely against the EULA/license.

This exercise turned out to be pretty trivial, and it goes like this:

  1. Create a new C# Smartphone project in Visual Studio (VS). Using J# would obviate a lot of small C#/Java syntax changes, but by default J# isn't supported for "smart device" projects. In theory it should be fairly easy to flip compiler switches and get it working (it's all IL, after all), but the fastest route for me to try this out was with C#.
  2. Make a lib folder and bring in the javaxnet10.dll, platfom.dll, and interop.dll libs from a Windows Mobile distro of Y!Go. As of today, all of the available Windows Mobile devices are ARM-based (as is the MS Smartphone emulator), so you'll have an ARM interop.dll, which is fine.
  3. In VS, add references to javaxnet10.dll and platform.dll. Add interop.dll as an "existing item" and mark it to be copied through into the output directory for your app.
  4. Delete the Program class that VS generates (which has the Smartphone app entry point) and the Form1 class (MIDP handles the display; you won't be using System.Windows.Forms directly).
  5. Add in the Hello World (or other) MIDlet class, and perform C#-ification. (C# is explicit about virtual/override, string and bool vs. String and boolean, etc.)
  6. Add a .Net-suitable entry point. I used the following code, which is taken from the decompiled Main entry point in Yahoo! Go:

    private static void Main()
               MIDlet midlet = new HelloWorld();

  7. Click "debug" to do a build-deploy-run. If this is the first .Net CF app you're debugging on this emulator image, be patient -- it takes a little while for VS to deploy the compact framework to the device. My HelloWorld C# MIDlet source produces this output:


Ok... so we have the ability to import MIDlet code and run it in on the .Net compact framework runtime. Pretty cool.

If we wanted to try this with a real J2ME codebase, we'd almost certainly want to build it with the jsc, the Visual J# compiler, or else run it through a Java-C# syntax conversion tool, so as to avoid creating a entire C# variant of the code.

I'm still curious where this J2ME implementation came from (I'm about to shoot an email off to Jeremy Zawodny with Yahoo's developer group / dev relations to ask) ... If it's not an in-house creation, my best guess would be the mobile game industry, since that's the one area where MIDP apps have been successful enough that it would be worth the investment to be able to sell them on Windows phones.

And I still plan to comment on what this all means in terms of an architecture choice for Yahoo! Go ... (and no, the last post about NIH and crackpots was not the promised commentary about it).

1 comment:

Wright Petter said...

Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Java developer learn from Java Training in Chennai. or learn thru Java Online Training from India . Nowadays Java has tons of job opportunities on various vertical industry.