---
title: "jdk 1.5下运行rmi例子的方法"
author: "DavyLee"
date: "2009-02-20T17:59:18Z"
tags: []
slug: "jdk-1-5-rmi"
draft: false
lastmod: "2009-02-20T17:59:18Z"
id: 336185
url: "https://davylee.iteye.com/blog/336185"
---

最近在jdk1.5下测试rmi的简单程序，明明照着教程上一步一步地做，却不能成功，搜索了很多篇帖子，报错依然如故。料想是运行方法不对。不过想想也很奇怪，网上一大堆的帖子，为什么我照做都不能成功呢？难道真的是人品问题？

 

      想到帖子可能大家胡乱转帖，以讹传讹，翻本书看看，然后在Core Java 上发现了下面一段话：

 

Before starting the RMI registry, make sure that the CLASSPATH environment variable is not set to anything, and double-check that the current directory contains no class files. Otherwise, the RMI registry might find spurious class files, which will confuse it when it should download additional classes from a different source. There is a reason for this behavior; see http://java.sun.com/javase/6/docs/technotes/guides/rmi/codebase.html.

这是Core Java上的原文，翻译一下：

 

【译】：

 

在你启动rmiregistry(远程注册表)以前，确定你的classpath这个环境变量没有指向任何变量（即别设置classpath）,同时再确保当前目录（指的是命令行提示符指示的目录）没有任何类。否则,rmiregistry会发现不相干的类(并注册到rmiregistry中),这会混淆它从另外的地方(源)下载其他的类。想了解其中的具体原因，请参考http://java.sun.com/javase/6/docs/technotes/guides/rmi/codebase.html

 

这说明，如果你想加载某个类注册到rmiregistry，可以把此类放到某个目录下，然后在命令行里，进入此目录，运行start rmiregisty,则此类已被成功注册到rmiregistry中。因此，如果你想测试那些helloWorld类似的rmi程序，只需要照书中的例子写，默认包即可，然后把所有的class文件放到一个目录里，命令行里进入此目录，运行start rmiregistry，再运行Server,最后运行Client即可。

说明：这是在jdk1.5下才行。