MIDIet能夠訪問Java壓縮包(*.JAR)和應用程式描述項檔案(*.JAD)中的屬性(attributes),你可以按照以下代碼實現:
javax.microedition.midlet.MIDlet.getAppProperty(String name)
以下舉一個非常簡單的例子來列印JAR跟JAD檔案中的條目:
import javax.microedition.midlet.*;
public class showProperties extends MIDlet
{
public void startApp() throws MIDletStateChangeException
{
System.out.println("Vendor: " +
getAppProperty("MIDlet-Vendor"));
System.out.println("Description: " +
getAppProperty("MIDlet-Description"));
System.out.println("JadFile Version: " +
getAppProperty("JadFile-Version"));
System.out.println("MIDlet-Data-Size: " +
getAppProperty("MIDlet-Data-Size"));
}
public void pauseApp()
{ }
public void destroyApp(boolean unconditional)
{ }
}
manifest.mf 檔案儲存體在ShowProperties.jar 檔案中:
MIDlet-Name: Show Properties MIDlet
MIDlet-Version: 1.0.1
MIDlet-Vendor: Core J2ME
MIDlet-1: ShowProps, , ShowProperties
MicroEdition-Profile&: MIDP-1.0
MicroEdition-Configuration: CLDC-1.0
MIDlet-Description: A simple property list example
MIDlet-Data-Size: 1500
ShowProperties.jad 檔案
MIDlet-Name: Show Properties MIDlet
MIDlet-Version: 1.0.1
MIDlet-Vendor: Core J2ME
MIDlet-Jar-URL: ShowProperties.jar
MIDlet-Jar-Size: 1190
MIDlet-1: ShowProps, , ShowProperties
MIDlet-Description: A simple property list example
JadFile-Version: 1.5
MIDlet-Data-Size: 500
要點:
1.manifest.mf檔案儲存體在JAR檔案中,那是你實際可以訪問的屬性(attributes)。
2.ShowProperties.jad 檔案中引用了這個JAR檔案:
MIDlet-Jar-URL: showProperties.jar
3.這些屬性包括:MIDlet-Name,MIDlet-Version,MIDlet-Vendor既存在於JAR檔案中的manifest.mf中又存在於JAD檔案中,所有的值都要求是一樣的。
4.屬性(attribute)JadFile Version是使用者自訂的屬性,並沒有在J2ME規格中定義。這樣就可以被MIDIet程式訪問而不用修改JAR檔案。
5.屬性(attribute)MIDlet-Data-Size同時存在於manifest.mf和JAD檔案中。當存在於兩個檔案中的屬性一樣時,JAD檔案中的會被優先選擇。
6.以上例子是基於MIDP和CLDC 1.0.3.