RMI对象已导出

时间:2016-12-05 19:43:34

标签: java rmi

我在试图让RMI正常工作时遇到了问题。我运行了注册表,当我尝试启动服务器时,窗口弹出不到一秒钟然后关闭。为什么会这样呢?

服务器包--- MyFileServer.java

package Server;

import java.io.File;
import java.io.FileInputStream;
import java.rmi.AlreadyBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;

/**
 * Created by alexi on 05/12/2016.
 */
public class MyFileServer extends UnicastRemoteObject implements ServerInit {



    protected  MyFileServer() throws RemoteException {
        super();
    }

    @Override
    public void importFiles(Notify n, String name) throws RemoteException {
        String videoPath = "src" + File.separator + "Videos" + File.separator + name;
        try {

            File video = new File(videoPath);
            FileInputStream in=new FileInputStream(video);
            byte [] mydata=new byte[(int)video.length()+1];
            int mylen=in.read(mydata);
            while(mylen>0){
                n.sendData(video.getName(), mydata, mylen);
                mylen=in.read(mydata);
            }

        }catch( Exception e){
            e.printStackTrace();
        }
    }

    public static void main(String[] args){
        try{
            ServerInit server = new MyFileServer();
            ServerInit stub = (ServerInit) UnicastRemoteObject.exportObject(server, 0);

            Registry registry = LocateRegistry.getRegistry();
            registry.bind("videoServer", stub);

        }catch (RemoteException e){
            e.printStackTrace();
        } catch (AlreadyBoundException e) {
            e.printStackTrace();
        }
    }


}

像这样运行它。

start java -classpath E:\Documents\Development\Projects\OOP3_Project1\bin\Server/ -Djava.rmi.codebase=file:/E:\Documents\Development\Projects\OOP3_Project1\bin\Server/ MyFileServer    

如果有人能提供帮助就会很棒

1 个答案:

答案 0 :(得分:3)

删除UnicastRemoteObject.exportObject()行。如果远程对象没有扩展UnicastRemoteObject,则只需要这样做。您可以绑定server而不是存根。