Sunday, 20 July 2008

How to write Java software for your mobile phone

How to write a "Hello World" program using J2ME

So, I've just managed to get a Hello World program running on my mobile phone. Thought I would share this as a simple set of steps, since it wasn't obvious. To figure this out I referred to this guide plus the Sun Wireless Toolkit manual which you will find in {install-dir}/docs/UserGuide-html/index.html after install.

OK, without further ado, here are the steps. This is just one way of doing it!

  1. Install a current Java J2SE Development Kit (such as JDK 6 update 7 (or later) from Sun
  2. Install the Sun Java Wireless Toolkit for CLDC
  3. Run the toolkit ({install-dir}/bin/ktoolbar.exe on Windows - shortcut in Start Menu)
  4. This is the build/compile environment for J2ME apps. Note that bizarrely, it has no text editor, so you need to use your own text editor to create/edit the code, then use this tool to compile/build
  5. Click the "New Project" button
  6. Enter HelloWorld as the Project Name
  7. Enter Hello as the MIDlet Class Name (corresponds to Hello.java, your main java file)
  8. Click OK
  9. Choose your target platform (to keep things simple, use MIDP 1.0 for now)
  10. Make note of the text in the main toolkit window ,this tells you where to put your code and other files. Mine said "Place Java source files in 'C:\Documents and Settings\Administrator\j2mewtk\2.5.2\apps\HelloWorld\src'"
  11. Using your text editor of choice, open two new text files in that directory, called Hello.java and HelloCanvas.java. The code for these can be found here. Copy and paste the code into each, then save them.
  12. Go back to the tool, and press Build. This compiles your code. Assuming no errors, continue to the next step.
  13. Click Run, this will launch your program in a mobile phone emulator. It should work.
  14. Now, to transfer it to your real phone, you need to generate two files - a .jar file (Java archive) and a .jad file (Java application descriptor). Go to the Project menu, Package submenu, select "Create Package"
  15. You can now navigate to 'C:\Documents and Settings\Administrator\j2mewtk\2.5.2\apps\HelloWorld\bin\' and see your generated JAR and JAD files
  16. Now use your phone's file transfer software to connect to your phone (via Bluetooth, USB etc). I used Nokia NSeries PC Suite from www.nseries.com. Transfer the JAR file and the JAD file.
  17. If your phone does not pick up the app as installable automatically, navigate to the JAD file on your phone and execute it, this will install the app.
  18. You should now find an installed icon for your HelloWorld program in your phone menu (on N95 this is in the Applications sub menu). Click the icon, and voila! Your first mobile phone app!
So, now you can write HelloWorld, in theory you can write any mobile phone application you like - the rest is just programming!!

4 comments:

Vikas Malik said...

nice tutorial

Shawn J. Goff said...

Thanks for the guide! Exactly what I was looking for.

Unknown said...

This tutorial will help lot of people. It is exactly what many are looking for.

Thanks!

Shane said...

Thanks :)

I'm trying to write some kind of timetable-related app for my studies. Text-based is the first step and this helps a lot :)