Content

Installing JDK ...

Firstly, goto "http://java.sun.com" and grab a copy of the latest JDK (notice: only J2RE will only allow you to run java apps, if you are going to developement MUST use a JDK).
Unzip it and put it in a folder, here we use "c:/java/j2sdk" as a default directory!
Then you have to setup an environment variable in order to make work.

Setting up environment variable for Java ...

Right click on "My Computer", select "properties"->"Advanced"->"Environment Variables", and you will see stuff like below:JAVA_HOME=C:\java\jdk1.2.4_01 (refer to path where you install the Java sdk)

PATH= ... ;C:\java\jdk1.2.4_01\bin

Installing J2ME Toolkit ...

This may be as simple as it seems, just goto j2me download site, pick a copy of " Sun Java Wireless Toolkit" ... install it and start coding. Note that you may have a few choice either using an IDE like NetBean, JBuilder, and so on; to the build-in code writing module with the WirelessToolkit that you just downloaded or even start off with a Notepad!

Well my personal favor, a Notepad will do, try compiling with command prompt also, had a better feel of the whole development process.


Testing ...
Kick Start

Six simple Steps for writing a J2ME:
  1. Code a Java file.
  2. Compile with CLDC and MIDP library.
  3. Preverify it with "preverify.exe".
  4. Write a manifest file, then package it with every classes and resources in a JAR file.
  5. Write a JAD file for deploying purposes.
  6. Test it on emulator or real device, do some performance tweaking and fine tuning for portability.

    Some assumption:
  • %J2ME_HOME%=C:\WTK22\
  • %JAVA_HOME%=C:\Program Files\Java\jdk1.5.0_04

Writing a simple CLDC app...
import java.io.*;
import javax.microedition.io.*;

public class TestInputOutput {

public static void main( String[] args ){
try {
String uri ="socket://www.ericgiguere.com:80";
StreamConnection conn = (StreamConnection)
Connector.open( uri );

// Send HTTP request...
PrintStream out = new PrintStream(conn.openOutputStream() );
out.println( "GET /index.html HTTP/0.9\r\n" );
out.flush();

// Get raw HTTP reply...
InputStream in = conn.openInputStream();
int ch;

while( ( ch = in.read() ) != -1 ){
System.out.print( (char) ch );
}

in.close();
out.close();
conn.close();
}
catch( ConnectionNotFoundException e ){
System.out.println( "Socket could not be opened" );
}
catch( IOException e ){
System.out.println( e.toString() );
}
System.exit( 0 );
}
}


Writing a simple MIDP app...
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

public class HelloMIDlet
extends MIDlet
implements CommandListener {
private Form mMainForm;

public HelloMIDlet() {
mMainForm = new Form("HelloMIDlet");
mMainForm.append(new StringItem(null, "Hello, MIDP!"));
mMainForm.addCommand(new Command("Exit", Command.EXIT, 0));
mMainForm.setCommandListener(this);
}

public void startApp() {
Display.getDisplay(this).setCurrent(mMainForm);
}

public void pauseApp() {}

public void destroyApp(boolean unconditional) {}

public void commandAction(Command c, Displayable s) {
notifyDestroyed();
}
}

To Compile your codes:
%JAVA_HOME%\bin\javac -bootclasspath %J2ME_HOME%\lib\midpapi20.jar;%J2ME_HOME%\lib\cldcapi.jar MyJ2MEApp.java

To Preverify your class:
%J2ME_HOME%\bin\preverify.exe -classpath %J2ME_HOME%\lib\midpapi20.jar;%J2ME_HOME%\lib\cldcapi.jar MyJ2MEApp

Writing a Menifest file:
MIDlet-Name: MyJ2MEApp
MIDlet-Version: 1.0.0
MIDlet-Vendor: Avatar

To Package everything:
%JAVA_HOME%\bin\jar cvfm MyJ2MEApp.jar Manifest.mf MyJ2MEApp.class

Writing a Java Application Describtion (JAD) file:
MIDlet-1: MyJ2MEApp, , MyJ2MEApp
MIDlet-Name: MyJ2MEApp
MIDlet-Version: 1.0.0
MIDlet-Vendor: Avatar
MIDlet-Jar-URL: MyJ2MEApp.jar
MIDlet-Jar-Size:
MicroEdition-Profile: MIDP-2.0
MicroEdition-Configuration: CLDC-1.1

*Important notes - remember to fill in the MIDlet-Jar-Size with a correct file size of your jar (e.g.:MIDlet-Jar-Size: 1469)*
To Test your application:
%J2ME_HOME%\bin\emulator.exe -Xdescriptor MyJ2MEApp.jad

Hooray, it is working!!!

Resources:
  1. Tutorial from java.net.
  2. Sample code from sun developer.

Comments

Popular posts from this blog

JDBC comes to J2ME finally?

JSR-184 a.k.a. Micro3D a.k.a. Java Mobile 3D