一共有三个文件,放在同一目录即可.
========================================================================
//RemoteClient.java
import java.rmi.RMISecurityManager;
import java.rmi.Naming;public class RemoteClient {
public static void main(String args[]) {
System.setSecurityManager(new RMISecurityManager());
try {
RemoteInterface server = (RemoteInterface)Naming.lookup("rmi://127.0.0.1/ServerTest");
String serverString = server.message("Hello There");
System.out.println("The server says:\n"+serverString);
} catch(Exception e) {
System.out.println("Error while performing RMI");
}
}
}
================================================================
//RemoteObject.java
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 = "rmi://192.168.200.101/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");
}
}
}
==========================================================
//RemoteInterface.java
public interface RemoteInterface extends java.rmi.Remote {
String message(String message) throws java.rmi.RemoteException;
}
========================================================================
//RemoteClient.java
import java.rmi.RMISecurityManager;
import java.rmi.Naming;public class RemoteClient {
public static void main(String args[]) {
System.setSecurityManager(new RMISecurityManager());
try {
RemoteInterface server = (RemoteInterface)Naming.lookup("rmi://127.0.0.1/ServerTest");
String serverString = server.message("Hello There");
System.out.println("The server says:\n"+serverString);
} catch(Exception e) {
System.out.println("Error while performing RMI");
}
}
}
================================================================
//RemoteObject.java
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 = "rmi://192.168.200.101/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");
}
}
}
==========================================================
//RemoteInterface.java
public interface RemoteInterface extends java.rmi.Remote {
String message(String message) throws java.rmi.RemoteException;
}
解决方案 »
- Java Date类型 减一个月怎么做?
- 一道简单的课后习题~帮忙看下做的对不对~谢谢
- 急求joc.jar的混淆器,有的请帮忙,收到即给分
- 拜托哪位高手哥哥看看哪错了,很简单的代码~
- java新手问问题。。。。
- repaint() 函数怎么老是不响应呢?(急!!!!)
- 郁闷了一上午--------------JDBC 访问SQLServer 一个巨奇怪的问题
- 怎样更改Java图标
- Socket通讯中建立多个数据通道!
- ?现在网页上有许多java写的文本编辑器,我想做一个,但不知道如何下手,有没有现成的例子,谢谢!给下载地点一定送分!
- 如何更改tomcat的端口
- where can download emulator(POSE)'s ROMs. Free is better
加上import java.net.*
运行java -Djava.security.policy=xxx.policy myClient试试?
XXX.policy是一文本文件,内容:
grant {
// allows anyone to listen on un-privileged ports
//permission java.net.SocketPermission "*:1024-65535", "listen,accept,connect";
permission java.security.AllPermission;
};你再对照下面的例子看看:
Java中的远程方法调用
远程方法调用(Remote Method Invocation)机制,就是你利用RMI可以访问别的计
算机上的一个对象,并调用这个远程对象的方法,至于参数传递等细节由RMI处理。RMI是Java1.1引入的分布式对象软件包,它的出现简化了在多台机器上的Java应用之间的通信。相比CORBA,RMI功能较弱且只能用于Java系统。
要使用RMI,必须构造四个主要的类:远程对象的本地接口、RMI客户、远程对象实现和RMI服务器。
远程对象以代码存根形式在网络上传输,而非远程对象通过复制。
Stub存根代码:1。待使用的远程对象的标识符2。被调用方法的说明3。调度的参数
RMI服务器生成远程对象实现的一个实例,并用一个特殊的URL注册它,RMI客户在远程服务器上查找对象,若找到就把它转换成本地接口类型,然后像一个本地对象一样使用它。下面是一个简单的RMI例子,远程对象只返回一个消息字符串。要使这个例子更有价值,我们需要做的就是完善远程对象实现类。
1.程对象的本地接口类(Rem.java)
该类仅仅是一个接口,而不是实现,RMI客户机可以直接使用它,RMI服务器必须通过一个远程对象来实现它,并用某个URL注册它的一个实例。
import java.rmi.*;
public interface Rem extends Remote { public String getMessage() throws RemoteException;}
本地接口(Rem)必须是公共的,否则客户机在加载一个实现该接口的远程对象时就会出错。此外,它还必须从java.rmi.Remote继承而来,接口中的每一个方法都必须抛出远程异常java.rmi.RemoteException。
2.RMI客户类(RemClient.java)
RMI客户使用Naming.lookup在指定的远程主机上查找对象,若找到就把它转换成本地接口Rem类型,然后像一个本地对象一样使用它。与CORBA不同之处在于RMI客户必须知道提供远程服务主机的URL,这个URL可以通过rmi://host/path或rmi://host:port/path来指定,如果省略端口号,就使用1099。Naming.lookup可能产生三个异常:RemoteException、NotBoundException、MalformedURLException,三个寻常都需要捕获。RemoteException、Naming和NotBoundException在java.rmi.*中定义,MalformedURLException在java.net.*中定义。另外,客户机将向远程对象传递串行化对象Serializable,所以还应在程序中输入java.io.*。
import java.rmi.*;
import java.net.*;
import java.io.*;
public class RemClient {
public static void main(String[] args) {
try {
String host = (args.length > 0) ? args[0] : "localhost"; //从命令行读取远程主机名
//通过URL在远程主机上查找对象,并把它转化为本地接口Rem类型
Rem remObject=(Rem)Naming.lookup("rmi://" + host + "/Rem"); System.out.println(remObject.getMessage()); //调用远程对象的方法
} catch(RemoteException re) {System.out.println("RemoteException: " + re);
} catch(NotBoundException nbe) {System.out.println("NotBoundException: " + nbe);
} catch(MalformedURLException mfe){System.out.println("MalformedURLException:"+ mfe);
}}}
3.远程对象实现类(RemImpl.java)
这个类真正实现RMI客户调用的远程对象,它必须从UnicastRemoteObject继承,其构造函数应抛出RemoteException异常。
import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;
public class RemImpl extends UnicastRemoteObject implements Rem {
public RemImpl() throws RemoteException {} //构造函数抛出RemoteException异常
public String getMessage() throws RemoteException {
return("Here is a remote message."); }} //向RMI客户返回一个消息串
4.RMI服务器类(RemServer.java)
该类创建远程对象实现RemImpl的一个实例,然后用一个特定的URL来注册它,所谓注册就是通过Naming.bind或Naming.rebind来将RemImpl实例绑定到URL上。
import java.rmi.*;
import java.net.*;
public class RemServer {
public static void main(String[] args) {
try {
RemImpl localObject = new RemImpl(); //生成远程对象实现的一个实例Naming.rebind("rmi:///Rem", localObject); //将远程对象实例绑定到rmi:///Rem 上
}catch(RemoteException re){System.out.println("RemoteException:"+re);
}catch(MalformedURLException mfe) {System.out.println("MalformedURLException: "+mfe);
}}}
编译和运行
1.编译RMI客户和服务器,这将自动编译远程对象的本地接口和远程对象实现
javac RemClient.java //自动编译远程对象的本地接口Rem.java
javac RemServer.java //自动编译远程对象实现RemImpl.java
2. 生成客户承接模块和服务器框架
rmic RemImpl
这将构造RemImpl_Stub.class和RemImpl_Skeleton.class。
将Rem.class、RemClient.class和RemImpl_Stub.class拷贝到RMI客户机,将Rem.class、RemImpl.class 、RemServer.class和RemImpl_Skeleton.class拷贝到RMI服务器。
3. 启动RMI注册
rmiregistry
//在服务器上执行。不论有多少个远程对象,本操作只需做一次
4. 运行
java RemServer.class
//启动RMI服务器(在服务器上执行)
java RemClient.class //启动RMI客户,将输出“Here is a remote message.”
谢谢指点,按照你的方法我试过了,没有问题.另外,我还想问一下,如果RemImpl的接口函数的内容改变了,需不需要重新生成RemImpl_skel.class和RemImpl_stub.class
非常谢谢你.
同样,Rem.class、RemImpl.class 、RemServer.class和RemImpl_Skeleton.class文件放到rmi服务器的哪呀?
如果是在本地机调试,这些文件放到哪些目录下?