shmoo6000
shmoo6000

Reputation: 495

Java Shipping JAR with SecurityManager Policy File

I'm trying to ship a Java application that performs some RMI calls.

I need to ship this as a JAR file (It's a requirement, no way around it).

Now, to allow certain thing (like sockets and RMI connections) I need a SecurityManager and a Policy file.

I'd like to ship this policy file inside the jar and set the policy path from inside the JAR.

Right now, this is what my code looks like:

public static void main(String[] args)
{
        System.setProperty("java.security.policy","jar:file:/Policies/Server.policy");
        Policy.getPolicy().refresh();

        ... /* All other code */

}

When I change the path to be a path on my PC and run the code without the JAR (An IntelliJ 'Application') I have no problems starting my application, When I try to run my JAR, I get the following exception:

Exception in thread "main" java.security.AccessControlException: access denied ("java.net.SocketPermission" "localhost:2000" "listen,resolve")

I've got a feeling the path I'm setting is wrong, can anyone please tell me what this path should be?

Upvotes: 0

Views: 410

Answers (1)

shmoo6000
shmoo6000

Reputation: 495

I was able to fix this, by changing my code to the following:

public static void main(String[] args)
{
    boolean quit = false;

    String serverPolicyPath = "/Policies/Server.policy";
    URL serverPolicyURL = Main.class.getResource(serverPolicyPath);

    if (serverPolicyURL == null)
    {
        System.err.println("getResource returned NULL");
        return;
    }

    System.setProperty("java.security.policy",serverPolicyURL.toString());
    Policy.getPolicy().refresh();

    ...

Instead of trying to figure out the path manually, I just let Java fix it for me.

Upvotes: 1

Related Questions