Eclipse编译 跟 CMD编译 java的区别

   阅读
Eclipse编译 和 CMD编译 java的区别
我写了这样一段程序,请看:

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 mark(){ 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 mark(){ 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("mark")){
methods[i].invoke(obj);
}
}
}
}
}


这段代码写完后,我用cmd编译:
D:\>javac Run.java

D:\>java Run

运行结果:
java.lang.NullPointerException
        at ORun.doSomething(Run.java:41)
        at ORun.run(Run.java:36)
阅读