当前位置: 技术问答>java相关
rmi编程问题,SOS
来源: 互联网 发布时间:2015-01-09
本文导语: 操作系统:WINDOWS ME 操作流程:JAVAC 接口.java JAVAC RemoteObject.java RMIC RemoteObject start rmiregistry java RemoteObject 服务器源程序如下: import java.rmi.Nam...
操作系统:WINDOWS ME
操作流程:JAVAC 接口.java
JAVAC RemoteObject.java
RMIC RemoteObject
start rmiregistry
java RemoteObject
服务器源程序如下:
import java.rmi.Naming;
import java.rmi.server.UnicastRemoteObject;
import java.rmi.RemoteException;
import java.rmi.RMISecurityManager;
public class RemoteObject extends UnicastRemoteObject implements RemoteInterface
{
String name;
public RemoteObject(String name) throws RemoteException{
super();
this.name=name;
}
public String message(String message) throws RemoteException
{
String returnString = "My Name is:"+name+",thanks for your message:"+message;
System.out.println("Returning:"+returnString);
return "My Name is:"+name+",thanks for your message:"+message;
}
public static void main (String args[])
{
System.setSecurityManager (new RMISecurityManager());
try{
String myName = "ServerTest";
RemoteObject theServer = new RemoteObject (myname);
Naming.rebind(myName,theServer);
System.out.println("Ready to continue");
} catch (Exception e){
System.out.println("An Exception occured while creating server");
e.printStackTrace();
} }}
错误提示
An Exception occured while creating server
java.security.AccessControlException: access denied (java.net.SocketPermission 1
27.0.0.1:1099 connect,resolve)
at java.security.AccessControlContext.checkPermission(Unknown Source)
at java.security.AccessController.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkConnect(Unknown Source)
at java.net.Socket.(Unknown Source)
at java.net.Socket.(Unknown Source)
at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(Unknown S
ource)
at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(Unknown S
ource)
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(Unknown Source)
at sun.rmi.transport.tcp.TCPChannel.createConnection(Unknown Source)
at sun.rmi.transport.tcp.TCPChannel.newConnection(Unknown Source)
at sun.rmi.server.UnicastRef.newCall(Unknown Source)
at sun.rmi.registry.RegistryImpl_Stub.rebind(Unknown Source)
at java.rmi.Naming.rebind(Unknown Source)
at RemoteObject.main(RemoteObject.java:41)
操作流程:JAVAC 接口.java
JAVAC RemoteObject.java
RMIC RemoteObject
start rmiregistry
java RemoteObject
服务器源程序如下:
import java.rmi.Naming;
import java.rmi.server.UnicastRemoteObject;
import java.rmi.RemoteException;
import java.rmi.RMISecurityManager;
public class RemoteObject extends UnicastRemoteObject implements RemoteInterface
{
String name;
public RemoteObject(String name) throws RemoteException{
super();
this.name=name;
}
public String message(String message) throws RemoteException
{
String returnString = "My Name is:"+name+",thanks for your message:"+message;
System.out.println("Returning:"+returnString);
return "My Name is:"+name+",thanks for your message:"+message;
}
public static void main (String args[])
{
System.setSecurityManager (new RMISecurityManager());
try{
String myName = "ServerTest";
RemoteObject theServer = new RemoteObject (myname);
Naming.rebind(myName,theServer);
System.out.println("Ready to continue");
} catch (Exception e){
System.out.println("An Exception occured while creating server");
e.printStackTrace();
} }}
错误提示
An Exception occured while creating server
java.security.AccessControlException: access denied (java.net.SocketPermission 1
27.0.0.1:1099 connect,resolve)
at java.security.AccessControlContext.checkPermission(Unknown Source)
at java.security.AccessController.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkConnect(Unknown Source)
at java.net.Socket.(Unknown Source)
at java.net.Socket.(Unknown Source)
at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(Unknown S
ource)
at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(Unknown S
ource)
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(Unknown Source)
at sun.rmi.transport.tcp.TCPChannel.createConnection(Unknown Source)
at sun.rmi.transport.tcp.TCPChannel.newConnection(Unknown Source)
at sun.rmi.server.UnicastRef.newCall(Unknown Source)
at sun.rmi.registry.RegistryImpl_Stub.rebind(Unknown Source)
at java.rmi.Naming.rebind(Unknown Source)
at RemoteObject.main(RemoteObject.java:41)
|
看来javaman1977(大海)是在跟你开玩笑。呵呵!
你的程序是报安全错误。是因为你的程序中server端有一个SecurityManager,所以你需要写一个.policy文件(如:server.policy)来允许你的程序访问本机的rmi相应端口(default好象是:1099)。
运行时:java -Djava.policy=server.policy RemoteObject
试试。
你的程序是报安全错误。是因为你的程序中server端有一个SecurityManager,所以你需要写一个.policy文件(如:server.policy)来允许你的程序访问本机的rmi相应端口(default好象是:1099)。
运行时:java -Djava.policy=server.policy RemoteObject
试试。
|
hehe
其实你在本机器跑这个程序,根本就不用装安全策略,可以省略他“System.setSecurityManager (new RMISecurityManager()); “
假如你要装,我给你一个例子
grant {
// Allow everything for now
permission java.security.AllPermission;
};
其实你在本机器跑这个程序,根本就不用装安全策略,可以省略他“System.setSecurityManager (new RMISecurityManager()); “
假如你要装,我给你一个例子
grant {
// Allow everything for now
permission java.security.AllPermission;
};