Old School Windows Mobile :: Getting Started with Java on PDAs

Below is an article I wrote back in 2002 that described how to build Personal Java apps on the iPAQ during the age of Pocket PC 2000, Embedded VB and C++:  

PDAs are becoming a permanent fixture in the everyday lives of consumers and business people. There's no question that we have Palm to thank for bringing us a small, pen-based, easy-to-use organizer to help keep our busy lives on track.

At the same time, Microsoft has been trying to hit one out of the park for years with its Windows CE operating system running on a variety of handheld devices. As with everything else at Microsoft, it usually takes them three times to get something right, and the Pocket PC is no exception.

Based on Windows CE 3.0, the Pocket PC looks and works much like its Palm rival. However, the Pocket PC is different due to its use of more powerful hardware, more memory, and a full-blown, multithreaded operating system - it's more like a small computer than an organizer.

The success of the Pocket PC in the marketplace is largely due to the unique styling and powerful processor found in the Compaq iPAQ. With all the other Pocket PC devices shaped like unimaginative rectangular boxes, the iPAQ has had an easy time gobbling up market share. That said, the Palm is still the market-share king and isn't sitting around on its laurels waiting to become another "Netscape-style" victim of Microsoft.

As evidenced by the dominance of Windows, having the most developers writing applications for a given platform does more for its success than superior technology. The existence of a variety of programming languages for a platform plays a large role when it comes to building the critical mass of applications needed to put that platform over the top. Development for the Palm is currently being done with languages such as C/C++, CASL, NS Basic, J2ME, and AppForge (Visual Basic). Until recently, the Pocket PC development languages included eMbedded Visual Basic, NS Basic, and eMbedded Visual C++.

The Palm has clearly had the upper hand in the language department, even though its operating system lags behind the Pocket PC in features. This is quite a paradox for the Pocket PC camp, which seems to have superior technology on its side. Even the two BASIC tools for the Pocket PC are actually based on VBScript, with its lack of true data types and horrible error handling. So, the only way to take advantage of everything Windows CE 3.0 has to offer is to use an eMbedded Visual C++ tool, which most developers find intimidating at best.

