Blog Archive

Friday, May 13, 2011

HelloWorld For J2ME Mobile (Every thing hand coded)

Writing applications for a mobile is totally different task as compared to writing applications for desktop,
because every thing is limited on your mobile device. The memory, processing capabilities, screen size, resolution every thing is limited. So you have to write light applications, which uses minimum resource.

Java for Mobiles (J2ME[Java 2 Micro Edition]):
Ok now here we come to our goal. As J2SE(Java 2 Standard Edition) J2ME is a java specification for Mobile devices. If you know the structure of java, you may know that for java application you need JVM (Java Virtual Machine), but this is specifically for Desktop system, which has greater resources as compared to mobiles. So on mobiles there is KVM (Kilobyte Virtual Machine).
And there are two important terms which you should know.
CLDC (Connected Limited Device Configuration) &
MIDP (Mobile Information Device Profile).

I won’t go in too much details because I might thinking that for now you are just interested in creating working program. So I will explain it in my later installments.

What you need:
You need two things to compile this tutorials
1. JDK 1.4(Yes it is, someone would say that I will use the latest JDK, but trust me you won’t get your program run on mobile without this, and ‘why this’ I will explain later)
2. Sun Wireless toolkit 2.5 or later (WTK)
So install these two things. You can find both of these tools on sun's website
Now I am assuming that you have installed Sun Wireless toolkit on c:\wtk25. and you have installed jdk 1.4 in c:\jdk1.4. and also you have started command prompt with following path set .

Set path=c:\jdk1.4\bin;c:\windows\system32;c:\windows;c:\wtk25\bin;

Step 1. Open command prompt and create following director structure on c:\.
Step2. Go to C:\myapp\src\prj and create and write following code in it.
package prj;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class helloMIDlet extends MIDlet
public TextBox tb;
public helloMIDlet()
tb = new TextBox("Hello","Balkans",15,0);
public void startApp()
public void destroyApp(boolean b)
public void pauseApp(){}

Step3. Now create cmp.bat file and write following.
javac -classpath c:\wtk25\lib\midpapi20.jar;c:\wtk25\lib\lcdui10.jar;. *.java
Step4. Now type cmp and press enter it will compile your
Step5. Goto c:\myapp and create pre.bat and write following in it.

preverify -d .\class -classpath c:\wtk25\lib\midpapi20.jar;c:\wtk25\lib\cldcapi10.jar;f:\myapp\src; prj.helloMIDlet

Step6. goto c:\myapp\class and create manifest.txt and write following in it.
MIDlet-Vendor: Balkans
MIDlet-Version: 1.1.0
MIDlet-Name: prj.helloMIDlet
MIDlet-1: helloMIDlet,,prj.helloMIDlet
MIDlet-Description: helloMIDlet
MicroEdition-Profile: MIDP-2.0
MicroEdition-Configuration: CLDC-1.0

Step7. Now create jarc.bat and write following in it.
jar -cvfm hello.jar manifest.txt prj
Step 8. Type jarc and press enter.
It will create a hello.jar. you can copy this jar file to your mobile and install to test.
Or alternatively you can create a jad file to run it on emulator.
Step 9. Create hello.jad and write following in it.
MIDlet-Name: prj.helloMIDlet
MIDlet-Vendor: Balkans
MIDlet-Version: 1.1.0
MIDlet-Description: hello
MicroEdition-Profile: MIDP-2.0
MicroEdition-Configuration: CLDC-1.0
MIDlet-Jar-URL: hello.jar
MIDlet-Jar-Size: size of hello.jar in bytes

To know the size of hello.jar right click on hello.jar and go to properties and see Size(not size on disk) and see the no of bytes written in ().

Step 10. now double click on hello.jad it will launch the emulator.

No comments:

Post a Comment