gpt4 book ai didi

java - RMI 中的 UnsupportedOperationException

转载 作者:行者123 更新时间:2023-12-02 07:48:47 27 4
gpt4 key购买 nike

我编写了一个简单的程序但是当你通过命令运行客户端时出现此错误

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

这是我的编码

接口(interface)类

    import java.rmi.*;

public interface HelloInterface extends Remote {

public String say() throws RemoteException;


}

实现类

           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);
}


}
}

客户端类

          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);
}

}
}

我准备写步骤了,但还是同样的错误

为什么?

最佳答案

你自己写的:

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

当然会抛出异常。尝试实际返回一个字符串:

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

关于java - RMI 中的 UnsupportedOperationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10486393/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com