Detecting Java ME Optional Packages

By Eric Giguere

The Java ME platform comprises configurations, profiles, and optional packages. Like configures and profiles, an optional package is a set of APIs. Unlike a profile, however, it does not define a complete application environment. An optional package is always used with a configuration or a profile. It extends the runtime environment to support device capabilities that are not universal enough to be defined as part of a profile or that need to be shared by different profiles.

Consider the Wireless Messaging API (WMA), a set of classes for sending and receiving Short Message Service (SMS) messages. Because the WMA is an optional package, it can be included on any Java ME device with SMS capabilities, not just MIDP-enabled cellphones. If WMA were part of a specific profile, such as MIDP, its use would have been limited to that profile and its supersets.

Just like profiles and configurations, optional packages are specified through the Java Community Process. So each has its own reference implementation (RI) and test compatibility toolkit (TCK).

Note that it is the vendor of a Java runtime environment – in many cases, the device manufacturer – that controls which optional packages are available, just as it controls which configuration is used and which profiles are available. In general, it isn't possible to use an optional package if it isn't preloaded onto a device as part of the runtime environment. It takes some time after an optional package's specification has been finalized before implementations of the package appear in commercially available devices.

Using Optional Packages

For the application developer, an optional package is just another set of Java classes to place in the Java compiler's classpath. The classes are not packaged with the applications, of course, because the devices that support the optional package include them in their runtime environments.

To detect the presence or absence of an optional package, test for the existence of a class unique to the optional package:

    public static boolean isWMAPresent(){
        try {
                   "javax.wireless.messaging.MessageConnection" );
            return true;
        catch( Exception e ){
            return false;

Choose the class to test with care. For example, the existence of the java.rmi.Remote interface does not imply that the RMI Optional Package (JSR 66) is supported, because the Personal Basis Profile includes the same interface as part of its support for inter-Xlet communication.

If an optional package is required for your application to function, make sure the application checks for its existence, and exits gracefully if it isn't found. Letting the application terminate with a cryptic Class not found error will confuse users.

Example Optional Packages

Optional packages are defined through the Java Community Process. For the complete list of optional packages, refer to the JCP web site for Java ME-related JSRs.

About the Author

Eric Giguere is a software developer and has written extensively on computing topics. He holds BMath and MMath degrees in Computer Science.

hey how can i get java for my computer? it won't let me get it. it is saying:
this installation package could not be opened verify the package exists and that you can access it, or contact the application vendor to verify that this is a valid windows installer package.

Posted by linda marcum on March 11, 2008 at 10:50 PM PDT #

You can download Java from several places; here are 3 options:

- The Java consumer download page: <>
- The developers download page: <>
- The Java SE download page: <>

Posted by christine dorffi on March 12, 2008 at 08:08 AM PDT #

i still download java onto my computer. so how can i get that. i need it to do alot of things on here. i can't even play games on here

Posted by linda marcum on March 13, 2008 at 01:46 PM PDT #

Please see the Download Help FAQ at <>

Posted by christine dorffi on March 13, 2008 at 01:59 PM PDT #

I also have the same question. I want this site to answer. I am really frustrated and Java 2 Standard Platform Edition is my last hope of downloading Java. Please tell me how i can verify that the package exists.

Posted by Rishi Singh on May 13, 2008 at 06:35 AM PDT #

To get Java on your computer, go to this page and choose the specific Java technology you want to download: <>. If you want to download the Java Platform, Standard Edition (Java SE), select that option or go to <>.

This tech tip shows you how to verify that the specific package you are interested in exists. It uses the Wireless Messaging API as an example. To download the Wireless Messaging API, go to <>. Go to <> to read more information on the WMA.

Posted by Christine Dorffi on May 13, 2008 at 08:07 AM PDT #

I am trying to detect the availability of jsr-82 bluetooth support on sony ericsson phones.
I have one phone which does support it and one which does not.

On the one that does not (Z310) I have not been able to find a graceful way of informing the user that the required software is not available.

I have tried -
System.getProperty("bluetooth.api.version") returns null on both phones.
Class.forName("javax.bluetooth.LocalDevice"); returns "Application Error" on un-supported phone.
System.getProperty("microedition.commports"); returns nothing even on the phone that supports bluetooth.

So I am left with having to let the un-supported phone crash the app at the moment with the un-helpful "Application Error" returned to the user.

Any help would be appreciated.

Posted by Mark Campbell on May 19, 2008 at 06:10 PM PDT #

Have you tried trapping the error? Note that java.lang.Error is a subclass of java.lang.Throwable, not java.lang.Exception, so if you're catching Exception then it won't trap errors. Try that.

But some devices don't let you trap those errors, which is silly, but that's the way it is. It may be that there's no good way to do it on all devices.

Posted by Eric Giguere - Java ME developer on May 20, 2008 at 05:50 AM PDT #

Hi Eric, thank you for your suggestion it has solved my problem very neatly.

I have left in the exception catch for those devices that go that route and added the error catch for those devices that want it.

Thanks again Eric.

Posted by Mark Campbell on May 20, 2008 at 08:20 AM PDT #

A little digging finds:

In the original Bluetooth specification - JSR 82 v1.0a (Apr 5, 2002), section 3.3.4 Device Properties states:

"This API defines the additional system properties that may be retrieved by a call to LocalDevice.getProperty, as shown in Table 3-2."

The table in question contains a "bluetooth.api.version" property that will return the String "1.1" if Bluetooth is supported. As you have discovered, you have to:

javax.bluetooth.LocalDevice LocDev = Class.forName( "javax.bluetooth.LocalDevice" )

in order to be able to:

String btver = LocDev.getProperty( "bluetooth.api.version" );

if ( btver == null ) /\* no Bluetooth \*/

which is the Catch-22 - you need a instance of the class to determine if the class is present and supported. As you discovered and Eric confirmed, you have to be prepared to handle the ClassNotFound exception generated by the Class.forName method.

This was fixed in JSR 82 v1.1 (Sep 02, 2006, Maintenance Release 2) where 3.3.4 has been revised to include the sentence:

"Additionally, all properties defined in the Bluetooth API and available through the LocalDevice.getProperty method MUST also be available through the CLDC System.getProperty() method."

The same section also defines the property / value pairs.

bluetooth.api.version "1.1"
obex.api.version "1.1"

Making it possible to:

btver = System.getProperty( "bluetooth.api.version" );

if ( btver == null ) /\* no Bluetooth \*/

without the exception handling of the previous version of the Bluetooth API.

As for JSR 248 MSA - Bluetooth is conditionally mandatory, i.e. if there is Bluetooth hardware then the JSR 82 APIs are present. Bluetooth v1.1 is part of both MSA and MSA Subset.

Posted by Rick Marejka on May 27, 2008 at 04:29 AM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed

Tips for developers who use Java technologies (Java SE, Java ME, JavaFX) for mobile and embedded devices.


« December 2016