我写了这样一段程序,请看:import java.lang.reflect.*;
public class Run{
public static void main(String[]args){
Lily lily = new Lily();
Lucy lucy = new Lucy();
ORun oLily = new ORun(lily,true);
ORun oLucy = new ORun(lucy,false);
new Thread(oLily,oLily.getObjName()).start();
new Thread(oLucy,oLucy.getObjName()).start();
}
}class Lily{
public void say(){ System.out.println("Lily say:If you can give me your book , i can give your my picture !"); }
public void (){ System.out.println("Lily say:the book is mine but now is your ! "); }
}class Lucy{
public void say(){ System.out.println("Lucy say:If you can give me your picture , i can give book!"); }
public void (){ System.out.println("Lucy say:the picture is mine but now is your ! "); }
}class ORun implements Runnable{
private int ticket = 10 ;
private Object obj ;
private boolean flag ;
public ORun(Object obj, boolean flag){
this.flag = flag ;
this.obj = obj ;
objName = obj.getClass().getSimpleName();
}
private String objName ;
public String getObjName(){ return objName ; }
@Override
public void run(){
try{
doSomething();
}
catch(Exception err){err.printStackTrace();}
}
public synchronized void doSomething() throws Exception{
Class cla = obj.getClass();
Method[] methods = cla.getDeclaredMethods();
if(flag){
for(int i=0 ; i<methods.length ; i++){
if(methods[i].getName().equals("say")){
methods[i].invoke(obj);
}
}
}else{
for(int i=0 ; i<methods.length ; i++){
if(methods[i].getName().equals("")){
methods[i].invoke(obj);
}
}
}
}
}这段代码写完后,我用cmd编译:
D:\>javac Run.javaD:\>java Run运行结果:
java.lang.NullPointerException
        at ORun.doSomething(Run.java:41)
        at ORun.run(Run.java:36)
        at java.lang.Thread.run(Unknown Source)
java.lang.NullPointerException
        at ORun.doSomething(Run.java:41)
        at ORun.run(Run.java:36)
        at java.lang.Thread.run(Unknown Source)
-----------------------检查了很多遍,我检测不出问题,于是,我用Eclipse编译试试,
结果:
Lily say:If you can give me your book , i can give your my picture !
Lucy say:the picture is mine but now is your ! 正常输出!!!---------------------------为什么同一个代码,在cmd下和Eclipse下编译的结果不一致 ???

解决方案 »

  1.   

    我用jdk1.6.0_01,可以的。D:\myjava\csdn>javac Run.javaD:\myjava\csdn>java Run
    Lily say:If you can give me your book , i can give your my picture !
    Lucy say:the picture is mine but now is your !
      

  2.   

    ---------- java ----------
    Lucy say:the picture is mine but now is your ! 
    Lily say:If you can give me your book , i can give your my picture !输出完成 (耗时 0 秒) - 正常终止为什么我用Editplus 是Lucy 先说话,lily后说话?用CMD也是这样?
      

  3.   

    我估计是你的eclipse使用的虚拟机版本和命令行里面的不一致。
    @Override这个注释好像是要到1.6以上的虚拟机才支持。
    很可能是你的eclipse用了1.6版本的虚拟机,而你的控制台默认的还是1.5.
      

  4.   

    好神奇。。不过你把private Object obj ;改成private final Object obj ;试试
      

  5.   

    兄弟误会我了 !!! 
    CPU不就是人品么,自然 ... 谁先运行,看人品咯 ... 我是个初学者,有很多不懂的地方,不知兄弟可否指教:http://bbs.csdn.net/topics/390556867