当前位置: 技术问答>java相关
请问这个RMI的程序怎么执行?
来源: 互联网 发布时间:2015-07-10
本文导语: 我用JDK 先定义一个扩展了Remote接口的接口: import java.rmi.*; public interface RemoteInterface extends Remote { public String getMessage(String msg)throws RemoteException; } 再定义RemoteServer类: import java.rmi.*; import java.rmi.server.*; public...
我用JDK
先定义一个扩展了Remote接口的接口:
import java.rmi.*;
public interface RemoteInterface extends Remote
{
public String getMessage(String msg)throws RemoteException;
}
再定义RemoteServer类:
import java.rmi.*;
import java.rmi.server.*;
public class RemoteServer extends UnicastRemoteObject implements RemoteInterface
{String name;
public RemoteServer(String name)throws RemoteException
{
super();
this.name=name;
}
public String getMessage(String msg)throws RemoteException
{
String str="The Server Name is:"+name+",your message is:"+msg;
System.out.println(str);
return str;
}
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("Server Exception:"+e.toString());
}
}
}
最后写客户端程序:
import java.rmi.*;
public class RemoteClient
{
public static void main(String[] args)
{
System.setSecurityManager(new RMISecurityManager());
try{
RemoteInterface server;
server=(RemoteInterface)Naming.lookup("ServerTest");
String serverString=server.getMessage("hello");
System.out.println("The Server Say;n"+serverString);
}catch(Exception e){
System.out.println("Server Exception:"+e.toString());
}
}
}
编译javac RemoteServer.java
显示:java.rmi.server.RemoteObject是抽象的,不能被实例化
RemoteObject theServer=new RemoteObject(myName);
这是为什么?另:RMI的程序怎么执行啊?是不是还要先设置好classpath并且启动RMI注册程序?请高手写出详细过程.谢谢!
先定义一个扩展了Remote接口的接口:
import java.rmi.*;
public interface RemoteInterface extends Remote
{
public String getMessage(String msg)throws RemoteException;
}
再定义RemoteServer类:
import java.rmi.*;
import java.rmi.server.*;
public class RemoteServer extends UnicastRemoteObject implements RemoteInterface
{String name;
public RemoteServer(String name)throws RemoteException
{
super();
this.name=name;
}
public String getMessage(String msg)throws RemoteException
{
String str="The Server Name is:"+name+",your message is:"+msg;
System.out.println(str);
return str;
}
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("Server Exception:"+e.toString());
}
}
}
最后写客户端程序:
import java.rmi.*;
public class RemoteClient
{
public static void main(String[] args)
{
System.setSecurityManager(new RMISecurityManager());
try{
RemoteInterface server;
server=(RemoteInterface)Naming.lookup("ServerTest");
String serverString=server.getMessage("hello");
System.out.println("The Server Say;n"+serverString);
}catch(Exception e){
System.out.println("Server Exception:"+e.toString());
}
}
}
编译javac RemoteServer.java
显示:java.rmi.server.RemoteObject是抽象的,不能被实例化
RemoteObject theServer=new RemoteObject(myName);
这是为什么?另:RMI的程序怎么执行啊?是不是还要先设置好classpath并且启动RMI注册程序?请高手写出详细过程.谢谢!
|
编译运行Server:
javac -d . *.java
rmic -d . RemoteServer
start rmiregistry
start java example2.CityServer
编译运行client:
首先要写一个client.policy文件
grant
{ permission java.net.SocketPermission
"*:1024-65535","connect";
permission java.net.SocketPermission
"*:80","connect";
};
保存在client目录下。
javac -d . *.java
然后将server目录下的*Stub.class复制到client目录下:
copy *Stub.class example2
java -Djava.security.policy=client.policy RemoteClient
一切搞定。
javac -d . *.java
rmic -d . RemoteServer
start rmiregistry
start java example2.CityServer
编译运行client:
首先要写一个client.policy文件
grant
{ permission java.net.SocketPermission
"*:1024-65535","connect";
permission java.net.SocketPermission
"*:80","connect";
};
保存在client目录下。
javac -d . *.java
然后将server目录下的*Stub.class复制到client目录下:
copy *Stub.class example2
java -Djava.security.policy=client.policy RemoteClient
一切搞定。