Introduction to J2ME

1) Introduction

J2ME (Java 2 Micro Edition) is an advanced technology in Java, developed with the help of Java Community Process Program. J2ME is a reduced version of the Java API and Java Virtual Machine that is designed to operate within the limited resources available in the embedded computers and microcomputers.

also read:

J2ME is targeted to developers of intelligent wireless devices and small computing devices who need to incorporate cross-platform functionality in their products. A key benefit of using J2ME is compatibility with all Java-enabled devices. Motorola, Nokia, Panasonic all have Java-enabled devices.A J2ME application is a balance between local and server-side processing.
The Java Community Process Program used a two approaches to addressing the needs of small computing devices.

  • Configurations:It is the Java run-time environment and core classes that operate on each device. A configuration defines the Java Virtual Machine for a particular small computing device. There are two configurations.
    • CLDC for handheld devices:The CLDC (Connected Limited Device Configuration) is designed for 16-bit or 32-bit small computing devices with limited memory. These devices usually have between 160KB and 512KB of available memory. Usually these are powered by battery. They use small-bandwidth network wireless connection. These devices uses a stripped-down version of the JVM the KJava Virtual Machine (KVM). These devices include pagers, personal digital assistants, cell phones, dedicated terminals, and handheld consumer device.
    • CDC for plug-in devices:CDC(Connected Device Configuration) devices use a 32-bit architecture, have at least 2 MB of memory available, and implement a complete functional JVM. CDC devices include digital set-top boxes, home appliances, navigation systems, point-of-sale terminals, and smart phones.
  • Profiles:It is defined for categories of small computing devices. A profile consists of classes that enable developers to implement features found on a related group of small computing devices.List of J2ME Profiles:
    • Profiles Used with CLDC:
      • Mobile Information Device Profile(MIDP)
      • PDA Profile(PDAP)
    • Profiles Used with CDC:
      • Foundation Profile
      • Game Profile
      • Personal Profile
      • Personal Basis Profile
      • RMI Profile.

2)J2ME Architecture

There are 5 layers in J2ME Architecture.Those are:

  • MIDP (TopMost Layer): Which contains Java APIs for user network connections, persistence storage, and the user interface. It also has access to CLDC libraries and MIDP libraries.
  • J2ME API’s(Profiles): Which consists of the minimum set of application programming interfaces for the small computing device
  • Configurations: Which handles interactions between the profile and the JVM.
  • JVM
  • Operating System(Bottom Layer).

1,2 and 3 are software Layers.

3) Midlets.

A MIDlet is a J2ME application which operate on an MIDP. A MIDlet is defined with at least a single class that is derived from the javax.microedition.midlet.MIDlet abstract class. Common programming is grouping related MIDlets into a MIDlet suite, which is contained within the same package and implemented simultaneously. All MIDlets within a MIDlet suite are considered a group and must be installed and uninstalled as a group. MIDlets from the same MIDlet suite run the same class. Benefit of the relationship among MIDlet suite members is that they share the same data.
A MIDlet is an event-based application. All routines executed in the MIDlet are invoked in response to an event reported to the MIDlet by the application manager. The initial event that occurs is when the MIDlet is started and the application manager invokes the startApp() method. The startApp() method in a typical MIDlet contains a statement that displays a screen of data and prompts the user to enter a selection from among one or more options. The nature and number of options is, MIDlet and screen dependent. A Command object is used to present a user with a selection of options to choose from when a screen is displayed. Each screen must have a CommandListener. A CommandListener monitors user events with a screen and causes the appropriate code to execute based on the current event.
Here we dicuss about Application Manager Responsibilities which is running on the device which is provided by the Device manufacturer:

  • Installing, executing, and removing a MIDLet Suite.
  • Giving access to classes of the JVM and CLDC for Each member of the MIDlet suite.
  • Makes the Java archive (JAR) file and the Java application descriptor (JAD) file available to members of the MIDlet suite.

3.1) JAR Files

3.1.1)Something About Manifest File:

  • Nine attributes are defined in this file.The first six attributes are necessary.
  • The file’s extension is changed to .mf when the MIDlet is prepared for deployment.
  • All entries are name:value pairs.
  • Each pair must be terminated with a carriage return.
  • Whitespace between the colon and the attribute value is ignored.

