What is an Optional Package?

An optional package is also a set of APIs, but unlike a profile, it does not define a complete application environment. An optional package is always used in conjunction 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 J2ME 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.

Because, just like profiles and configurations, optional packages are specified through the Java Community Process, each has its own reference implementation (RI) and test compatibility toolkit (TCK). Besides aiding the vendors in implementing optional packages as part of their runtime environments, the RI and TCK also ensure that those implementations are done consistently and correctly, no matter which device is being used.

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 {
        Class.forName(
               "javax.wireless.messaging.MessageConnection" );
        return true;
    }
    catch( Exception e ){
        return false;
    }
}
...

JSR 66: RMI Optional Package

JSR 120: Wireless Messaging API

As I mentioned earlier, the Wireless Messaging API lets J2ME applications send and receive messages using the Short Message Service, a messaging protocol used by cellphones across the world. The WMA extends the CLDC's Generic Connection Framework (GCF) by defining a new connection interface, javax.wireless.messaging.MessageConnection, and exposing SMS and CBS connections (short for Cell Broadcast Service, a related protocol) through protocol handlers for URLs beginning with "sms:" or "cbs:". For example, here's how to send an SMS text message with the WMA:

...

 

import javax.microedition.io.*;

import javax.wireless.messaging.*;

 

...

 

MessageConnection conn = null;

String url = "sms://+417034967891";

 

try {

    conn = (MessageConnection) Connector.open( url );

    TextMessage msg = conn.newMessage( conn.TEXT_MESSAGE );

    msg.setPayloadText( "Please call me!" );

    conn.send( msg );

}

catch( Exception e ){

    // handle errors

}

finally {

    if( conn != null ){

        try { conn.close(); } catch( Exception e ){}

    }

}

...

 

The WMA can be used with any J2ME profile.

JSR 135: Mobile Media API