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,此错误是不会出现的。

原文地址:https://www.cnblogs.com/potoofly/p/3228296.html