我正在尝试学习JINI库,并找到了简单程序的例子。程序在这里:
package book.basic;
import net.jini.core.discovery.LookupLocator;
import net.jini.core.lookup.ServiceRegistrar;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
/**
* UnicastRegistrar.java
*/
public class UnicastRegister {
static public void main(String argv[]) throws RemoteException {
new UnicastRegister();
}
public UnicastRegister() throws RemoteException {
LookupLocator lookup = null;
ServiceRegistrar registrar = null;
System.setSecurityManager(new RMISecurityManager());
try {
lookup = new LookupLocator("jini://localhost");
} catch(java.net.MalformedURLException e) {
System.err.println("Lookup failed: " + e.toString());
System.exit(1);
}
try {
registrar = lookup.getRegistrar();
} catch (java.io.IOException e) {
System.err.println("Registrar search failed: " + e.toString());
System.exit(1);
} catch (java.lang.ClassNotFoundException e) {
System.err.println("Registrar search failed: " + e.toString());
System.exit(1);
}
System.out.println("Registrar found");
// the code takes separate routes from here for client or service
}
} // UnicastRegister
如果我运行此应用程序,则抛出异常:
线程中的异常" main" java.security.AccessControlException: 访问被拒绝(" java.net.SocketPermission"" 127.0.0.1:4160" "连接,解决")at java.security.AccessControlContext.checkPermission(AccessControlContext.java:372) 在 java.security.AccessController.checkPermission(AccessController.java:559) 在 java.lang.SecurityManager.checkPermission(SecurityManager.java:549) 在java.lang.SecurityManager.checkConnect(SecurityManager.java:1051) 在java.net.Socket.connect(Socket.java:574)at java.net.Socket.connect(Socket.java:528)at java.net.Socket。(Socket.java:425)at java.net.Socket。(Socket.java:241)at net.jini.core.discovery.LookupLocator.getRegistrar(LookupLocator.java:328) 在 net.jini.core.discovery.LookupLocator.getRegistrar(LookupLocator.java:286) 在book.basic.UnicastRegister。(UnicastRegister.java:26)at book.basic.UnicastRegister.main(UnicastRegister.java:12)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在java.lang.reflect.Method.invoke(Method.java:606)at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
我创建了策略文件" policy.all"并把它放在主目录中:
此文件包含文字:
grant {
permission java.security.AllPermission;
};
接下来,我在命令行中将此文件作为参数传递: -Djava.security.manager -Djava.security.policy = / policy.all, 更像是-Djava.security.manager -Djava.security.policy =。/ policy.all 我在想法中这样做了:
我得到下一个编译错误:
如果我想直接设置策略文件:
System.setProperty("java.security.policy","file:/policy.all");
System.setSecurityManager(new RMISecurityManager());
它没有产生任何结果,我再次得到java.security.AccessControlException。