JAVA RMI ClassNotFound

时间:2018-08-09 04:21:46

标签: java rmi classnotfoundexception

最近几天我一直在尝试启动基本的JAVA RMI教程服务器。

我首先启动命令行并输入以下内容:

rmiregistry
java -classpath E:\Rmi\src\* -Djava.rmi.server.codebase=file:/E:/RMI/Src/Hello** Server
* I have all my *.java, and *.class files under "E:\RMI\SRC"
** I have attempted to use Hello.class, Hello.Java, made 'Hello' into a jar file and
** I have even-attempted to leave it blank

尝试启动我的服务器。我在Java 8中编译了它。

我得到的例外是:

Server exception: 
java.rmi.ServerException: 
RemoteException occurred in server thread; 
nested exception is: 
java.rmi.UnmarshalException: 
error unmarshalling arguments; 
nested exception is: 
java.lang.ClassNotFoundException: 
Hello java.rmi.ServerException: 
RemoteException occurred in server thread; 
nested exception is: 
java.rmi.UnmarshalException: 
error unmarshalling arguments; 
nested excep tion is: 
java.lang.ClassNotFoundException:
Hello at java.rmi/sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:391)

类定义如下:

Hello.java:

import java.rmi.Remote; 
import java.rmi.RemoteException;  

// Creating Remote interface for our application 
public interface Hello extends Remote {  
   void printMsg() throws RemoteException;  
}

ImplExample.java

// Implementing the remote interface 
public class ImplExample implements Hello {
    // Implementing the interface method 
    public void printMsg() {  
        System.out.println("This is an example RMI program");  
    }  
}

Client.java

    import java.rmi.registry.LocateRegistry; 
    import java.rmi.registry.Registry;  
    public class Client {  
    private Client() {}  
    public static void main(String[] args) {  
       try {  
     // Getting the registry 
        Registry registry = LocateRegistry.getRegistry(null); 

     // Looking up the registry for the remote object 
        Hello stub = (Hello) registry.lookup("Hello"); 

     // Calling the remote method using the obtained object 
        stub.printMsg(); 

     // System.out.println("Remote method invoked"); 
     } catch (Exception e) {
        System.err.println("Client exception: " + e.toString()); 
        e.printStackTrace(); 
     }
  } 
}

Server.java

import java.rmi.registry.Registry; 
import java.rmi.registry.LocateRegistry; 
import java.rmi.RemoteException; 
import java.rmi.server.UnicastRemoteObject; 

public class Server extends ImplExample { 
   public Server() {} 
   public static void main(String args[]) { 
      try { 
         // Instantiating the implementation class 
         ImplExample obj = new ImplExample(); 

         // Exporting the object of implementation class  
         // (here we are exporting the remote object to the stub) 
         Hello stub = (Hello) UnicastRemoteObject.exportObject(obj, 0);  

         // Binding the remote object (stub) in the registry 
         Registry registry = LocateRegistry.getRegistry(); 

         registry.bind("Hello", stub);  
         System.err.println("Server ready"); 
      } catch (Exception e) { 
         System.err.println("Server exception: " + e.toString()); 
         e.printStackTrace(); 
      } 
   } 
} 

到达“ registry.bind(“ hello”,stub)'时,服务器崩溃。存根不为空。再次,“ rmiregistry”此时已经启动。

我将非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

来自文档

  

在其中组织类的目录的URL   程序包命名的子目录

因此,URL应该指向类的根目录,或多或少是这样的:

-Djava.rmi.server.codebase=file://E:/Rmi/src

尝试一下。