Last summer, the Pocket PC gained a powerful and easy-touse language that enabled developers take full advantage of the Windows CE operating system. Sure to make Microsoft executives cringe, Java has finally arrived for the iPAQ! Insignia Solutions (https://www.insignia.com/) released its Jeode Embedded Virtual Machine for the Intel StrongARM processor.

The Jeode EVM is a Sun Authorized Virtual Machine that is certified and fully compliant with Sun's PersonalJava 1.2 and Embedded Java 1.0.3 specifications. It can run Java applets and applications, use a dynamic compiler to run Java apps up to six times faster than a normal JVM interpreter, and provide a preemptible, concurrent garbage collector for superior memory management. Most developers don't know much about Sun's PersonalJava and usually think about MIDP when talk of Java development for a handheld device comes up.

PersonalJava, as opposed to MIDP, wouldn't fit very well on a Palm (for example), nor could all of its classes be utilized, since the Palm OS doesn't provide all the underlying services required.

Since the iPAQ doesn't skimp on memory - 32MB is now the minimum - you don't have to compromise on your Java. PersonalJava is roughly equivalent to JDK 1.1.8 with a dash of JDK 1.2 classes and APIs thrown in for good measure. The Jeode EVM requires only 3-4.5MBs of space, depending on your need for the internationalization classes. This means that the Java Native Interface plus all of the following classes are available to you:

java.io: System input and output through data streams, serialization, and the file system

java.util: Collection classes, event model, date and time facilities, internationalization, and miscellaneous utility classes

java.util.jar: Classes for reading and writing the JAR (Java ARchive) file format

java.util.zip: Classes for reading and writing the standard Zip and GZip file formats

java.lang: Core Java API classes

Core Java API Classes

java.lang.reflect: Classes and interfaces for obtaining reflective information about classes and objects

java.net: Classes for implementing networking applications

java.math: Classes for performing arbitrary-precision integer arithmetic (BigInteger) and arbitrary-precision decimal arithmetic (BigDecimal)

java.text: Classes and interfaces for handling text, dates, numbers, and messages

java.rmi: Classes that allow Java to make remote procedure calls

java.rmi.dgc: Classes and interface for RMI distributed garbage collection

java.rmi.registry: A class and two interfaces for the RMI registry

java.rmi.server: Classes and interfaces for supporting the server side of RMI

java.security: Classes and interfaces for the security framework

java.security.acl: Interface to access control list data structures

java.security.cert: Classes and interfaces for parsing and managing certificates

java.security.interfaces: Interfaces for generating RSA and DSA keys

java.security.spec: Classes and interfaces for key specifications and algorithm parameter specifications

java.sql: An API for accessing and processing data found in databases

java.beans: Classes that support the creation and usage of embeddable, reusable software components

java.applet: Classes necessary to create an applet and the classes an applet uses to communicate with its applet context

java.awt: Classes for creating user interfaces and for painting graphics and images

java.awt.datatransfer: Interfaces and classes for transferring data between and within applications

java.awt.event: Interfaces and classes for dealing with different types of events fired by AWT components

java.awt.image: Classes for creating and modifying images

As you can see by all the packages, classes, and APIs supported in PersonalJava, you have a handheld programming language that’s more feature-rich than eMbedded Visual Basic, AppForge, NS Basic, or MIDP. Only eMbedded Visual C++ rivals PersonalJava in power, but it can’t compare when it comes to developer productivity. In addition, the Jeode EVM supports the creation of console applications, and a Timer class is provided for use in animations, scheduling jobs, or any situation in which you need to execute code at set intervals.

Let’s cover what PersonalJava can’t do. Generally, anything that’s dependent on Java 2 is off-limits, with the exception of the handful of JDK 1.2 classes found in PersonalJava. Alas, Swing is officially unsupported in this environment.

There is a workaround to make JFC 1.1.1 work with PersonalJava that requires you to change a line of code in one of the classes that make up swingall.jar. I’ve made this change myself and it’s a good news, bad news situation. On the one hand, you get to use great-looking, Swing user interface elements on your iPAQ. The downside is the deployment of a 2MB swingall.jar file, slow program loading times, and extremely poor performance.

In my opinion, you should stick with AWT and other GUI widgets that are based on JDK 1.1. You may have to do some digging to find things like AWT-based grids and tab controls, but they’re out there. My advice is to look in your old copies of “pre-Swing” JBuilder and VisualCafé to find lots of GUI JavaBeans based on AWT.

Building Applications
So what kind of cool applications can you build on your iPAQ with this technology?

 

A single-tier application that uses JDBC to store and manipulate data on a local, pure-Java database such as Cloudscape or PointBase

A wireless two-tier client that uses JDBC to communicate directly with enterprise databases such as Oracle and SQL Server

A wireless n-tier client that uses RMI to make remote procedure calls to Java objects on a middle-tier server

A mobile, wireless RMI server that allows other Java clients to invoke its objects

A chat/instant messenger client or server using Socket and URL classes

A wireless telemetry client that remotely monitors vital corporate assets from anywhere

A remote data-entry application to be used by personnel who work in the field.

Miniature, mobile versions of just about any desktop or server Java application you’ve ever built.

I think you get the gist of where I’m going here. The best recommendation I can make is to get your hands on a wireless Ethernet (802.11b) or a CDPD (mobile Internet) card so you can start building the kind of distributed wireless applications that will take you to the next level in your career as a developer (you probably never thought that going backwards to Java 1.1 would help advance your career).

For complete information and the APIs related to PersonalJava, go to the PersonalJava application environment Web site at https://java.sun.com/products/personaljava.

Getting Jeode
Now that you know what PersonalJava and the Jeode EVM are capable of, it's time to go get it. The Jeode EVM is available for purchase on the Handango Web site at https://www.handango.com/PlatformProductDetail.jsp?siteId=1&platformId=2&productType=2&catalog=0&sectionId=0&productId=17215for $19.99. The software is downloadable from the Web site after you go through the shopping cart and checkout routine.

Once the software is downloaded, load it onto your Pocket PC. To begin the installation, be sure the iPAQ is in its cradle and connected to your PC via ActiveSync. Double-click on the "JeodeForArmPocketPC" executable to start the InstallShield Wizard. When asked to install Jeode in the default folder, click Yes. The installation shouldn't take more than a minute.

To verify the installation and to be sure the EVM is working properly, complete the following steps: from the Start menu on your iPAQ, navigate to Programs|Jeode|EVM to bring up the EVMConsole (see Figure 1).

Figure 1:

The EVMConsole is equivalent to the DOS/Command Prompt you have in Windows. From here you can launch both console and AWT Java applications. There are a number of command line options that you can pass to the EVM when using the EVMConsole:

 

-?, -h, or -help: Displays Jeode EVM help.

-cp <pathnames> or - classpath <pathnames>: Here you specify the path(s) used for loading classes. Semicolons separate multiple pathnames that point to JAR files. If you need to launch a Java application that resides in a JAR file called "ipaq" that sits in the "windows" directory with a main class called "frame1", you would type in "-cp \windows\ipaq.jar frame1". To take this a step further, if your application needs classes found in a third-party JAR file, you might type in "-cp \windows\lib\jcbwt363.jar;\windows\ipaq.jar frame1". If you put your application in a package, you would type in "-cp \windows\ipaq.jar mypackage.frame1". That said, application classes that don't reside in a JAR file can be executed directly without the use of "-cp".

-D <propertyName> = <value>: This supplies the value for either a Jeode EVM or Java system property. To keep the console open after an application has been executed, use the command "-Djeode.evm.console.local.keep=TRUE". If you have a console application that sends more than one screen of data to the console, use the command "-Djeode.evm.console.local.paging=TRUE".

-v or -verbose: This causes messages to be displayed by the EVMConsole when a class file or dynamic library is successfully loaded or when a garbage-collection cycle is performed.

-version: Displays the EVM and class library versions.

-Xnowinceconsole: This option disables the EVMConsole if you don't want it to remain visible while running graphical applications.

You can further test your installation by trying out all the test applications found in the Examples and AWT folders.

I think I've done enough talking about what can be done with Java on the iPAQ, so let's put theory into practice and build some simple applications. We'll be building both a console and a GUI app that uses AWT. I'll be building Java applications that are Java 1.1-compliant, so if you're using a current IDE based on Java 2, you'll need to utilize its JDK switching feature to compile against JDK 1.1. In some cases I'll package the Java apps in JAR files - in this case, either use your IDE's JAR wizard or JAR your classes together at the command line.

Console App
The console application we develop will demonstrate multithreading and reside in a package called "basicconsole" and consist of two classes. The code for the class called "counter" is shown in Listing 1.

This class is the worker thread that simply counts upward from one number to another based on the integers that are passed into it. It sends those numbers to the console. The main class is called "app" (see Listing 2).

When this class executes, it creates two threads, passes them the number ranges, assigns thread names, then starts them.

After successfully compiling and testing these classes on your desktop computer, you need to JAR them up in a file called "BasicConsole.jar". The next step copies this JAR file to your iPAQ.

With an open ActiveSync connection, click the ActiveSync Explore icon to view the file structure on your iPAQ. I suggest you copy your JAR file to the "My Pocket PC" folder so you won't have to type in any path arguments to launch your application (see Figure 2).

Figure 2:

To try out this console application on your iPAQ, bring up the Jeode console and type in "-cp BasicConsole.jar basicconsole.app", the result of which is shown in Figure 3.

Figure 3:

Of course, there's not much demand for console apps anymore, so let's build that AWT application. Keep in mind that when using your latest and greatest Java IDE, it may insert all kinds of Swing references that you'll have to delete to create an AWT application that's compliant with JDK 1.1.

AWT App
The AWT application we build will also demonstrate multithreading, reside in a package called "basicawt", and consist of two classes. The code for the main class, called "app," is shown in Listing 3. (Note: It's rather obvious that JBuilder has been used to generate this code.)

The second class is the actual Frame that will display our GUI elements. This class is called "frame1" (see Listing 4).

The Frame loads and displays two buttons and two text boxes. Clicking on either of the buttons will spawn a thread that starts counting from one to 200. This counting will go by almost instantaneously on your desktop but will take a little more time to execute on your iPAQ.

After compiling and testing this app on your computer, JAR it up into a file called "basicawt.jar", then copy it to the same "My Pocket PC" directory that you copied your console application to. To try out this AWT application on your iPAQ, bring up the Jeode console and type in "-cp basicawt.jar basicawt.app". Figure 4 shows what this application should look like.

Figure 4:

(Note: Clicking on either of the buttons will trigger the counting race that's displayed in the corresponding text boxes.)

Keep in mind that this is just the tip of the iceberg when working with AWT. You can display images, check boxes, panels, combo boxes, labels, scrollbars, menus, and much more. That being said, if the AWT was so cool, why was it replaced by Swing? I can't argue there, but I do have a suggestion. Sitraka Software (formerly KL Group) allows you to freely download their pre-Swing GUI components from www.sitraka.com/software/jclass. These JavaBeans make a great replacement for AWT and even rival Swing with components that include tree views, tabs, multicolumn list boxes (grids), progress meters, sliders, spinners, and windows splitters, to name a few. You can easily put together a pretty slick GUI (see Figure 5).

Figure 5:

Now that you know what can be done with Java on the iPAQ, let's wrap up with one more note on the deployment of your applications. You can imagine that the average user won't be interested in launching his or her Java apps by typing commands in the Jeode console. Luckily, there's a better way through the use of shortcuts.

Start out by creating a text file on your desktop with a descriptive name like "BasicAWT.txt". Now change the "txt" extension to "lnk" to turn it into a shortcut. Open this shortcut in Notepad and type in text similar to what you typed in the Jeode console. To launch your BasicAWT application, the text on the first line of your "lnk" file will look like "1#\windows\evm.exe -cp basicawt.jarbasicawt.app". All you have to do now is save and copy this file to your iPAQ in the \Windows\Start Menu directory. From now on, you'll see the Jeode icon along with "BasicAWT" listed on the menu that you pull up from the Start button.

Congratulations! You've gone from learning about PersonalJava, to installing the Jeode VM and building and deploying your own Java apps. I hope you've enjoyed this glimpse into the world of Java on the iPAQ. Good luck in your development efforts.

Author Bio
Rob Tiffany is vice president of technology for True Quote (truequote.com), an online energy trading company. He has published numerous articles for a variety of magazines on topics ranging from Java servlets to wireless technologies. Rob is the author of Pocket PC Database Development with eMbedded Visual Basic from Apress.