1

我正在尝试测试 java 的 RMI,但我无法让 rmic 命令工作。

截屏

请告诉您是否需要我的代码,尽管我很确定这并不重要,但我已经正确实现了 RMI 功能。

服务器端代码:

  public static void main(String[] args)
    {
        try {
            Registry r = LocateRegistry.getRegistry();
            r.bind("RService", new RSimpl());
        }
        catch (Exception ex) {
            ex.printStackTrace();
            System.out.println("Cannot Start Remote Service");
        }
        System.out.println("Remote Service Is Running");
    }   

客户端代码:

@SuppressWarnings("deprecation")
    public Object[] getServiceList()
    {
        boolean connectionsuccess = false;
        Object[] objList = null;
        try {
            System.setSecurityManager(new RMISecurityManager());
            server = (RemoteService) Naming.lookup("rmi://192.168.1.77/RService");
            connectionsuccess = true;
        }
        catch(Exception ex) {
            Object[] options = {"Retry","Cancel"};
            int o = JOptionPane.showOptionDialog(frame, "Cannot Establish Connection To The Server. \n Do You Want To Retry?", "Connection Error", JOptionPane.YES_NO_OPTION, JOptionPane.ERROR_MESSAGE, null, options, options[0]);
            if(o==0)
            {
                i=0;
                attemptConnect();
            }
            else
            {
                i=1;
                return error;
            }
        }
    }

RSimpl.java:

    package testrmi;

import java.rmi.*;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.*;
import java.util.*;

public class RSimpl extends UnicastRemoteObject implements RemoteService {

    HashMap map;

    public RSimpl() throws RemoteException
    {
        setUpServices();
    }

    public void setUpServices()
    {
        map = new HashMap();
        map.put("Dice Roll Service", new DiceRoll());
        map.put("Calculator Service", new Calculator());
        // Keep Adding Services On The Go!
    }

    public Object[] getServiceList()
    {
        return map.keySet().toArray();
    }

    public Service getService(Object SvcKey)
    {
        Service theService = (Service) map.get(SvcKey);
        return theService;
    }

    public static void main(String[] args)
    {
        try {
            Registry r = LocateRegistry.getRegistry();
            r.bind("RService", new RSimpl());
        }
        catch (Exception ex) {
            ex.printStackTrace();
            System.out.println("Cannot Start Remote Service");
        }
        System.out.println("Remote Service Is Running");
    }   
}

RemoteService.java(接口):

package testrmi;

import java.rmi.*;

public interface RemoteService extends Remote {

    public Object[] getServiceList() throws RemoteException;
    public Service getService(Object SvcKey) throws RemoteException;

}
4

3 回答 3

2

命名类的类文件testrmi.RSImpl必须testrmi/RSImpl.class相对于 CLASSPATH 中命名的目录之一。

于 2017-07-17T13:17:10.093 回答
-1

在这种情况下似乎找不到该文件。尝试在 cmd 中输入 dir 以查看您的文件夹是否存在。您还可以导航到 testrmi 文件夹并输入 rmic RSimpl。

如果它仍然不起作用,请尝试将您需要的文件复制到桌面上的文件夹中,导航到那里并进行测试。

于 2017-07-17T11:25:05.273 回答
-1

抱歉我的愚蠢问题...我没有添加 Oracle 上 RMIC 文档中定义的类路径。对于其他面临此问题的人,请打开您的系统环境变量并添加这两个路径变量:

  • .;
  • C:\user\myproject\myCustomCompiledClasses

您必须添加 DOT 路径变量以及已编译类的路径

于 2017-07-17T13:12:02.010 回答