【问题标题】:RMI Server-side java.net.MalformedURLException: unknown protocol: cRMI 服务器端 java.net.MalformedURLException:未知协议:c
【发布时间】:2019-03-28 16:41:33
【问题描述】:

我上网了2天,没有找到解决我的问题的办法,所以决定在这里问。 我正在做一个书本练习:“用 Java RMI 编写一个程序 Client-Server,它返回客户端给出的整数的总和。它必须实现一个添加整数的方法,以及一个获取总数的方法”。 我面对的是服务器端。

程序返回的错误是:

java.rmi.UnmarshalException: Error unmarshaling return; nested exception is: 
java.net.MalformedURLException: unknown protocol: c
at sun.rmi.transport.StreamRemoteCall.executeCall(Unknown Source)
at sun.rmi.server.UnicastRef.invoke(Unknown Source)
at sun.rmi.registry.RegistryImpl_Stub.bind(Unknown Source)
at java.rmi.Naming.bind(Unknown Source)
at ContatoreServer.main(ContatoreServer.java:15)
Caused by: java.net.MalformedURLException: unknown protocol: c
    at java.net.URL.<init>(Unknown Source)
    at java.net.URL.<init>(Unknown Source)
    at java.net.URL.<init>(Unknown Source)
    at sun.rmi.server.LoaderHandler.pathToURLs(Unknown Source)
    at sun.rmi.server.LoaderHandler.getDefaultCodebaseURLs(Unknown Source)
    at sun.rmi.server.LoaderHandler.loadClass(Unknown Source)
    at java.rmi.server.RMIClassLoader$2.loadClass(Unknown Source)
    at java.rmi.server.RMIClassLoader.loadClass(Unknown Source)
    at sun.rmi.server.MarshalInputStream.resolveClass(Unknown Source)
    at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
    at java.io.ObjectInputStream.readClassDesc(Unknown Source)
    at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
    at java.io.ObjectInputStream.readObject0(Unknown Source)
    at java.io.ObjectInputStream.readObject(Unknown Source)
    ... 5 more

这是我的课程: 本地对象

public class ContatoreLocale {
    private int num;

    public ContatoreLocale(){
        this.num=0;
    }

    public synchronized int get(){
        return this.num;
    }

    public synchronized void add(int i){
        this.num+=i;
    }
}

远程接口

public interface IContatore extends Remote{
    int get() throws RemoteException;
    void add(int i) throws RemoteException;

}

远程对象

public class ContatoreRemoto extends ContatoreLocale implements IContatore{
    Logger log= Logger.getLogger("global");

    //Costruttore
    public ContatoreRemoto() throws RemoteException{
        super();
        UnicastRemoteObject.exportObject(this, 0);
        try {
            Naming.rebind("Contatore", this);
            //errore
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }   
    }

    public int get(String nome) throws RemoteException{
        return this.get();
    }

    public void add(String nome,int i) throws RemoteException{
        this.add(i);
    }
}

服务器

public class ContatoreServer {
    static Logger log= Logger.getLogger("global");

    public static void main(String[] args) {
            try {
                ContatoreRemoto cr=new ContatoreRemoto();
                log.info("Server Pronto");
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    }

请帮帮我:S

【问题讨论】:

  • 感谢@AnkurChrungoo 的评论我将 rmiregistry 作为 Eclipse 中的外部工具启动,所以我不知道如何管理您提出的解决方案
  • Namng.rebind() 需要一个 URL,而不仅仅是一个名称。你需要"//localhost/Contatore",或者至少需要"Contatore"

标签: java server rmi malformedurlexception


【解决方案1】:

根据我的经验,这与放置 RMI Eclipse 项目的文件系统中的绝对路径有关。我想你可能在路径中有一个空白,一个空格。所以 RMI 无法解析路径并被中断,......然后我们得到“格式错误”的异常。它可能在“c”附近,因此,您会在异常中得到“c”。 我建议您转到文件系统,导航到项目文件夹,然后检查项目的绝对路径。我敢打赌你有一个空白。 如 c:\Users\mylab c\MyProjects.... 注意到空格了吗?

【讨论】:

  • Naming.rebind() 抛出异常,并且不使用文件系统路径。
猜你喜欢
  • 2011-11-22
  • 1970-01-01
  • 1970-01-01
  • 2020-04-20
  • 1970-01-01
  • 2016-10-08
  • 1970-01-01
  • 2012-09-03
相关资源
最近更新 更多