⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.java

📁 有关j2me的很好的例子可以研究一下
💻 JAVA
字号:
/** * * @(#)Main.java	1.24 01/08/30 * * Copyright 2001 by Sun Microsystems, Inc., * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A. * All rights reserved. */package com.sun.midp;import java.io.IOException;import java.io.ByteArrayInputStream;import java.lang.String;import com.sun.midp.dev.DevMIDletSuiteImpl;import com.sun.midp.midlet.MIDletInfo;import com.sun.midp.midlet.MIDletSuite;import com.sun.midp.midletsuite.Installer;import com.sun.midp.midletsuite.InvalidJadException;/** * The first class loaded in VM by main.c to initialize internal security and * perform a MIDP command. * <p> * This class performs 6 basic commands: * <ul> * <li>Application installation * <li>Application removal * <li>Application listing * <li>Application execution * <li>Graphical Application Management * <li>Execute a single MIDlet from the classpath * </ul> */public class Main {    /**     * Initializes internal security, performs then next     * command indicated in the command state, and sets up the next command     * to be performed. The command loop is in main.c.     *     * When done exitInternal is called with a special constant so     * main.c knows that this was the last method run, as opposed to the VM     * aborting.     * @param args not used, instead a {@link CommandState} object is obtained     *             using a native method.     */    public static void main(String args[]) {        CommandState state = new CommandState();        /*         * We will try to handle any printing at this level, because         * displaying JAM command line errors is device specific.         */                 try {            initializeInternalSecurity();            restoreCommandState(state);            // handle any development machine only functions at this level            switch (state.nextCommand) {            case CommandProcessor.RUN_CLASS:                runLocalClass(state);                state.nextCommand = CommandProcessor.EXIT;                break;            case CommandProcessor.MANAGE:                manage(state);                break;            case CommandProcessor.LIST:            case CommandProcessor.STORAGE_NAMES:                list(state);                state.nextCommand = CommandProcessor.EXIT;                break;            default:                CommandProcessor.perform(state);                if (state.status == CommandProcessor.MIDLET_SUITE_NOT_FOUND) {                    System.out.println("The MIDlet suite was not found.");                }                if (state.initialCommand == CommandProcessor.INSTALL &&                    state.status == CommandProcessor.OK) {                    System.out.println("Storage name: " +                                       state.suiteStorageName);                }            }        } catch (InvalidJadException ije) {            System.out.println("** Error installing suite (" +                               ije.getReason() + "): " +                                messageForInvalidJadException(ije));        } catch (IOException ioe) {            System.out.println("** Error installing suite: " +                               ioe.getMessage());        } catch (ClassNotFoundException ex) {            System.out.println("MIDlet class(s) not found: " +                                ex.getMessage());        } catch (InstantiationException ex) {            System.out.println("MIDlet instance(s) could not be created: " +                                ex.getMessage());        } catch (IllegalAccessException ex) {            System.out.println("MIDlet class(s) could not be accessed: " +                                ex.getMessage());        } catch (IllegalArgumentException ex) {            System.out.println(ex.getMessage());        } catch (Throwable t) {            System.out.println("Exception caught in main:");            t.printStackTrace();        }        saveCommandState(state);        /*         * return any non-zero number so the native main can know that         * this is graceful exit and not the power button on the phone.         */        exitInternal(CommandProcessor.MAIN_EXIT);    }    /** Initialize the internal security for MIDP */    private static void initializeInternalSecurity() {        com.sun.midp.security.SecurityDomain internalSecurityDomain;        com.sun.midp.security.ImplicitlyTrustedClass trustedClass;        /*         * As the first caller to create a domain we can pass in null         * for the security domain.         */        internalSecurityDomain =            new com.sun.midp.security.SecurityDomain(null, null);        com.sun.midp.midletsuite.Installer.initSecurityDomain(            internalSecurityDomain);        com.sun.midp.dev.Manager.initSecurityDomain(            internalSecurityDomain);        com.sun.midp.rms.RecordStoreFile.initSecurityDomain(            internalSecurityDomain);        try {            trustedClass = (com.sun.midp.security.ImplicitlyTrustedClass)                 Class.forName("com.sun.midp.io.j2me.https.Protocol").                    newInstance();            trustedClass.initSecurityDomain(internalSecurityDomain);        } catch (ClassNotFoundException e) {            // HTTPS is optional for now        } catch (InstantiationException e) {            // HTTPS is optional for now        } catch (IllegalAccessException e) {            // HTTPS is optional for now        }    }    /**     * Runs a MIDlet that Manages installed MIDlet Suites.     *     * @param state command state to put the status and next command in     */    private static void manage(CommandState state) {        Installer installer;        DevMIDletSuiteImpl midletSuite;        String nextMidletSuiteToRun;        // we need to get the installer now, before the security level drops        installer = Installer.getInstaller();        // assume a class name of a MIDlet in the classpath        com.sun.midp.dev.Manager.initialize(state.logoDisplayed);        state.logoDisplayed = true;        try {            midletSuite =                new DevMIDletSuiteImpl(null, "com.sun.midp.dev.Manager",                                       "manager_storage_");            midletSuite.schedule();            // Check to see if we need to run a selected suite next            nextMidletSuiteToRun = installer.getNextMIDletSuiteToRun();            if (nextMidletSuiteToRun != null) {                state.nextCommand = CommandProcessor.RUN;                state.suiteStorageName = nextMidletSuiteToRun;                state.midletName = installer.getNextMIDletToRun();            }            state.status = CommandProcessor.OK;            return;        } catch (Throwable e) {            state.status = CommandProcessor.ERROR;            e.printStackTrace();        }    }    /**     * Lists the installed MIDlet Suites.     *     * @param state command state to put the status in     */    private static void list(CommandState state) {        Installer installer = Installer.getInstaller();        String[] appList;        MIDletSuite midletSuite;        String temp;        MIDletInfo midletInfo;                appList = installer.list();        if ((appList == null) || (appList.length == 0)) {            System.out.println("** No MIDlet Suites installed on phone");        } else {            for (int i = 0; i < appList.length; i++) {                midletSuite = installer.getMIDletSuite(appList[i]);                if (midletSuite == null) {                    System.out.println((i + 1) + ": suite corrupted");                    continue;                }                if (state.nextCommand == CommandProcessor.STORAGE_NAMES) {                    // just list the storage name, no number                    System.out.println(appList[i]);                    continue;                }                System.out.println("[" + (i + 1) + "]");                System.out.println("  Name: " +                     midletSuite.getProperty("MIDlet-Name"));                System.out.println("  Vendor: " +                     midletSuite.getProperty("MIDlet-Vendor"));                System.out.println("  Version: " +                     midletSuite.getProperty("MIDlet-Version"));                System.out.println("  Storage name: " + appList[i]);                System.out.println("  Size: " +                    ((midletSuite.getStorageUsed() + 1023) / 1024) + "K");                System.out.println("  Installed From: " +                     midletSuite.getJadUrl());                System.out.println("  MIDlets:");                for (int j = 1; ; j++) {                    temp = midletSuite.getProperty("MIDlet-" + j);                    if (temp == null) {                        break;                    }                    midletInfo = new MIDletInfo(temp);                    System.out.println("    " + midletInfo.name);                }            }        }        state.status = CommandProcessor.OK;    }    /**     * Run a given MIDlet subclass.     *     * @param state command state containing MIDlet's classname.     */    private static void runLocalClass(CommandState state) {        DevMIDletSuiteImpl midletSuite;        try {            // assume a class name of a MIDlet in the classpath            midletSuite = new DevMIDletSuiteImpl(state.descriptorName,                                                 state.midletClassName);            // if no class name was specified than repeat the selector            do {                midletSuite.schedule();            } while (state.midletClassName == null);        } catch (Throwable e) {            e.printStackTrace();        }    }    /**     * Returns the associated message for the given exception.     * This function is here instead of in the exception its self because     * it not need on devices, it needed only on development platforms that     * have command line interface.     * @param ije reason reason code for the the exception     * @return associated message for the given reason     */    private static String messageForInvalidJadException(                                                    InvalidJadException ije) {        switch (ije.getReason()) {        case InvalidJadException.MISSING_PROVIDER_CERT:        case InvalidJadException.MISSING_SUITE_NAME:        case InvalidJadException.MISSING_VENDOR:        case InvalidJadException.MISSING_VERSION:        case InvalidJadException.MISSING_JAR_URL:        case InvalidJadException.MISSING_JAR_SIZE:        case InvalidJadException.MISSING_JAR_DIGEST:        case InvalidJadException.MISSING_CONFIGURATION:        case InvalidJadException.MISSING_PROFILE:            return "A required attribute is missing";        case InvalidJadException.SUITE_NAME_MISMATCH:        case InvalidJadException.VERSION_MISMATCH:        case InvalidJadException.VENDOR_MISMATCH:            return "A required attribute in the JAR manifest " +                "do not match the one in the JAD";        case InvalidJadException.SIGNATURE_NOT_FIRST:        case InvalidJadException.CORRUPT_PROVIDER_CERT:        case InvalidJadException.UNKNOWN_DOMAIN_OWNER:        case InvalidJadException.INVALID_PROVIDER_CERT:        case InvalidJadException.CORRUPT_SIGNATURE:        case InvalidJadException.INVALID_SIGNATURE:        case InvalidJadException.UNSUPPORTED_CERT:        case InvalidJadException.EXPIRED_PROVIDER_CERT:        case InvalidJadException.EXPIRED_DOMAIN_OWNER_KEY:        case InvalidJadException.INVALID_VERSION:        case InvalidJadException.CORRUPT_JAR_DIGEST:        case InvalidJadException.INVALID_JAR_DIGEST:            return "Error authenticating the application suite";        case InvalidJadException.JAR_SIZE_MISMATCH:            return "The Jar downloaded was not the size in the JAD";        case InvalidJadException.OLD_VERSION:            return "The application is an older version of one that is " +                "already installed";        case InvalidJadException.NEW_VERSION:            return "The application is an newer version of one that is " +                "already installed";        case InvalidJadException.JAD_SERVER_NOT_FOUND:            return "JAD server not found";        case InvalidJadException.JAD_NOT_FOUND:            return "JAD not found";        case InvalidJadException.JAR_SERVER_NOT_FOUND:            return "JAR server not found";        case InvalidJadException.JAR_NOT_FOUND:            return "JAR not found";        case InvalidJadException.CORRUPT_JAR:            return "JAR is corrupt";        case InvalidJadException.INVALID_JAR_TYPE:            if (ije.getExtraData() != null) {                return "JAR did not have the correct media type, it had " +                   ije.getExtraData();            }            return "The server did not have a resource with an acceptable " +                "media type for the JAR URL. (code 406)";        case InvalidJadException.INVALID_JAD_TYPE:            if (ije.getExtraData() != null) {                return "JAD did not have the correct media type, it had " +                   ije.getExtraData();            }            return "The server did not have a resource with an acceptable " +                "media type for the JAD URL. (code 406)";        case InvalidJadException.INVALID_KEY:        case InvalidJadException.INVALID_VALUE:            return "A key or value is not in the proper format";        case InvalidJadException.INSUFFICIENT_STORAGE:            return "There is insuffient storage to install this suite";        case InvalidJadException.UNAUTHORIZED:            return "Authentication required or failed";        case InvalidJadException.JAD_MOVED:            return "The JAD to be installed is for an existing suite, " +                "but not from the same domain as the existing one.";        case InvalidJadException.CANNOT_AUTH:            return "Cannot authenticate with the server, unsupported scheme";        case InvalidJadException.DEVICE_INCOMPATIBLE:            return "Either the configuration or profile is not supported.";        }        return ije.getMessage();    }    /**     * Save the command state.     *     * @param state current command state     */    private static native void saveCommandState(CommandState state);    /**     * Restore the command state.     *     * @param state current command state     */    private static native void restoreCommandState(CommandState state);    /**     * Exit the VM with an error code. Our private version of Runtime.exit.     * <p>     * This is needed because the MIDP version of Runtime.exit cannot tell     * if it is being called from a MIDlet or not, so it always throws an     * exception.     * <p>     *     * @param status Status code to return.     */    private static native void exitInternal(int status);    /** This class is not meant to be instatiated */    private Main() {    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -