Eclipse编译 和 CMD编译 java的区别

weidu23 2013-08-19 09:34:08
我写了这样一段程序,请看:

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)
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下编译的结果不一致 ???

...全文
276 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
guangliang05 2013-08-20
  • 打赏
  • 举报
回复
---------- 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也是这样?
weidu23 2013-08-20
  • 打赏
  • 举报
回复
引用 5 楼 Neil_Zhao 的回复:
不要误导别人,这个是个多线程的程序。线程执行次序是不固定的。
兄弟误会我了 !!! CPU不就是人品么,自然 ... 谁先运行,看人品咯 ... 我是个初学者,有很多不懂的地方,不知兄弟可否指教:http://bbs.csdn.net/topics/390556867
lcf 2013-08-20
  • 打赏
  • 举报
回复
好神奇。。不过你把private Object obj ;改成private final Object obj ;试试。。。
Neil_Zhao 2013-08-20
  • 打赏
  • 举报
回复
引用 3 楼 weidu23 的回复:
[quote=引用 2 楼 guangliang05 的回复:] ---------- 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也是这样?
谁先说话,看人品的 ... [/quote] 不要误导别人,这个是个多线程的程序。线程执行次序是不固定的。
Neil_Zhao 2013-08-20
  • 打赏
  • 举报
回复
我估计是你的eclipse使用的虚拟机版本和命令行里面的不一致。 @Override这个注释好像是要到1.6以上的虚拟机才支持。 很可能是你的eclipse用了1.6版本的虚拟机,而你的控制台默认的还是1.5.
weidu23 2013-08-20
  • 打赏
  • 举报
回复
引用 2 楼 guangliang05 的回复:
---------- 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也是这样?
谁先说话,看人品的 ...
nmyangym 2013-08-19
  • 打赏
  • 举报
回复
我用jdk1.6.0_01,可以的。 D:\myjava\csdn>javac Run.java D:\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 !

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