External Links:

Using Preprocessor Directives to Port Java ME Applications

There may be times when you want to take advantage of device-specific features, yet support as wide a selection of devices as possible. For example, perhaps you want to play sound in your application, but you also want to target several different platforms, each with different APIs for playing sounds. You could manually create a new project for each device you wish to support, but that could quickly turn into a nightmare as you try to keep common code in sync. That’s where preprocessor directives come in. This handy feature allows you to insert conditional logic into your source files that will include or exclude blocks of code before it gets to the Java compiler.

For the purposes of this article, we will be focusing on the NetBeans implementation of preprocessor directives. First, you will need to create a configuration for each platform you wish to support. To do this, open the project properties dialog (available from the “File” menu), and click “Manage Configurations” in the upper right hand corner of the dialog. Click “Add…” to add a new configuration. Choose the template that corresponds to the platform you are developing for, then enter a meaningful name for the configuration, and click OK. Note that if you are targeting a platform that utilizes a third-party API, you may need to download the corresponding development kit from the vendor’s website in order to enable the proper templates.

Next, define any variables you wish to reference from the preprocessor directives in the Abilities table. You can do this from the “Abilities” tree node in the project properties dialog. A number of useful variables are already predefined by NetBeans, and you can add others as necessary.

Now you are ready to put preprocessor directives to work. Revisiting our previous tutorial on Sprint MIDP 1.0 Extensions, here are some snippets of code that could be used to play a sound, in both the MIDP 2.0 and Sprint PCS MIDP 1.0 platforms.


//#if MIDP=="2.0"
import javax.microedition.media.*;
//#elif vendor=="SprintPCS"
import com.sprintpcs.media.*;
//#endif

public void playSound(String uri) throws Exception {
//#if MIDP=="2.0"
    
    // Play sound
    Player p = Manager.createPlayer(uri);
    p.start();

//#elif vendor=="SprintPCS"

     // Create a new clip object
    Clip clip = new Clip(
        uri,
        "audio/vnd.qcelp",  // MIME type
        1,  // Priority
        0   // Seconds of vibration
    );

     // Play sound
    Player.playBackground(
        clip, // Clip object
        0   // Do not repeat
    );

//#endif

}

For a more in depth look at the NetBeans preprocessor directive syntax, check out the following article: Defining and Using Preprocessor Directives in the Mobility Pack. You may also want to take a look at another preprocessing solution offered by J2ME Polish.

Bookmark this Post

One Response to “Using Preprocessor Directives to Port Java ME Applications”

  1. Igor Maznitsa Says:

    To use a preprocessor for J2ME is a great idea.. I have been it for own developmants since 2002 , I have written own version of the tool which can be downloaded from my site.. may be it is only preprocessor which allows make cycles with its commands and work with XML files.. it’s free tool
    URL: http://www.igormaznitsa.com/Download/jcp491b.zip

Leave a Reply