天天看點

eclipse編譯錯誤

ERROR: JDWP Unable to get JNI 1.2 environment, jvm->GetEnv() return code = -2

JDWP exit error AGENT_ERROR_NO_JNI_ENV(183):  [../../../src/share/back/util.c:820]

網上查了下

出錯原因:裝了JDK1.6.0

查閱Java Doc,發現其中有這麼一段話:  http://download.java.net/jdk6/docs/api/java/io/Console.html  "Whether a virtual machine has a console is dependent upon 

the underlying platform and also upon the manner in which 

the virtual machine is invoked. If the virtual machine is 

started from an interactive command line without redirecting

 the standard input and output streams then its console will 

exist and will typically be connected to the keyboard and 

display from which the virtual machine was launched. 

If the virtual machine is started automatically, 

for example by a background job scheduler, 

then it will typically not have a console.

" ">http://download.java.net/jdk6/docs/api/java/io/Con..."  翻譯一下:  虛拟機是否有一個控制台Console取決于所依賴的平台和虛拟機解析該方法的方式。如果虛拟機是從一個互動式的指令行中啟動的,而沒有重定向标準輸入和輸出流,那麼虛拟機會自動的連接配接到鍵盤作為标準輸入,并且把啟動虛拟機的地方作為标準輸出。如果虛拟機是自動啟動的,例如通過背景的一個任務計劃,那麼典型的情況就是沒有Console控制台......。 

解決方法:

1、使用指令行進行運作,編譯可以使用內建開發環境。這樣可以完成标準輸出。  2、在程式中重定向标準輸出到其他的裝置或者方式(例如寫到文本檔案),這樣也可以"比較不友善的"完成該功能。 這2種方法我沒試過,其實我對這個故障描述也不是很懂。但是我用過2種方法可以解決。

3:解除安裝JDK1.6.0,裝1.5.0或者以下版本。

4:在main函數後面加個System.exit(0); 這個問題就可以解決。但是我不知道為什麼加了這句話就可以解決了。我了解的這句話的意思是main程式退出時傳回0,經過我的試驗,發現其實你傳回多少都不影響,你可以退出時傳回1,2,3,都可以,參照網上的資料,Console()傳回的預設情況下是Null,于是就産生了上面的出錯資訊。那麼也就是說隻要控制台傳回的不是null,那麼就不會出錯了。應該是這樣了解吧,^_^

 補充:第4個方法是有問題的,在有些程式中,加了這句和不加的結果是有差別的。比如在下面的這個關于Thread的程式中

public class Example9_2 {

 public static void main(String[] args) {

  People teacher,student;

  ComputerSum sum = new ComputerSum();

  teacher = new People("老師",200,sum);

  student = new People("學生",200,sum);

  teacher.start();

  student.start();

 }

}

class ComputerSum

{

 int sum;

 public void setSum(int n)

 {

  sum=n;

 }

 public int getSum()

 {

  return sum;

 }

}

class People extends Thread

{

 int timeLength;     //線程休眠的時間長度

 ComputerSum sum;

 People(String s,int timeLength,ComputerSum sum)

 {

  setName(s);     //Thread類的方法setName()

  this.timeLength=timeLength;

  this.sum= sum;

 }

 public void run()

 {

  for(int i=1;i<=5;i++)

  {

   int m=sum.getSum();

   sum.setSum(m+1);

   System.out.println("我是"+getName()+",現在的和:"+sum.getSum());

   try{

    sleep(timeLength);

   }

   catch(InterruptedException e){}

  }

 }

}

 如果不加System.exit(0);那麼結果沒有什麼問題。但是如果加上這句話,結果就變成了

我是老師,現在的和:1

我是學生,現在的和:2

也就是說老師和學生的線程都隻分别執行了一次就退出了。是以解決此編譯錯誤用此方法是不大可行的。

由于此debug錯誤不影響編譯,是以還是沒什麼太大問題。而且如果不debug直接點run,此錯誤是不會出現的。