1

我正在为与服务器的蓝牙通信创建简单的 midlet,但我无法让它在我的手机上工作,当我尝试在 Eclipse/Emulator 中运行它时一切正常,但在手机上我得到“无法创建 MIDlet 实例:java .lang.ClassNotFoundException"

我在某处看到此异常主要与路径相关,但我没有任何外部 jar 或多个包。

这是代码:

package j2meclient;

import java.io.OutputStream;
import javax.bluetooth.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

public class J2MEClientMidlet extends MIDlet implements CommandListener,
        Runnable {

    Display d;
    Command cmExit, cmConnect;
    Form f;
    Thread t;
    String connString;

    public J2MEClientMidlet() {
        f = new Form("Client");
        cmExit = new Command("Exit", Command.EXIT, 1);
        cmConnect = new Command("Connect", Command.ITEM, 2);

        f.addCommand(cmExit);
        f.addCommand(cmConnect);
        f.setCommandListener(this);
    }

    public void startApp() {
        if (d == null) {
            d = Display.getDisplay(this);
            d.setCurrent(f);
            t = new Thread(this);
        }
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void commandAction(Command c, Displayable d) {
        if (c == cmExit) {
            destroyApp(false);
            notifyDestroyed();
        }
        if (c == cmConnect) {
            t.start();
        }
    }

    public void run() {
        try {

            LocalDevice local = LocalDevice.getLocalDevice();
            DiscoveryAgent agent = local.getDiscoveryAgent();
            connString = agent.selectService(new UUID(
                    "86b4d249fb8844d6a756ec265dd1f6a3", false),
                    ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
        } catch (Exception e) {
        }

        if (connString != null) {

            try {

                StreamConnection conn = (StreamConnection) Connector
                        .open(connString);
                OutputStream out = conn.openOutputStream();
                Thread.sleep(2000);

                out.write("Hello, World".getBytes());
                out.close();
                conn.close();
                f.append("Message sent correctly");

            } catch (Exception ex) {
                f.append("IOException: ");
                f.append(ex.getMessage());
            }
        } else {
            f.append("Unable to locate service");
        }
        }
    }

杰德;

MIDlet-1: J2MEClient,,J2MEClient
MIDlet-Jar-Size: 2254
MIDlet-Jar-URL: BTClient.jar
MIDlet-Name: BTClient Midlet Suite
MIDlet-Vendor: Midlet Suite Vendor
MIDlet-Version: 1.0.0
MicroEdition-Configuration: CLDC-1.1
MicroEdition-Profile: MIDP-2.0

任何想法为什么我不能在手机上运行它?

4

1 回答 1

1

您的 midlet 类文件已命名J2MEClientMidlet,但在 jad 文件中它被定义为 J2MEClient。Midlet 类文件名必须与 jad 上的定义匹配才能从 jad 成功运行。

于 2013-01-01T07:29:56.467 回答