承接上篇環境建置,本文實作第一個 MIDlet,並完整演示編譯、預驗證、執行、封裝 JAR/JAD、建立 MIDlet Suite 的端到端流程。
// simpleMIDlet.java
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class simpleMIDlet extends MIDlet implements CommandListener {
private Display display;
private TextBox textBox;
private Command cmdExit;
public simpleMIDlet() {
display = Display.getDisplay(this);
cmdExit = new Command("Exit", Command.SCREEN, 1);
textBox = new TextBox("Simple MIDlet", "Welcome", 50, 0);
textBox.addCommand(cmdExit);
textBox.setCommandListener(this);
}
protected void startApp() {
display.setCurrent(textBox);
}
protected void pauseApp() {}
protected void destroyApp(boolean unconditional) {}
public void commandAction(Command c, Displayable s) {
if (c == cmdExit) {
destroyApp(false);
notifyDestroyed();
}
}
}
# 1. 編譯 (指定 bootclasspath 為 MIDP 類庫,避免引用 J2SE 類)
javac -bootclasspath c:\j2me\midp-fcs\classes simpleMIDlet.java
# 2. 預驗證 (產生可在裝置執行的 class)
preverify -classpath c:\j2me\midp-fcs\classes;. -d . simpleMIDlet
# 3. 執行於模擬器
midp simpleMIDlet
關鍵:-bootclasspath必須指向 MIDP 類庫,否則javac會使用 J2SE 類庫導致編譯通過但裝置無法執行。
.class 檔案META-INF/MANIFEST.MF 清單檔| 屬性 | 範例值 |
|---|---|
| MIDlet-Name | My Game Pack |
| MIDlet-Version | 1.0 |
| MIDlet-Vendor | My Company |
| MIDlet-1 | Game1, /icon1.png, com.my.Game1MIDlet |
| MicroEdition-Profile | MIDP-2.0 |
| MicroEdition-Configuration | CLDC-1.1 |
MIDlet-Name: KOF2001
MIDlet-Version: 1.0
MIDlet-Vendor: NeoRage Corporation Inc.
MIDlet-1: kof, /images/kof.png, kof.KOFMIDlet
MicroEdition-Profile: MIDP-2.0
MicroEdition-Configuration: CLDC-1.1
MIDlet-Jar-URL: https://server/kof.jar
MIDlet-Jar-Size: 45678
MIDlet-Data-Size: 1024
MIDlet-Description: Classic fighting game
MIDlet-Icon: /icon.png
MIDlet-Info-URL: https://server/info.html
MIDlet-Jar-URL 指向 JAR 下載位址
MIDlet-Jar-URL: https://domain/directory/test.jar
MIDlet-Jar-Size: 12345
原始碼 (.java)
↓ javac -bootclasspath MIDP_classes
類別檔 (.class)
↓ preverify
預驗證類別檔
↓ jar + manifest
JAR 封裝檔
↓ 建立 JAD (含 MIDlet-Jar-URL)
部署至 Web Server (設定 MIME)
↓ 使用者手機瀏覽 JAD URL
OTA 安裝 → 執行