读书人

Eclipse编译 跟 CMD编译 java的区别

发布时间: 2013-08-25 10:49:56 作者: rapoo

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)


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


[解决办法]
我用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 !

[解决办法]
---------- 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也是这样?
[解决办法]
我估计是你的eclipse使用的虚拟机版本和命令行里面的不一致。
@Override这个注释好像是要到1.6以上的虚拟机才支持。
很可能是你的eclipse用了1.6版本的虚拟机,而你的控制台默认的还是1.5.
[解决办法]

引用:
Quote: 引用:

---------- 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也是这样?



谁先说话,看人品的 ...


不要误导别人,这个是个多线程的程序。线程执行次序是不固定的。
[解决办法]
好神奇。。不过你把private Object obj ;改成private final Object obj ;试试。。。

读书人网 >J2SE开发

热点推荐