[英]Subclassing sun.* class in same package gives IllegalAccessError
Foreword: 前言:
I'm trying to sub-class sun.nio.ch.SourceChannelImpl
which is package private class with package private constructor present in JDK (in rt.jar) so I have to create it in sun.nio.ch
package. 我正在尝试子类
sun.nio.ch.SourceChannelImpl
子类,它是包私有类,包含JDK中的包私有构造函数(在rt.jar中)所以我必须在sun.nio.ch
包中创建它。
Here is my sub-class: 这是我的子类:
package sun.nio.ch;
import java.io.FileDescriptor;
import java.nio.channels.spi.SelectorProvider;
class MySourceChannel extends SourceChannelImpl {
public MySourceChannel(SelectorProvider sp, FileDescriptor fd) {
super(sp, fd);
}
}
Here is my simple test: 这是我的简单测试:
package sun.nio.ch;
import java.io.FileDescriptor;
public class Main {
public static void main(String[] args) {
new MySourceChannel(null, FileDescriptor.in);
}
}
And here's the failure: 而这是失败:
Exception in thread "main" java.lang.IllegalAccessError: class sun.nio.ch.MySourceChannel cannot access its superclass sun.nio.ch.SourceChannelImpl
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at sun.nio.ch.Main.main(Main.java:5)
It's probably not you can't define class in JDK package XYZ ((java|sun).*) type of problem because otherwise I'd get 可能不是你不能在JDK包中定义类XYZ((java | sun)。*)类型的问题因为否则我会得到
java.lang.SecurityException: Prohibited package name: XYZ
Main
class works fine in this package. Main
类在此包中正常工作。
I've also tried to disable security checks by setting Policy
allowing everything and that didn't help neither. 我也尝试通过设置
Policy
允许一切来禁用安全检查,这也没有帮助。 I've also tried System.setSecurityManager(null);
我也试过
System.setSecurityManager(null);
(I'm not sure if this actually disables it) and it didn't help neither. (我不确定这是否真的禁用它)并且它也没有帮助。
What's the problem? 有什么问题? How can I fix it please?
我该怎么办呢?
I've tried it with JDK 1.7.0_45, both Oracle and OpenJDK. 我已经尝试过使用JDK 1.7.0_45,Oracle和OpenJDK。
SourceChannelImpl
is a "package private" class. SourceChannelImpl
是一个“包私有”类。 In the JVM a package is always loaded by a single class loader. 在JVM中,包总是由单个类加载器加载。 If you have two packages with the same name loaded by different class loaders, they are not the same package.
如果有两个具有相同名称的包由不同的类加载器加载,则它们不是同一个包。
You can fix this by loading some or all of your code in the bootstrap class loader with -Xbootclasspath/a:mybootspath
. 您可以通过使用
-Xbootclasspath/a:mybootspath
在引导程序类加载器中加载部分或全部代码来解决此-Xbootclasspath/a:mybootspath
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.