MyEclipse解决不推荐和禁止类问题

  1.Unsafe类利用反射机制突破私有构造函数无法实例化,其中Player的构造函数使用private修饰符。利用unsafe类的allocateInstance()方法可以进行实例化。

  2.在MyEclipse运行上述代码会出现如下错误。

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Access restriction: The type Unsafe is not accessible due to restriction on required library D:\Program Files\MyEclipse\Common\binary\com.sun.java.jdk.win32.x86_1.6.0.013\jre\lib\rt.jar
Unhandled exception type NoSuchFieldException
Access restriction: The type Unsafe is not accessible due to restriction on required library D:\Program Files\MyEclipse\Common\binary\com.sun.java.jdk.win32.x86_1.6.0.013\jre\lib\rt.jar
Access restriction: The type Unsafe is not accessible due to restriction on required library D:\Program Files\MyEclipse\Common\binary\com.sun.java.jdk.win32.x86_1.6.0.013\jre\lib\rt.jar
Unhandled exception type IllegalAccessException
Access restriction: The method allocateInstance(Class) from the type Unsafe is not accessible due to restriction on required library D:\Program Files\MyEclipse\Common\binary\com.sun.java.jdk.win32.x86_1.6.0.013\jre\lib\rt.jar
Unhandled exception type InstantiationException

at MyTest.main(MyTest.java:5)

  3.修改compiler属性,如图所示将Error更改为Warning。如下图所示

  4.修改JRE。MyEclipse自带的JRE无法引入sun.misc.Unsafe,需要引入自己安装的J2SE版本,如图所示:

 

posted @ 2015-03-03 14:18  √珞珈搬砖工√  阅读(175)  评论(0)    收藏  举报