1

我有一个包含 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?

4

1 回答 1

0

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

  1. 首先,您必须MANIFEST.MF向您的小程序添加一个文件。检查:在 Maven 中将属性添加到 jar Manifest的最简单方法,用于签署清单文件并将其添加到 applet。确保在小程序参数配置中没有覆盖此配置

  2. Java 8+ 需要签名的小程序,因此您必须对您的小程序进行签名。如果您在开发环境中,则必须对您的小程序进行自签名并将此证书添加到 Java 控制面板。有关将自签名证书添加到 Java 运行时中的受信任证书列表的方法,请参阅此内容。

  3. 即使在那之后,也有一些代码只能使用PrivilegedActions. 请参阅此答案:https ://stackoverflow.com/a/1730904/2692914 。

无论如何,我就是这样做的,我使用了这个用 Maven 构建的 Minimal Java Applet 作为基础项目

清单文件

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>

最小的.cer

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

将证书添加到 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;
}

嗯,就是这样!

于 2014-12-03T04:43:01.470 回答