[英]java.security.AccessControlException: access denied (“java.lang.RuntimePermission” “loadLibrary.jpcap”)
I have an application for my school project which has jpcap classes and I get these exceptions when trying to run the jar file: 我的学校项目有一个jpcap类的应用程序,尝试运行jar文件时遇到以下异常:
java.lang.ExceptionInInitializerError
...
Caused by: java.security.AccessControlException: access denied ("java.lang.RuntimePermission" "loadLibrary.jpcap")
at java.security.AccessControlContext.checkPermission(Unknown Source)
at java.security.AccessController.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkPermission(Unknown Source)
at sun.plugin2.applet.FXAppletSecurityManager.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkLink(Unknown Source)
at java.lang.Runtime.loadLibrary0(Unknown Source)
at java.lang.System.loadLibrary(Unknown Source)
at jpcap.JpcapCaptor.<clinit>(JpcapCaptor.java:251)
... 12 more
Is there any way to permit my code without adding: 有什么办法可以允许我的代码而无需添加:
grant { permission java.security.AllPermission; };
to jre location\\lib\\security\\java.policy? 到jre location \\ lib \\ security \\ java.policy?
For all of you who have this same problem: 对于所有有同样问题的人:
First, you will have to add a MANIFEST.MF
file to your applet. 首先,您必须将
MANIFEST.MF
文件添加到小程序。 Check: Simpliest way to add an attribute to a jar Manifest in Maven for a way for signing and adding the manifest file to your applet. 检查: 向Maven的jar清单中添加属性的最简单方法,用于对清单文件进行签名并将其添加到applet的方法。 Make sure that this configuration isn't override in the applet parameters configurations .
确保在applet参数配置中未覆盖此配置 。
Java 8+ requires signed applets, so you will have to sign your applet. Java 8+需要签名的小程序,因此您必须对小程序进行签名。 If you are in a development environment you will have to self-sign your applet and add this certificate to the Java Control Panel.
如果您在开发环境中,则必须对小程序进行自签名并将此证书添加到Java控制面板中。 See this for a way to add Self-Signed Certificates to the List of Trusted Certificates in the Java Runtime .
有关在Java Runtime中将自签名证书添加到受信任证书列表的方法,请参见此内容 。
Even after that, there is some code that will run only with PrivilegedActions
. 即使在那之后,仍有一些代码只能与
PrivilegedActions
一起运行。 See this answer: https://stackoverflow.com/a/1730904/2692914 . 查看此答案: https : //stackoverflow.com/a/1730904/2692914 。
Anyway, this is how I have done this, I've used this Minimal Java Applet built with Maven as a base project . 无论如何,这就是我这样做的方式,我已经使用了以Maven构建的Minimal Java Applet作为基础项目 。
MANIFEST.MF MANIFEST.MF
Manifest-Version: 1.0
Application-Name: One Applet
Codebase: *
Permissions: all-permissions
Application-Library-Allowable-Codebase: *
Caller-Allowable-Codebase: *
Trusted-Only: false
Trusted-Library: false
pom.xml 的pom.xml
<build>
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<index>true</index>
<manifestFile>${project.basedir}/MANIFEST.MF</manifestFile>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>attached</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jarsigner-plugin</artifactId>
<version>1.2</version>
<configuration>
<keystore>src/main/resources/minimal-keystore.jks</keystore>
<alias>minimal</alias>
<storepass>abcd1234</storepass>
<keypass>abcd1234</keypass>
</configuration>
<executions>
<execution>
<id>sign</id>
<goals>
<goal>sign</goal>
</goals>
</execution>
<execution>
<id>verify</id>
<goals>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
...
</plugins>
</build>
minimal.cer minimal.cer
keytool -export -keystore minimal-keystore.jks -alias minimal -file minimal.cer
SomeClass.java SomeClass.java
try {
AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
try {
for (String dll : dlls) {
String dllPath = basePath + dll;
System.out.println("Cargando: " + dllPath);
System.load(dllPath);
}
return null;
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
});
} catch (Exception e) {
e.printStackTrace();
throw e;
}
Well, that's it! 好吧,就是这样!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.