Full BlackBerry Java

This article was originally published in VSJ, which is now part of Developer Fusion.
We have looked at two easy routes to building BlackBerry applications, web-based and RAD (see the March and April issues of VSJ), now it’s time to turn our attention to the full API and application development using Java. If the mention of Java makes you feel in the slightest bit uneasy, the first and most important thing to say is that Java is a lot like C# (or perhaps more fairly C# is a lot like Java). The two are both object-oriented, block-structured languages, and apart from a few minor differences that might slow you down the first time you encounter them, if you program in C# you probably don’t even need to be re-educated in how to program in Java.

If you are a .NET programmer then the second problem might be the need to use an alternative IDE. Again there is no reason to regard the need to switch IDE as a huge barrier as the whole point of an IDE is to make writing programs easy and, just like languages, they tend to follow the same basic design. In the case of the BlackBerry and Java development you have two choices. You can either download a plug-in for the Eclipse IDE or you can download a standalone development environment. If you already use Eclipse then the choice of the plug-in is obvious, but if you just want to experiment with BlackBerry development and don’t want to use any of the other facilities Eclipse provides then the standalone environment is the better choice.

Either way you will need to download the BlackBerry JDE and the latest JDK from the Sun website. If you download and run the JDE from the BlackBerry site then a requirements checker will make sure that your machine is correctly configured and take you to the appropriate website so that you can download anything that is missing. One slight complication is that different devices use different versions of the software. If you don’t want to use any of the latest features then simply use version 4.0, but to find out about the system use the most up-to-date. You also need to make sure that you download and install the correct JDK for the JDE you have selected. The Sun download site isn’t particularly helpful in this respect as it keeps on changing the exact naming of each of the download offerings.

BlackBerry applications are Java ME (Micro Edition) programs, and you can make use of the MIDP standard Java APIs, but there are also some additional classes added to allow you to make use of the unique features of the platform. The standalone IDE contains everything you need to create and debug applications. You can create a project using the IDE, build it and try it out using the supplied Smartphone Simulator. The range of applications you can build is more or less unconstrained by the development environment. Of course you can build standalone applications such as games that don’t make use of connectivity of any sort, but the most valuable applications are, equally obviously, going to make use of the range of connectivity options that a BlackBerry device offers. To this end you not only get a device simulator but also an MDS and email simulator to provide sample connectivity.

Hello World demo
The Hello World demo saying goodbye

To get started it is worth having a look at the collection of samples that are included with the download. This will give you some idea about the range of applications you can create, check that you have everything installed and configured correct and familiarise yourself with the use of the IDE.

To get started use the File, Open Workspace command and navigate to the samples directory contained within the Research in Motion directory in the standard Program Files directory. You need to load the samples.jdw file which populates the workspace with a large number of projects. If you use the command Build All and Run then all of the projects are compiled, linked and loaded into the device simulator for you to look at. A better way to get started is to narrow down the test projects to just one – use the command Project,Set Active Projects and clear all but the HelloWorldDemo project. Now you can use the Workspace browser to examine the structure of the demo.

Demo screen 1
The standalone development system is an easy IDE to master

The HelloWorldDemo project is composed of two files, a .png graphic which acts as the applications icon and a Java file which contains two classes, the application and its display screen. If you examine the HelloWorldDemo file in more detail you should be able to see how a basic BlackBerry application works. The HelloWorldDemo class inherits from the UiApplication class and its main method is the entry point for the application. Here an instance of the applications main class is created and the application started:

public static void main(String[] args)
//create a new instance of the
//application and start the application
//on the event thread
	HelloWorld theApp = new HelloWorld();
In this case the HelloWorld constructor simply creates an instance of the display class and pushes it on the stack of screens so making it visible to the user:
public HelloWorld()
//display a new screen
	pushScreen(new HelloWorldScreen());
The HelloWorldScreen class, which inherits from MainScreen, is used to construct a display using control classes in much the same way as you would in any object oriented language that provides a control library but no drag-and-drop designer. That is, you simply create the controls you need by instantiating them and customising them by setting properties. In this case we just need some text fields. The first is a LabelField used to set the text in the title bar of the application:
	LabelField title = new LabelField(
		"Hello World Demo" ,
		LabelField.ELLIPSIS |
The second is a RichTextField which displays the usual message:
add(new RichTextField(
	"Hello World!", Field.NON_FOCUSABLE));
The only other HelloWorldScreen method is an optional override of the close method which is inherited from MainScreen and automatically called when the application ends. In this case is used to display a dialog box and goodbye message:
public void close()
// Display a farewell message
// before closing application.
If you now use the Build, Build Selected menu command you should see “Build complete”. If you see any messages concerning a File I/O error or not being able to find javac then the chances are that your development machine’s configuration is wrong. Make sure that you have a path variable set to point at the directory that the correct javac (Java compiler) is stored. As the path variable is not much used any more it is worth mentioning that you have to do this by right-clicking on MyComputer, selecting Properties, Advanced and Environment Variables. Edit the Path to ensure that it starts with something like:
C:\Program Files\Java\jdk1.6.0_13\bin
…and that the bin directory contains javac and it is the correct JDK version.

Once you have a correct build you can run the application using the Debug,Go command which automatically starts the device simulator and downloads the code for your program. The first time this happens it takes some time for the simulator to load and initialise. When it does load you might be puzzled as to where your application is? The simple answer is that it is in the Downloads folder. Click the menu button at the bottom, click the Downloads folder and you should see the Hello World icon. If you click the icon your application will run and you should see the usual message and the dialog box when you close the application. Notice that the running program is under full debugger control and you can experiment by setting breakpoints and single stepping through the program.

Now you are ready to move on and examine the other samples that demonstrate everything from email and http to using the GPS and a built-in accelerometer. One final practical tip is that as you develop applications the Download folder and other settings of the device simulator slowly but surely become full of things you would rather not keep. The solution is to close the simulator, navigate to the folder that it is stored in and double-click the batch file Clean.bat. When the batch has finished the simulator will be reset to the state it was in when you first used it. Now that you know how to clean up the simulator you can use the Build, Build and run all command to compile and download the entire collection of samples to the Download directory in one go!

Demo screen 2
You can set breakpoints and single-step through an application

Once you have your hello world application working the rest is a matter of more of the same. Start a new Workspace, import the necessary Java libraries and create the classes you need. All BlackBerry applications start in the same way with an application class and its helpers and a number of screen classes to display and get information from the user. The BlackBerry API is fully documented in the help file that is supplied as part of the development environment and the developer forums are active and helpful.

After trying each of the three approaches to developing BlackBerry applications I have to conclude that the full Java development approach isn’t so difficult that the RAD approach should always win out. Using the RAD to create a demo or a first iteration and then moving to a Java ME environment seems to be an ideal development ploy.

Mike James has over 20 years of programming experience, both as a developer and lecturer, and has written numerous books on programming and IT-related subjects. His PhD is in computer science.


The starting point for everything developer: www.blackberry.com/developer

You might also like...



Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Anyone who considers arithmetic methods of producing random digits is, of course, in a state of sin.” - John von Neumann