JAVA rmi嵌套异常由类未找到引起

时间:2014-06-28 21:03:47

标签: java rmi

我正在学习rmi,我想出了一个问题,我无法解决它。 这是简单的代码

客户代码:

package rmi_test;

import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;


public class client {
public static void main(String [] args) throws RemoteException, NotBoundException
{
    Registry registry = LocateRegistry.getRegistry("127.0.0.1");
    DNInterface s = (DNInterface) registry.lookup("DNInterface");
    if (s.test())
        System.out.println("Hello World");
}
}

接口代码:

package rmi_test;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface DNInterface  extends Remote{
      public boolean test() throws RemoteException;
}

服务器代码:

package rmi_test;

import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
public class DataNodeImp implements DNInterface{
@Override
public boolean test() throws RemoteException {
        System.out.println("test success");
        return true;
}

public DataNodeImp()
{
    super();
}
private static void boot() throws RemoteException
{
    DNInterface d = new DataNodeImp();
    DNInterface stub =
            (DNInterface) UnicastRemoteObject.exportObject(d, 0);
    Registry registry = LocateRegistry.getRegistry();
    registry.rebind("DNInterface", stub);

}
public static void main(String[] args) throws RemoteException 
{
    String name = "DNInterface";
    boot();

}
 } 

然后我使用以下命令编译代码

javac -cp src: src/rmi_test/DNInterface.java 
javac -cp src: src/rmi_test/DataNodeImp.java 
javac -cp src: src/rmi_test/client.java

然后我输入

rmiregistry &

当我尝试使用

java -cp src: rmi_test.DataNodeImp to run the server

它说

  Exception in thread "main" java.rmi.ServerException: RemoteException occurred in server thread; nested exception is: 

  java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is: 
  java.lang.ClassNotFoundException: rmi_test.DNInterface

我运行代码的命令有什么问题吗? 为什么我找不到DNInterface.class?

非常感谢!

1 个答案:

答案 0 :(得分:0)

注册表在其CLASSPATH中没有该类。

最简单的解决方案是在服务器JVM而不是rmiregistry中使用LocateRegistry.createRegistry()。