Here are Necessary Attributes of a Manifest File :

  • MIDlet-Name: MIDlet suite name.
  • MIDlet-Version: MIDlet version number.
  • MIDlet-Vendor: Name of the vendor who supplied the MIDlet.
  • MIDlet-n: Attribute per MIDlet. Values are MIDlet name, optional icon, and MIDlet class name.
  • MicroEdition-Profile: Identifies the J2ME profile that is necessary to run the MIDlet.
  • MicroEdition-Configuration: Identifies the J2ME configuration that is necessary to run the MIDlet.

Optional Attributes are

  • MIDlet-Icon: Icon associated with MIDlet, must be in PNG image format.
  • MIDlet-Description: Description of MIDlet.
  • MIDlet-Info-URL: URL containing more information about the MIDlet.

3.2) JAD Files

JAD Files are used to pass parameters to a MIDlet without modifying the JAR file . Also used to provide the application manager with additional content information about the JAR file to determine whether the MIDlet suite can be implemented on the device. There are five necessary attributes for a JAD file:

3.2.1)Something About JAD File

  • Files must have the .jad extension.
  • The values of the MIDlet-Name, MIDlet-Version, and MIDlet-Vendor attributes in the JAD file must match the same attributes in the manifest .
  • All entries are name:value pairs.

Necessary Attributes are

  • MIDlet-Name: MIDlet suite name.
  • MIDlet-Version: MIDlet version number.
  • MIDlet-Vendor: Name of the vendor who supplied the MIDlet.
  • MIDlet-n: Location of the JAR file.
  • MIDlet-Jar-URL: Attribute per MIDlet. Values are MIDlet name, optional icon, and MIDlet class name.

Optional attributes available are

  • MIDlet-Jar-Size: Size of the JAR file in bytes.
  • MIDlet-Data-Size: Minimum size (in bytes) for persistent data storage.
  • MIDlet-Description: Description of MIDlet.
  • MIDlet-Delete-Confirm: Confirmation required before removing the MIDlet suite.
  • MIDlet-Install-Notify: Send installation status to given URL.

3.3) Programs

A MIDlet is a class that extends the MIDlet class and is the interface between application statements and the run-time environment , which is controlled by the application manager . A MIDlet class must contain three abstract methods that are called by the application manager to manage the life cycle of the MIDlet. These abstract methods are.

  • startApp(): called by the application manager when the MIDlet is started and contains statements that are executed each time the application begins execution. Public and have no return value nor parameter list.
  • pauseApp(): called before the application manager temporarily stops the MIDlet. The application manager restarts the MIDlet by recalling the startApp() method. Public and have no return value nor parameter list.
  • destroyApp(): called prior to the termination of the MIDlet by the application manager. Public method without a return value. It has a boolean parameter that is set to true if the termination of the MIDlet is unconditional, and false if the MIDlet can throw a MIDletStateChangeException.

The Basic Midlet Shell.

	public class BasicMIDletShell extends MIDlet
{
	public void startApp(){ }
	public void pauseApp(){ }
	public void destroyApp( boolean unconditional){ }
}

MIDP API classes are used by the MIDlet to interact with the user and handle data management. User interactions are managed by user interface MIDP API classes. These APIs prompt the user to respond with an appropriate command. The command causes the MIDlet to execute one of three routines:

  • Perform a computation.
  • Make a network request.
  • Display another screen.

The data-handling MIDP API classes enable the developer to perform four kinds of data routines:

  • Write and read persistent data.
  • Store data in data types.
  • Receive data from and send data to a network.
  • Interact with the small computing device’s input/output features.

3.4) Differences Between Core Java and Java for J2ME

Major Differences are :

  • Floating-point math is missing in J2ME. MIDlet cannot use any floating-point data types or calculations.
  • Absence of finalize() method in J2ME.
  • Reduced number of error-handling exceptions in J2ME.
  • JVM for small computing devices requires a custom class loader that is supplied by the device manufacturer and cannot be replaced or modified.
  • We cannot group threads. All threads are handled at the object level.
  • The class file verification is replaced with two processes called Preverificaton and Validation. Preverification occurs prior to loading the MIDLet’s and Validation occurs after loading the MIDLet.

