2012-05-07 43 views
0

我曾一個簡單的程序 但是,當你在命令運行客戶端 這個錯誤出現的UnsupportedOperationException異常的RMI

HelloClient exception: java.lang.UnsupportedOperationException: Not supported yet. 

這是我的編碼

接口類

import java.rmi.*; 

    public interface HelloInterface extends Remote { 

    public String say() throws RemoteException; 


    } 

實施class

  import java.rmi.RemoteException; 
      import java.rmi.server.UnicastRemoteObject; 

     /** 
      * 
      * @author x 
      */ 
    public class HelloServerImpl extends UnicastRemoteObject implements HelloInterface { 

    private String message; 

    public HelloServerImpl(String msg)throws RemoteException{ 
    message = msg; 
    } 


@Override 
public String say() throws RemoteException { 
    throw new UnsupportedOperationException("Not supported yet."); 
} 




} 

服務器類

 import java.rmi.Naming; 

     /** 
     * 
     * @author x 
     */ 
     public class HelloServer { 
     public static void main (String []args){ 
      try { 
     Naming.rebind("HELLOSERVER", new HelloServerImpl("Hello word")); 
     System.out.println("Hello Server is ready."); 
    } catch (Exception ex) { 
     System.out.println("Hello server failed: "+ ex); 
    } 


    } 
    } 

Client類

  import java.rmi.Naming; 

     /** 
      * 
      * @author x 
      */ 
      public class HelloClient { 
      public static void main(String[]args){ 

     HelloInterface hello; 
     String url = "rmi://localhost/HELLOSERVER"; 


     try { 
     hello = (HelloInterface)Naming.lookup(url); 
     System.out.println(hello.say()); 
    } catch (Exception ex) { 
     System.err.println("HelloClient exception: " + ex); 
    } 

    } 
    } 

我準備寫的步驟,但仍是同樣的錯誤

爲什麼?

+1

+1 for humor :) –

+0

而執行你寫的代碼的問題是什麼? – EJP

回答

2

那麼你該寫自己:

@Override 
public String say() throws RemoteException { 
    throw new UnsupportedOperationException("Not supported yet."); 
} 

當然,它拋出一個異常。嘗試實際返回一個字符串:

@Override 
public String say() throws RemoteException { 
    return "hello"; 
} 
+1

[對不起](http://i1.kym-cdn.com/entries/icons/original/000/000/015/orly.jpg),忍不住...... –

+0

非常感謝你 – Seetah

相關問題