import java.rmi.*;

public interface Hello extends Remote {
public String getGreeting() throws RemoteException;
}
import java.rmi.*;
import java.rmi.server.*; public class HelloImpl extends UnicastRemoteObject implements Hello { public HelloImpl() throws RemoteException {
// No action needed here.
} public String getGreeting() throws RemoteException {
return ("Hello there!");
}
}
import java.rmi.*;

public class HelloServer {
private static final String HOST = "localhost"; public static void main(String[] args) throws Exception {
// Create a reference to an
// implementation object
HelloImpl temp = new HelloImpl();
// Create the string URL holding the
// object's name
String rmiObjectName = "rmi://" + HOST + "/Hello";
// (Could omit host name here, since 'localhost'
// would be assumed by default.)
// 'Bind' the object reference to the name
Naming.rebind(rmiObjectName, temp);
// Display a message so that we know the process
// has been completed
System.out.println("Binding complete…\n");
}
}
import java.rmi.*;

public class HelloClient {
private static final String HOST = "localhost"; public static void main(String[] args) {
try {
// Obtain a reference to the object from the
// registry and typecast it into the appropriate
// type
Hello greeting = (Hello) Naming.lookup("rmi://" + HOST + "/Hello");
// Use the above reference to invoke the remote
// object's method
System.out.println("Message received: " + greeting.getGreeting());
} catch (ConnectException conEx) {
System.out.println("Unable to connect to server!");
System.exit();
} catch (Exception ex) {
ex.printStackTrace();
System.exit();
}
}
}

编译运行用到的命令

note

javac Hello.java
javac HelloImpl.java
javac HelloServer.java
javac HelloClient.java //This would generate both a stub fi le and a skeleton fi le. However, this stage is no
//longer required
rmic HelloImpl //Starting the RMI registry
rmiregistry //start server
java HelloServer //start client
java HelloClient //result
"Hello there!"

java RMI的更多相关文章

  1. Exception thrown by the agent : java.rmi.server.ExportException: Port already in use

    今天有个应用一直起不来,感觉配置都对啊,奇了怪了.看日志发现如下: STATUS | wrapper | 2017/01/04 08:09:31 | Launching a JVM...INFO | ...

  2. Java RMI之HelloWorld篇

    Java RMI 指的是远程方法调用 (Remote Method Invocation).它是一种机制,能够让在某个 Java 虚拟机上的对象调用另一个 Java 虚拟机中的对象上的方法.可以用此方 ...

  3. 启动tomcat时 错误: 代理抛出异常 : java.rmi.server.ExportException: Port already in use: 1099;

     错误: 代理抛出异常 : java.rmi.server.ExportException: Port already in use: 1099; nested exception is:  java ...

  4. JAVA RMI 实例

    下面我将介绍一个完整的实例,让初学者能快速体验RMI的功用. 分为以下四个步骤 1. 创建远程接口及声明远程方法(HelloInterface.java)2. 实现远程接口及远程方法(继承Unicas ...

  5. JAVA RMI例子

    RMI 是java语言的一个RPC框架,本文给出基础例子如下: 1.实现接口: public interface ICalc extends Remote { public int add(int p ...

  6. java.rmi.NoSuchObjectException: no such object in table

    jmx链接的时候,最简单的例子都行不通,郁闷,出现了: 参考:http://reiz6153.blog.163.com/blog/static/401089152009442723208/ 代码: M ...

  7. 启动tomcat时 错误: 代理抛出异常 : java.rmi.server.ExportException: Port already in use: 1099的解决办法

    一.问题描述 今天一来公司,在IntelliJ IDEA 中启动Tomcat服务器时就出现了如下图所示的错误:

  8. Java RMI 介绍和例子以及Spring对RMI支持的实际应用实例

    RMI 相关知识 RMI全称是Remote Method Invocation-远程方法调用,Java RMI在JDK1.1中实现的,其威力就体现在它强大的开发分布式网络应用的能力上,是纯Java的网 ...

  9. Java学习笔记(十六)——Java RMI

    [前面的话] 最近过的好舒服,每天过的感觉很充实,一些生活和工作的技巧注意了就会发现,其实生活也是可以过的如此的有滋有味,满足现在的状况,并且感觉很幸福. 学习java RMI的原因是最近在使用dub ...

  10. JAVA RMI helloworld入门

    Java RMI 指的是远程方法调用 (Remote Method Invocation).它是一种机制,能够让在某个 Java 虚拟机上的对象调用另一个 Java 虚拟机中的对象上的方法.可以用此方 ...

随机推荐

  1. windows脚本定时执行

    linux下可以直接用cron定时任务,window下可以使用schtasks 命令代替. 第一次在win7 cmd输入: schtasks 如果出现错误:“错误:无法加载列表资源” 的问题原因很简单 ...

  2. Grunt上手指南<转>

    原文链接:http://www.hulufei.com/post/grunt-introduction 安装 如果之前有装过grunt,卸载之 npm uninstall -g grunt 安装gru ...

  3. 编译fdk-aac for ios

    Build all: build-fdk-aac.sh Build for some architectures: build-fdk-aac.sh armv7s x86_64 Build unive ...

  4. parseFloat

    parseFloat会把输入完整的数进行比较,不会比较中间数字.

  5. MVC 依赖注入扩展

    需求: 小明想要完成一个功能F,需要一把锤子T. 有两种办法可以实现: 1)小明很爱动手,精力很旺盛,于是,自己创建一个具有功能F的锤子T,并使用T来完成F: 2)小明很懒,天天睡大觉,于是,他叫小健 ...

  6. kmp 和boyer-moore

    <html> <head> <meta http-equiv="content-type" content="text/html; char ...

  7. BZOJ2674 : Attack

    整体二分+树状数组套Treap,时间复杂度$O(n\log^3n)$. #include<cstdio> #include<cstdlib> #include<algor ...

  8. HDU 1045 (DFS搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1045 题目大意:在不是X的地方放O,所有O在没有隔板情况下不能对视(横行和数列),问最多可以放多少个 ...

  9. Codeforces Round #192 (Div. 2) A. Cakeminator

    #include <iostream> #include <vector> using namespace std; int main(){ int r,c; cin > ...

  10. oracle系列--第六篇 Oracle上面小试牛刀

    现在我们可以在oracle上面进行创建表,向表中插入数据,修改表中数据,删除数据,甚至删除表等一系列操作. 即我们所说的CRUD操作. --create a table which name is t ...