How to disable all network connections in Java

后端 未结 3 422
萌比男神i
萌比男神i 2021-01-05 08:56

Is there a way to run Java VM (java.exe) on Windows Server 2008 and disable all network connections using a command line argument or a system variable?

3条回答
  •  佛祖请我去吃肉
    2021-01-05 09:40

    I had the same task to test offline installer for our product. All said above is almost right, but creating .policy file is not easy for the first time. Here is what I did:

    1. Crated generic policy file that has no permission to resolve host names (see code snippet below);

    2. Added -Djava.security.manager -Djava.security.policy=pathto/policy.file in jvm parameters;

    generic .policy file content:

    grant {
        permission java.io.FilePermission "<>", "read,write,execute,delete";
        permission java.util.PropertyPermission "*", "read,write";
        permission java.lang.RuntimePermission "*";
        permission java.net.NetPermission "*";
        permission java.lang.reflect.ReflectPermission "*";
    };
    

    If something tries to get content outside during the test, it fails with security exception.

提交回复
热议问题