and deliver a small J2ME program to a mobile device. I will assume that you have
... 1.0 and MIDP 1.0. You will also be requested to place Java source files in.
J2ME development Johan Montelius
Introduction In this laboration we are going to go through the steps of compiling, package and deliver a small J2ME program to a mobile device. I will assume that you have programming experience but not that you have been working with Java before.
Getting started I’ll use very basic tools in this tutorial; there are fare better development platforms than the ones we use here but instead of selecting one for you I’ll make that up to you. You could take a look at the Eclipse development environment www.wclipse.org, however, if you don’t want to build larger programs you will be fine with the tools that we use in this tutorial. We need three things to get starting: • the Java 2 SDK SE 1.4 or higher • Sun Java Wireless Toolkit for CLDC, • a good editor. To build J2ME applications you need the a Java compiler and a preverifyer. The Java compiler comes with the Sun JDK and the preverifier comes with the Wireless Toolkit. The Wireless Toolkit also includes a building tool and emulators for different terminals. The Wireless Toolkit is not a integrated IDE with source code editor or debugger. To do basic development, enough for this course, you only need an text editor. If you want to do more advanced applications you might want to use an IDE such as NetBeans IDE or Eclipse. If you want to use the Java compiler from JDK 5 or 6 from the command line or from a script, you need to compile with -target 1.4 since the class file format changed. The Java 2 SDK and the Wireless Toolkit are both downloaded from java.sun.com. The tools are limited in that they do not provide a full integrated development environment with editor and debugger. The Wireless Toolkit and its emulators does of course not provide an exact replication of any real phone on the market so if you want to test your application on a particular device you would need an emulator from that phone manufacturer. 1
You will notice that the SDKs offered by the phone manufacturers in some cases just are versions of the Sun Java Wireless Toolkit. Also notice that in the following examples I’m using the Wireless Toolkit 1.0 and not the later versions. This will limit us in that we will only be able to develop MIDP 1.0 applications but that will be fine for now. Install the tools, start with the J2SE SDK, and start the KToolbar found in the Wireless Toolkit. Make sure that it works by opening an existing project, the demo project, then try to build it and run it. If everything works a emulator will start with a set of demo applications to choose from. If you have problems make sure that you have the right versions of the tools installed. If you have installed it on a Linux machine make sure that you have write access rights to the project directories.
Your first J2ME program Now let’s create a project of our own. Select ”New Project” and enter a project name such as ”HelloWorld” and a class name ”HelloWord”. You will then be prompted to fill in any specific details about the project but the default settings are ok for now. Notice that the default is an application targeting CLDC 1.0 and MIDP 1.0. You will also be requested to place Java source files in c:\WTK104\apps\HelloWorld\src or similar depending on where you installed the toolkit. Shared resources should go into \res and any libraries (pre-compiled code) into \lib. Now start your editor an create the file HelloWorld.java in the \src directory. import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class HelloWorld extends MIDlet implements CommandListener { private Command exit; private TextBox tbox; public HelloWorld() { exit = new Command("Exit", Command.EXIT, 1); tbox = new TextBox("Message", "Hello World!", 25, 0); tbox.addCommand(exit); tbox.setCommandListener(this); } protected void startApp() { Display.getDisplay(this).setCurrent(tbox); }
2
protected void pauseApp() {} protected void destroyApp(boolean bool) {} public void commandAction(Command cmd, Displayable disp) { if (cmd == exit) { destroyApp(false); notifyDestroyed(); } } } If everything works you should then be able to build (compile, verify etc) the application and run it. The problem now is to get it running on a regular phone.
On the phone There are basically two ways of shipping it to the phone. Either we use a local connection or we do it over the mobile network. The local connection is simpler and a lot faster when we want to experiment with new applications. Before we send anything over we need to build a package, that is a .jar file that includes everything. Using the Wireless Toolkit you can select ”Project/Package/Create Package” from the menus. This will create HelloWorld.jar in the \bin directory. Notice that the \bin directory contains a MANIFEST.MF file that holds the description of the midlet. The manifest file will be included in the .jar file. You must now connect your phone to your laptop and the easiest way to do this is over IrDA or Bluetooth. You might need specific drivers for your phone if you want to do more advances things but transferring a file should be possible right away. You will be prompted of you want to accept the file, where to store it and possibly to run the program. When you run the program we will hopefully see the magical “Hello World!” across the screen.
A second MIDLet To experiment with two midlets in the same package we can create a second Hello World example but this time using a Canvas to draw the text. The canvas is a lower level graphical object that requires some more work to get it right. We need to implement our own paint method that will be called by the midlet environment. We create our own canvas that prints a message nicely on the center of the screen. 3
import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class HelloCanvas extends MIDlet implements CommandListener { private Display display; private Command exit; private Canvas canvas; private class MyCanvas extends Canvas { private String message; public MyCanvas(String msg) { message = msg; } public void paint(Graphics g) { int h = getHeight(); int w = getWidth(); g.setColor(200, 100, 20); g.fillRect(0, 0, w, h); g.setColor(255, 255, 255); g.drawString(message, h/2, w/2, g.TOP|g.HCENTER); } } public HelloCanvas() { display = Display.getDisplay(this); exit = new Command("Exit", Command.EXIT, 1); canvas = new MyCanvas("Hello Canvas!!!"); canvas.addCommand(exit); canvas.setCommandListener(this); } public void startApp() { display.setCurrent(canvas); } public void pauseApp() { } public void destroyApp(boolean uncond) { }
4
public void commandAction(Command cmd, Displayable disp) { if (cmd == exit) { destroyApp(false); notifyDestroyed(); } } } Place the source code in a file, HelloCanvas, in the src directory. The file will now be compiled when we build the project but in order for the midlet to show up as one alternative when we start the application we need to edit the manifest file. This is best done by changing the settings in “Project/Settings.../MIDLets”. Add a new entry for the HelloCanvas midlet. If we now build, package and install the application on a phone you should see that we have to midlets to choose from. The .jar file can also contain class files that both midlets can share or other resources such as images and sound files.
Provisioning It’s now high time to put the application on the web server. We could then link to the .jar file but instead we will use the .jad file that describes the application and let this link to the .jar file. You will find the .jad file in the \bin directory but when you put this on the web server you will have to patch the URL or you have to change the URL in the settings and package the application again. Once you have both the .jad file and .jar file on the server you can create a small WML file that links to the .jad file.
Download Hello World.
Try it and notice how you’re prompted before the application is downloaded. Choose “more information” or something similar before you download it and you will see the information that was presented in the .jad file.
5
Notifications One additional feature that we will explore is to include a notification URL in the .jad file. The file would then look as follows:
MIDlet-1: HelloWorld, , HelloWorld MIDlet-2: HelloCanvas, , HelloCanvas MIDlet-Jar-Size: 2542 MIDlet-Jar-URL: http://web.it.kth.se/~jm/wap/HelloWorld.jar MIDlet-Name: HelloWorld MIDlet-Vendor: Johan MIDlet-Version: 1.0 MIDlet-Intall-Notify: http://web.it.kth.se/~jm/wap/notify.php
The notify.php file is then a small PHP script that receives a POST with the content “900 Success”. Write a PHP script that reads a post message and stores the content on a file (or database). You can of course use more elaborative notify URL’s that include a query string with information about which application that was downloaded.
Exploring J2ME Once you have the development-deployment cycle up and running I want you to explore some of the specific features of J2ME such as networks connections, RMI, having information in the .jad file or messaging. You do not have to do very advanced programs but I want you to explore something and have a link to it on your index.wml file.
6