- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
对于一个实验项目,我试图在启用安全管理器的情况下使用 URLClassLoader 加载一个类。尽管 URL 和类确实存在,但代码总是失败并出现 ClassNotFoundException。
至少在以下版本的 java 中观察到此行为(在我的系统上可用,MacOSX Mojave 10.14.6;尚未在 Windows 或 Linux 或其他 Java 实现上测试):
Java 版本“1.8.0_181”Java(TM) SE 运行时环境(build 1.8.0_181-b13)Java HotSpot(TM) 64 位服务器 VM(构建 25.181-b13,混合模式)
Java 版本 "10.0.2"2018-07-17Java(TM) SE 运行时环境 18.3(内部版本 10.0.2+13)Java HotSpot(TM) 64 位服务器 VM 18.3(构建 10.0.2+13,混合模式)
Java 版本 "12.0.1"2019-04-16Java(TM) SE 运行时环境(build 12.0.1+12)Java HotSpot(TM) 64 位服务器 VM(构建 12.0.1+12,混合模式,共享)
经过数小时的故障排除(检查路径、寻找路径中的不可见字符、阅读类加载器的文档等)后,我意识到只有在启用安全管理器时才会发生异常。当我禁用它时,发现该类没有问题。我本以为会出现某种安全异常,但不是 ClassNotFoundException。
显然,安全管理器的缺失严重限制了类加载器的实用性(特别是如果想要加载不受信任的、签名的类和 jar)。创建类加载器的权限显然是在安全策略中授予的。
下面的代码是重现问题所需的最少代码。
我对 Java 比较陌生。我可能正在做一些愚蠢的事情。以前有人观察过这种行为吗?是故意的吗?我错过了什么吗?
感谢您的帮助。
.
├── ClassLoaderTest.class
├── ClassLoaderTest.java
├── Makefile
├── security.policy
└── untrusted
├── Makefile
├── Untrusted.class
└── Untrusted.java
1 directory, 7 files
// ClassLoaderTest.java
import java.net.URLClassLoader;
import java.net.URL;
public class ClassLoaderTest {
public static void main(String[] args) throws Exception {
URL[] urls = new URL[1];
urls[0] = new URL("file:./untrusted/"); // untrusted/ could be anywhere; for the purpose of the test, it is in the working directory
URLClassLoader loader = new URLClassLoader(urls);
Class<?> cl = loader.loadClass("Untrusted"); // one tries to load: untrusted/Untrusted.class
cl.getConstructor().newInstance(); // or cl.getDeclaredConstructor().newInstance()
System.exit(0);
}
}
public class Untrusted {
public Untrusted() {
System.out.println("Instantiation of Untrusted");
}
public static void main(String args[]) {
Untrusted u = new Untrusted();
}
}
// security.policy
// permissions granted to any code (no matter where it came from or whether it is signed)
// THIS IS OBVIOUSLY FOR TESTING ONLY, only trusted sources should be granted a createClassLoader permission
grant {
permission java.lang.RuntimePermission "createClassLoader";
};
这是程序的结果:
没有安全管理器:工作
$ java ClassLoaderTest
Instantiation of Untrusted
启用安全管理器:失败!
$ java -Djava.security.manager -Djava.security.policy=security.policy ClassLoaderTest
Exception in thread "main" java.lang.ClassNotFoundException: Untrusted
at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:436)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:588)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
at ClassLoaderTest.main(ClassLoaderTest.java:10)
最佳答案
将问题提交给 Oracle 后,由于以下非常有用的标记(对于与安全管理器一起工作的人),他们解决了问题:-Djava.security.debug=access
https://bugs.java.com/bugdatabase/view_bug.do?bug_id=JDK-8229532
这样问题就清楚多了:
// ... more lines above (skipped for clarity)
access: access denied ("java.util.PropertyPermission" "user.dir" "read") // shows up only in debug mode
Exception in thread "main" java.lang.ClassNotFoundException: Untrusted
...
因此必须向 security.policy 文件添加权限:
grant {
permission java.lang.RuntimePermission "createClassLoader";
permission java.util.PropertyPermission "user.dir", "read"; // new line
};
它解决了这个问题。
因此,该问题被标记为不存在,并由 Oracle 的受让人关闭。虽然他的回复非常有帮助,但我仍然认为存在错误,因为 SDK(而非应用程序)抛出了错误的异常:ClassNotFoundException 而不是权限或安全异常。
对于不知道 -Djava.security.debug=access 属性的人(以及生产上下文中的用户或管理员),这可能会产生误导并且非常困惑。
无论如何,我的问题已经关闭,我希望这个解决方案对遇到类似问题的其他人有所帮助。
向大家致以最诚挚的问候。
关于java - 启用安全管理器时 URLClassLoader 发出的 ClassNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57433655/
我的应用在尝试访问外部依赖项时遇到 NoClassDefFoundError,但仅限于作为 jar 运行时。 使用 Intellij,我有一个带有主类的简单应用程序,其中包含一些对外部依赖项(例如 s
我收到以下异常: java.lang.ClassNotFoundException: org.jboss.resteasy.plugins.server.servlet.ResteasyBootstr
我正在尝试使用eclipse和tomcat调试solr4.6源代码。我收到错误消息:HTTP 状态 500 - {msg=SolrCore 'collection1' 由于初始化失败而不可用:[sch
我是新来的,很抱歉我的英语不好;)。 我尝试使用以下代码在 java 中加载插件 jar: package testprogramm; import java.io.File; import java
我重新安装了 ADT Bundle,因为我在更新 eclipse 后遇到了一些问题。 现在,当我想测试我的应用程序时,出现以下异常: 06-05 10:33:35.770: E/AndroidRunt
我浏览过现有的帖子,这些帖子处理在 Eclipse 中的 Tomcat 下运行 Java Web 应用程序的 ClassNotFoundException。 我无法提供源代码和配置 Artifact
我忙于我的应用一个多星期,突然: 11-12 07:59:17.860 1653-1653/nl.test.myapp E/AndroidRuntime﹕ FATAL EXCEPTION: ma
我的应用程序有问题,昨天一切正常,但今天我更新了 sdk,现在当我尝试运行我的应用程序时,出现这样的错误 05-21 00:14:19.285: W/dalvikvm(7061): Unable t
我正在阅读 JPA docs在 Spring ,我正在尝试重组我的代码。 我现在所拥有的: BrewerRepository @Repository public class BrewerReposi
我想像 BalusC's example 一样实现 fileUpload . 不幸的是,我已经在努力声明 servlet。 Extensions Filter javax.faces
在某些机器上运行这个小程序不起作用,但在其他机器上它工作得很好。在所有情况下看起来都安装了 JRE 1.6.0_26。 var attributes = {codebase:'http://kas
我将跟随这个非常简单的教程(http://www.tutorialspoint.com/hadoop/hadoop_mapreduce.htm)一起学习,直到我尝试运行Java文件为止,一切运行良好。
我看到了帖子,并按照流程进行了操作。但这没有用。 ClassNotFoundException, while running example job of Hadoop 请帮助我。 创建的 mapre
我尝试在cloudera quickstart vm上为hadoop运行Mahout Kmeans示例。我在这里读link to clouudera block和这里stack overflow po
我有一个mapreduce程序,其中我使用Hcatalog从Hive表'A'中获取带有HcatInputFormat的详细信息,对其进行处理,然后使用HcatOutput格式将其写回到Hive表'B'
我是激发应用程序编程的新手,因此在这里为这个基本的编程而苦苦挣扎。 我有 scala ide 并附加了来自最新 hadoop 和 spark 发行版的相关 jar 文件。我正在使用的只有一个基本的 s
我正在尝试在本地模式下运行Spark示例,但是正在获取以下堆栈跟踪: Exception in thread "main" java.lang.NoClassDefFoundError: org/ap
我有以下代码: // Test TODO remove try { System.out.println(System.getProperties().getPrope
我有以下文件: src/my_proj/myns.clj: (ns my-proj.myns) (defrecord MyRecord [a b c]) 测试/my_proj/myns_test.c
我试图让应用程序动态加载某些类,然后调用启动方法,但问题是,由于 ClassLoader 不同,一个类无法调用另一个类的方法,但是正如我已经用 google 搜索的那样,我用父类创建了两个类加载器。这
我是一名优秀的程序员,十分优秀!