4)Simple Example Program

This section shows a simple application. The MIDlet will display the message “ Hi This is My First Program” on the screen. Clicking the Exit button will terminate the MIDlet. The application consists of two classes: the MIDlet subclass called FirstOne and the Canvas subclass called FirstCanvas.

// **********FirstOne.java************

Package FirstOne;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
 
public class FirstOne extends MIDlet implements CommandListener
{
// The canvas is the region of the screen that has been allotted to the game. 
 
	FirstCanvas myCanvas; 
	private Command exitCommand = new Command("Exit", Command.EXIT, 99);
	private Command changeCommand = new Command("changeMessage ", Command.SCREEN, 1);

// Initialize the canvas and the commands.

	public FirstOne() 
	{
	  myCanvas = new FirstCanvas();  
	  myCanvas.addCommand(exitCommand);
	  myCanvas.addCommand(changeCommand);
	  
	// Set one command listener to listen to all of the commands on the canvas  

	  myCanvas.setCommandListener(this);
	}

	public void startApp() throws MIDletStateChangeException
	 {
	// display my canvas on the screen:
	  Display.getDisplay(this).setCurrent(myCanvas);  
	  myCanvas.repaint();
	}

// If the MIDlet was using resources, it should release them in this method.
 
	public void destroyApp(boolean unconditional) 
	    throws MIDletStateChangeException { }

 //MIDlet enters a paused state. The MIDlet should release shared resources. 

	public void pauseApp() {  }

//Respond to a command. Either reset or exit.
 
	public void commandAction(Command c, Displayable s) 
	{
		  if(c == changeCommand) 
		{
		    myCanvas.changeFirst();
		} 
		else if(c == exitCommand) 
		{ 
		    try {
		      destroyApp(false);
		      notifyDestroyed();
		    } catch (MIDletStateChangeException ex){  }
		}
	 }
}

// *************FirstCanvas.java.***************

package FirstOne;
import javax.microedition.lcdui.*;
//This class represents the region of the screen that has been allotted.

public class FirstCanvas extends Canvas 
{
	boolean firstDisp = true;

//Change the display message.

	void changeMsg() 
	{
	  firstDisp = !firstDisp;
	  repaint();
	}

//clear the screen and display the " Hi This is My First Program " message.
 
	public void paint(Graphics g) 
	{
	  
	// get the dimensions of the screen:
	  int width = getWidth ();
	  int height = getHeight();
	  
	// clear the screen. Setting Screen Color to white:
	  g.setColor(0xffffff);	  
	  g.fillRect(0, 0, width, height);
	  
	// display the " Hi This is My First Program " message if appropriate:.
		if(firstDisp) 
		{
		    Font font = g.getFont();
		    int fontHeight = font.getHeight();
		    int fontWidth = font.stringWidth("Hi This is My First Program ");
		// set the text color
		    g.setColor(255, 255, 0);
		    g.setFont(font);
		   
		// write the string in the center of the screen
		    g.drawString("Hi This is My First Program ", (width - fontWidth)/2,   
		                  (height - fontHeight)/2,g.TOP|g.LEFT);
		}
	}
}

5) Limitations with J2ME

Some ofthe limitations of J2ME compared with Core JAVA

  • Some J2SE applications require classes that are not available in J2ME.
  • Java applications won’t run in the J2ME environment without requiring modification to the code.
  • Devices that use the CDC configuration use the full Java Virtual Machine implementation, while devices that use the CLDC configuration use the Kjava Virtual Machine implementation.
  • MIDlets are controlled by application management software (AMS). So we cant invoke a MIDLET like a J2SE Application.

6)Conclusion

Herewith we have an introduction to Java 2 Micro Edition. This example is very simle example. It can be executed using editors like NetBeans IDE or J2ME Wireless Toolkit(KToolBar). Using API’s like JSR 135 etc.. try to create more programs and try them in these tools. I will get back to you with more articles on J2ME.Refer this site for more information on starting with J2ME.

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Comments

  1. its very nice n helpful

Speak Your Mind

*