请教:java.lang.ClassNotFoundException这是为什么?

请教:java.lang.ClassNotFoundException这是为什么?

以前没有接触过J2ME这一块,对于它现在我是完全陌生的。

首先,我说说我的操作步骤:

1、我下载了eclipsme插件(1.7.9)安装好了,下载了sun_java_wireless_toolkit-2_5_2-windows.exe安装好,本人的机器上安装有eclipse3.4。
2、在window/preference--->J2ME,设置好WTK Root,再设置好Device Management,接着设置好其它几个配置项。

3、编写一个MIDlet。如下:

package cn.chris.test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Image;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class TestMIDlet extends MIDlet implements CommandListener {

public TestMIDlet() {
}

private Display display=null;
private Form mainForm =null;
public static final Command exitCommand = new Command("退出",Command.OK,1);

protected  void destroyApp(boolean flag) throws MIDletStateChangeException{
    if(flag){
        display=null;
        mainForm=null;
    }
}

protected  void pauseApp() {

}
//startApp()方法是程序的入口,相当于Java应用程序中的main()方法,startApp()同时是MIDlet的生命周期方法
protected  void startApp() throws MIDletStateChangeException{
    if (display==null)
        display=Display.getDisplay(this);
    mainForm = new Form("Hello");
    String text="Hello World!";
    mainForm.append("\n"+text);
    //为mainForm添加Command,用于与用户交互
    mainForm.addCommand(exitCommand);
    mainForm.setCommandListener(this);
    //将mainForm设置为手机界面当前显示对象
    display.setCurrent(mainForm);
}

public void commandAction(Command cmd, Displayable displayable) {
    if (cmd==exitCommand){
        try {
            destroyApp(false);
        } catch (MIDletStateChangeException e) {
            e.printStackTrace();
        }
        notifyDestroyed();
    }
}

}

RUN,报错:

Running with storage root C:\Documents and Settings\xhb_86\j2mewtk\2.5.2\appdb\DefaultColorPhone
Running with locale: Chinese_People's Republic of China.936
Running in the maximum security domain
java.lang.ClassNotFoundException: cn/chris/test/TestMIDlet
at com.sun.midp.midlet.MIDletState.createMIDlet(+29)
at com.sun.midp.midlet.Scheduler.schedule(+52)
at com.sun.midp.main.Main.runLocalClass(+28)
at com.sun.midp.main.Main.main(+80)
Execution completed.
3334814 bytecodes executed
39 thread switches
1667 classes in the system (including system classes)
17248 dynamic objects allocated (514280 bytes)
1 garbage collections (0 bytes collected)

没有接触过j2me,但是从error log来看,是程序调用
C:\Documents and ettings\xhb_86\j2mewtk\2.5.2\appdb\DefaultColorPhone下的
cn/chris/test/TestMIDlet.class文件出错,找不到这个类

建议先检查下有没有C:\Documents and ettings\xhb_86\j2mewtk\2.5.2\appdb\DefaultColorPhone\cn\chris\test\TestMIDlet.class(肯定没有:))

没有的话,应该是那个eclipse插件可能需要设置一下编译目录及其它调用参数

把程序放到其它盘目录下

没有找到类文件啊,明显编译时出现问题了

可以试试看:
修改下类文件,然后保存,然后右键点击项目,选择刷新选项,然后重新运行项目.

ClassNotFound异常最近常出现在你的编译环境的jdk版本和运行环境的jdk版本不一致,又或者A类和B类的编译环境不一致,却相互调用的情况

没有发现你写的这个类,重新编译一下试试!

没有发现你写的这个类,重新编绎并保存

建议先使用eclipse编程.....
打开这个目录
C:\Documents and Settings\xhb_86\j2mewtk\2.5.2\appdb\DefaultColorPhone\cn\chris\test\
看看有没有二进制源码?

1.换个目录,不要放到带有空格的目录
2.全部重新编译、打包

类没有找到,估计是你用了没有存在的类,请看看是不是新的类,然后还在低版本的开发,一般都是版本不一致

C:\Documents and Settings\xhb_86\j2mewtk\2.5.2\appdb\DefaultColorPhone 在这个目录里找不一文件

在java当中,执行的所有的类都是ClassLoader加载的,所以如果说没找到类,就应该想到是不是ClassLoader没有在指定的位置load到应该有的类呢?由于ClassLoad的特有的树形继承结构,使得越底层的ClassLoad比上层的能够加载更多的类。所以,发现这种问题,首先看看是不是classPath设置错误,或者跟踪一下ClassLoadder看看是不是真的在线程执行的当前的ClassLoader当中没有加载到该class.

没有找到类文件