RMI服务器端java.net.MalformedURLException:未知协议:c

时间:2018-10-24 09:55:15

标签: java server rmi malformedurlexception

我在网上冲浪了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

这里有我的课程: LocalObject

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

1 个答案:

答案 0 :(得分:-1)

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