【问题标题】:java.security.AccessControlException: access denied ("java.lang.RuntimePermission" "loadLibrary.jpcap")java.security.AccessControlException:访问被拒绝(“java.lang.RuntimePermission”“loadLibrary.jpcap”)
【发布时间】:2014-12-26 11:12:30
【问题描述】:

我的学校项目有一个应用程序,其中包含 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

有什么方法可以让我的代码不加:

grant { permission java.security.AllPermission; };

到 jre 位置\lib\security\java.policy?

【问题讨论】:

    标签: java javafx-2 jpcap accesscontrolexception


    【解决方案1】:

    对于所有遇到同样问题的人:

    1. 首先,您必须将MANIFEST.MF 文件添加到您的小程序中。检查:Simpliest way to add an attribute to a jar Manifest in Maven 以获取签署清单文件并将其添加到小程序的方法。 确保此配置不会在小程序参数配置中被覆盖

    2. Java 8+ 需要已签名的小程序,因此您必须对您的小程序进行签名。如果您在开发环境中,则必须对您的小程序进行自签名并将此证书添加到 Java 控制面板。看到这个a way to add Self-Signed Certificates to the List of Trusted Certificates in the Java Runtime

    3. 即使在此之后,仍有一些代码只能使用 PrivilegedActions 运行。看到这个答案:https://stackoverflow.com/a/1730904/2692914

    不管怎样,我就是这样做的,我用过这个Minimal Java Applet built with Maven as a base project

    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

    <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

    keytool -export -keystore minimal-keystore.jks -alias minimal -file minimal.cer
    

    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;
    }
    

    嗯,就是这样!

    【讨论】:

      猜你喜欢
      • 2011-09-04
      • 2013-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-04
      • 2016-04-06
      • 2014-10-24
      相关资源
      最近更新 更多