java 遇到的问题_Java 遇到的问题

1、No enclosing instance of type MyThreadDemo is accessible. Must qualify the allocation with an enclosing instance of type MyThreadDemo (e.g. x.new A() where x is an instance of MyThreadDemo).

根据提示:不能使用类型为MyThreadDemo的封闭实例。 必须使用封闭的MyThreadDemo类型的实例来限定分配(例如,x.new A(),其中x是MyThreadDemo的一个实例)。

我已经 new 实例化了这个类,还会报错。

原来我写的内部类是动态的,也就是开头以public class开头。而主程序是public static class main。在Java定义中,类中的静态方法不能直接调用动态方法。只有将某个内部类修饰为静态类,然后才能够在静态类中调用该类的成员变量与成员方法。所以在不做其他变动的情况下,最简单的解决办法是将public class改为public static class.

public classMyThreadDemo {public static voidmain(String[] args) {//TODO Auto-generated method stub//创建线程对象//MyThread my=new MyThread();//启动线程//my.run();

MyThread my1=newMyThread();//这里会报错

//此处程序具体描述为什么回报这个错,代码无关重要也是为了提醒自己

}public class MyThread extendsThread{public voidrun(){//一般来说,被线程执行的代码肯定是比较耗时的,所以我们用循环该井

for(int x=0;x<200;x++){

System.out.println(x);

}

}

2、

Debug模式下报错:重定向输出的问题

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]

解决方法:

查找Java Doc 有这么一句话:

"Whether a virtual machine has a console is dependent upon the underlying platfor  m and also upon the manner in which the virtual machine is invoked. If the v irtual machine is started from an interactive command line without redirecting th e 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控制台......。

于是,思考一下我们运行上面程序的地点——Eclipse等集成开发环境,这对于上面描述中的把启动虚拟机的地方作为标准输出来说是不合适的,这里的Console()返回的默认情况下是Null,于是就产生了上面的出错信息。(也许未来Eclipse会有所改进吧)

怎么解决该问题呢?这里提供两条思路给大家(已经实际操作过,可以实现功能的正常运作)

1、使用命令行进行运行,编译可以使用集成开发环境。这样可以完成标准输出。

2、在程序中重定向标准输出到其他的设备或者方式(例如写到文本文件),这样也可以"比较不方便的"完成该功能。

3、在main函数结束时,添加system.exit(0);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值