[英]Java Shipping JAR with SecurityManager Policy File
I'm trying to ship a Java application that performs some RMI calls. 我正在尝试运送执行一些RMI调用的Java应用程序。
I need to ship this as a JAR file (It's a requirement, no way around it). 我需要将此文件作为JAR文件发送(这是必要条件,无法解决)。
Now, to allow certain thing (like sockets and RMI connections) I need a SecurityManager and a Policy file. 现在,为了允许某些事情(例如套接字和RMI连接),我需要一个SecurityManager和一个Policy文件。
I'd like to ship this policy file inside the jar and set the policy path from inside the JAR. 我想将此策略文件运送到jar中,并从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: 当我将路径更改为PC上的路径并在没有JAR(IntelliJ“应用程序”)的情况下运行代码时,启动我的应用程序没有问题,当我尝试运行JAR时,出现以下异常:
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? 我感觉到我设置的路径错误,有人可以告诉我这条路径应该是什么吗?
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. 我没有尝试手动找出路径,而是让Java为我修复了它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.