Thursday, March 11, 2021

J2ME Jumpstart Tutorial With Wireless Toolkit 2.5.2 Part 2

J2ME Jumpstart Tutorial With Wireless Toolkit 2.5.2 Part 2
compile with the Wireless Toolkit


Using the wireless toolkit


If you already installed the WTK (for Windows, Linux, Mac OS or Solaris), you should able to launch it with ktoolbar command.

$ /usr/local/wtk-2.5.2/bin/ktoolbar


If you use the Linux package to install on macOS, you are not able to build the j2me project. Then follow the command line tutorial (part 3). This tutorial give you a brief understanding of how the j2me project build and run.

Create new project


Create a new project:

New > Project:
Project Name: [ HelloSuite ] / HelloWorld
MIDlet Class Name: [ HelloWorld ]
(To use a different project name can differentiate which field is project name, and which field is class name)

The project will be created at <home>/j2mewtk/2.5.2/apps/HelloSuite (Project name), and the following directory:
src/ # source code here, now empty
lib/ # extra library
res/ # resource
bin/ # package file
classes/ # classes after preverify
tmpclasses/ # classes before preverify
tmplib/

The WTK doesn't include an editor. Use your preferred editor to create src/HelloWorld.java

$ edit src/HelloWorld.java
  import javax.microedition.lcdui.*;
  import javax.microedition.midlet.*;

  public class HelloWorld extends MIDlet
    implements CommandListener {
  private Form mMainForm;
 
  public HelloWorld() {
    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();
  }
}
  


Hit the build button, sytem will compile the program in the src directory. If everything goes smoothly, it just shows:

⇒ Build complete
Note: This might not working in Mac OS, because the preverifier on Mac OS is not available. Follow the part 3 to complete the tutorial.
Run the project, program will be displayed in the emulator. If the emulator is not working, can try this j2me emulator https://github.com/hex007/freej2me:

$ jar -jar build/freej2me.jar

Select the HelloSuite.jar

No comments: