2

I have an application for my school project which has jpcap classes and I get these exceptions when trying to run the jar file:

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?

joeNio
  • 21
  • 1
  • 5

1 Answers1

0

For all of you who have this same problem:

  1. First, you will have to add a MANIFEST.MF file to your applet. 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. Make sure that this configuration isn't override in the applet parameters configurations.

  2. Java 8+ requires signed applets, so you will have to sign your applet. If you are in a development environment you will have to self-sign your applet and add this certificate to the Java Control Panel. See this for a way to add Self-Signed Certificates to the List of Trusted Certificates in the Java Runtime .

  3. Even after that, there is some code that will run only with PrivilegedActions. See this answer: 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.

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

Adding cert to the Java Control Panel

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!

Community
  • 1
  • 1
lmiguelmh
  • 3,074
  • 1
  • 37
  • 53