Java RMI教程编译时错误 - 找不到接口

时间:2012-11-28 11:06:58

标签: java rmi

我在这里关注分布式系统的基本Java RMI教程:http://people.cs.aau.dk/~bnielsen/DS-E08/Java-RMI-Tutorial/

我在编译服务器实现时遇到问题。

错误如下:

RMIServer.java:5: cannot find symbol
symbol: class ServerInterface
public class RMIServer extends UnicastRemoteObject implements ServerInterface {
                                                          ^
1 error

这是我的服务器实现:

package rmiTutorial; 
import java.rmi.server.UnicastRemoteObject; 
import java.rmi.*; 

public class RMIServer extends UnicastRemoteObject implements ServerInterface { 

  private String myString = " "; 

  //Default constructor 
  public RMIServer() throws RemoteException { 
    super(); 
  } 

  //inherited methods 
  public void setString(String s) throws RemoteException { 
    this.myString =s; 
  } 

  public String getString() throws RemoteException{ 
    return myString; 
  } 

  //main: instantiate and register server object 
  public static void main(String args[]){ 
    try{ 
      String name = "RMIServer"; 
      System.out.println("Registering as: \"" + name + "\""); 
      RMIServer serverObject = new RMIServer(); 
      Naming.rebind(name, serverObject); 
      System.out.println(name + " ready..."); 
    } catch (Exception registryEx){ 
      System.out.println(registryEx); 
    } 
  }  
}

ServerInterface:

package rmiTutorial;                                                                                
import java.rmi.*;

public interface ServerInterface {

  public String getString() throws RemoteException;
  public void setString(String s) throws RemoteException;

}

RMIServer类和ServerInterface都在同一个包中。 我完全按照教程,所以我真的不明白我是如何设法打破它的!

任何帮助将不胜感激! 感谢。

花托

1 个答案:

答案 0 :(得分:1)

我怀疑你是在分别编译它们。本教程对此并不清楚,但您需要将它们编译在一起(在最简单的情况下):

javac rmiTutorial/RMIServer.java rmiTutorial/ServerInterface.java

(包括必要的其他适当标志 - libs,classpath等)。

您需要将两者编译在一起,以便编译器在构建ServerInterface时可以找到RMIServer。您可以首先编译ServerInterface,但是您需要使用合适的类路径引用编译RMIServer,以便它可以找到接口。