我运行下面的 rmi 程序,但它显示命令执行失败
Posted
技术标签:
【中文标题】我运行下面的 rmi 程序,但它显示命令执行失败【英文标题】:i run the rmi program below but it shows command execution failed 【发布时间】:2021-12-26 09:15:02 【问题描述】:这个程序应该从客户端向服务器发送一条文本消息。我完成了编译文件。 server.java 运行成功,但 client.java 显示错误。 错误列表太长,所以我只截图了几行。
这是代码ReceiverMessageInterface.java
package com.mycompany.dsa3;
import java.rmi.*;
public interface ReceiveMessageInterface extends Remote
void receiveMessage(String x) throws RemoteException;
RmiServer.java
package com.mycompany.dsa3;
import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.*;
import java.net.*;
public class RmiServer extends
java.rmi.server.UnicastRemoteObject implements ReceiveMessageInterface
String address;
Registry registry;
public void receiveMessage(String x) throws RemoteException
System.out.println(x);
public RmiServer() throws RemoteException
try
address = (InetAddress.getLocalHost()).toString();
catch(Exception e)
System.out.println("can't get inet address.");
int port=3232;
System.out.println("this address=" + address + ",port=" + port);
try
registry = LocateRegistry.createRegistry(port);
registry.rebind("rmiServer", this);
catch(RemoteException e)
System.out.println("remote exception"+ e);
static public void main(String args[])
try
RmiServer server = new RmiServer();
catch (Exception e)
e.printStackTrace();
System.exit(1);
RmiClient.java
package com.mycompany.dsa3;
import java.rmi.*;
import java.rmi.registry.*;
import java.net.*;
public class RmiClient
static public void main(String args[])
ReceiveMessageInterface rmiServer;
Registry registry;
String serverAddress=args[0];
String serverPort=args[1];
String text=args[2];
System.out.println ("sending " + text + " to " +serverAddress + ":" + serverPort);
try
registry=LocateRegistry.getRegistry
(serverAddress,(new Integer(serverPort)).intValue());
rmiServer=(ReceiveMessageInterface)(registry.lookup("rmiServer"));
// call the remote method
rmiServer.receiveMessage(text);
catch(RemoteException e)
e.printStackTrace();
catch(NotBoundException e)
System.err.println(e);
请帮助我更正代码。谢谢
【问题讨论】:
这与rmi无关。 【参考方案1】:从 args 数组中读取元素时似乎发生了错误。 请检查您传递的参数的长度。 在从中读取元素之前,我们还应该始终检查 args[] 的长度。 请参阅下面的链接和答案 What is "String args[]"? parameter in main method Java
【讨论】:
以上是关于我运行下面的 rmi 程序,但它显示命令执行失败的主要内容,如果未能解决你的问题,请参考以下文章
FlywayDB:迁移失败并显示“执行语句错误”,但手动运行迁移 SQL 代码有效