---
title: "java RMI的例子(可以运行) "
author: "<冫茖>"
date: "2010-01-29T16:30:36Z"
tags: ["JDK", "Java", "Linux", "Security", "应用服务器"]
categories: ["编程语言"]
slug: "java-rmi"
draft: false
lastmod: "2010-01-29T16:30:36Z"
id: 583476
url: "https://liuliang136.iteye.com/blog/583476"
---

0

[推荐]()

RMI，远程方法调用（Remote Method Invocation）是Enterprise  
     JavaBeans的支柱，是建立分布式Java应用程序的方便途径。RMI是非常容易使用的，但是它非常的强大。  
    RMI的基础是接口，RMI构架基于一个重要的原理：定义接口和定义接口的具体实现是分开的。

    **java 代码**

 

01. **package** org.itrun.remote;
02. **import** java.rmi.Remote;
03. **import** java.rmi.RemoteException;
04. /\**
05. * 远程接口
06. * @author jiangzhen
07. \*
08. \*/
09. **public** **interface** TestInterfactRemote **extends** Remote{
10. **public** String add(String a,String b) **throws** RemoteException;
11. **public** String add() **throws** RemoteException;
12. }
13. **package** org.itrun.remote;
14. **import** java.rmi.RemoteException;
15. **import** java.rmi.server.UnicastRemoteObject;
16. /\**
17. * 接口的实现
18. * @author jiangzhen
19. \*
20. \*/
21. **public** **class** TestInterfaceRemoteImpl **extends** UnicastRemoteObject **implements**
22. TestInterfactRemote {
23. **public** TestInterfaceRemoteImpl() **throws** RemoteException {
24. **super**();
25. }
26. **public** String add(String a, String b) **throws** RemoteException {
27. **return** a+b;
28. }
29. **public** String add() **throws** RemoteException {
30. **return** "Hello Word";
31. }
32. }
33. **package** org.itrun.server;
34. **import** java.rmi.Naming;
35. **import** org.itrun.remote.TestInterfaceRemoteImpl;
36. **import** org.itrun.remote.TestInterfactRemote;
37. /\**
38. * 服务器端
39. * @author jiangzhen
40. \*
41. \*/
42. **public** **class** Server{
43. **public** Server() {
44. **try** {
45. TestInterfactRemote testInterfactRemote = **new** TestInterfaceRemoteImpl();
46. Naming.rebind("rmi://10.0.0.123/server", testInterfactRemote);
47. } **catch** (Exception e) {
48. e.printStackTrace();
49. }
50. }
51. **public** **static** **void** main(String args\[]) {
52. **new** Server();
53. }
54. }
55. **package** org.itrun.client;
56. **import** java.rmi.Naming;
57. **import** org.itrun.remote.TestInterfactRemote;
58. /\**
59. * 客户端程序
60. * @author jiangzhen
61. \*
62. \*/
63. **public** **class** Client {
64. **public** **static** **void** main(String args\[]) {
65. **try** {
66. TestInterfactRemote testInterfactRemote = (TestInterfactRemote) Naming
67. .lookup("rmi://10.0.0.123/server");
68. System.out.println(testInterfactRemote.add("rmi a ", "rmib"));
69. } **catch** (Exception e) {
70. e.printStackTrace();
71. }
72. }
73. }

程序完成后

运行RMI系统

     现在我们建立了所有运行这个简单RMI系统所需的文件，现在我们终于可以运行这个RMI系统啦！来享受吧。

     我们是在命令控制台下运行这个系统的，你必须开启三个控制台窗口，一个运行服务器，一个运行客户端，还有一个运行RMIRegistry。

    首先运行注册程序RMIRegistry，你必须在包含你刚写的类的那么目录下运行这个注册程序。

    &gt;rmiregistry（linux） start rmiregistry(windows)

   运行程序首先需要修改下RMI的安全策略策略文件在 JDK 目录下的jre/lib/security/java.policy 的最后面加上下面这句话

grant{  
        permission java.security.AllPermission "","";  
};  
这条命令表示允许任何人作任何事情，

修改完后运行命令 rmiregistry ，然后就不用管他了

然后运行 server 端的代码 运行后也不用管他了  
java org.itrun.server.Server

最后运行 client 端代码  
java org.itrun.server.Client

运行结果 rmi a rmib