首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> J2ME

J2ME 與 MIDP 開發實戰 (二):從原始碼到 MIDlet Suite 封裝部署完整流程

     編輯:J2ME

承接上篇環境建置,本文實作第一個 MIDlet,並完整演示編譯、預驗證、執行、封裝 JAR/JAD、建立 MIDlet Suite 的端到端流程。

一、撰寫第一個 MIDlet 原始碼


// 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 類庫導致編譯通過但裝置無法執行。

三、MIDlet Suite 封裝:JAR 與 JAD

JAR 檔案結構

  • 所有 .class 檔案
  • 資源檔 (圖片、資料檔)
  • META-INF/MANIFEST.MF 清單檔

Manifest.mf 必備屬性 (六大強制項)

屬性範例值
MIDlet-NameMy Game Pack
MIDlet-Version1.0
MIDlet-VendorMy Company
MIDlet-1Game1, /icon1.png, com.my.Game1MIDlet
MicroEdition-ProfileMIDP-2.0
MicroEdition-ConfigurationCLDC-1.1

完整 Manifest 範例


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

JAD (Java Application Descriptor) 檔案

  • 部署描述檔,內容與 Manifest 類似但供「應用程式管理器」下載前檢查
  • 關鍵差異:JAD 必須包含 MIDlet-Jar-URL 指向 JAR 下載位址

MIDlet-Jar-URL: https://domain/directory/test.jar
MIDlet-Jar-Size: 12345

四、應用程式管理器 (AMS)

  • 裝置內建軟體,負責 MIDlet 安裝、執行、刪除、錯誤處理
  • 實作由裝置廠商提供,介面各異
  • 支援 OTA (Over-The-Air) 下載:使用者瀏覽 JAD URL → AMS 解析 → 下載 JAR → 安裝

五、開發流程總結圖


原始碼 (.java)
    ↓ javac -bootclasspath MIDP_classes
類別檔 (.class)
    ↓ preverify
預驗證類別檔
    ↓ jar + manifest
JAR 封裝檔
    ↓ 建立 JAD (含 MIDlet-Jar-URL)
部署至 Web Server (設定 MIME)
    ↓ 使用者手機瀏覽 JAD URL
OTA 安裝 → 執行
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved