Amy Teresa Hyland
Amy Teresa Hyland

Reputation: 79

RMI Object already exported

I am having an issue trying to get RMI working. I have the registry running, when I try start the server a window pops up for less than a second and then closes. Why is this happening.

Server package --- 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();
        }
    }


}

Running it like so.

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

If someone could help would be great

Upvotes: 1

Views: 7594

Answers (1)

user207421
user207421

Reputation: 311039

Remove the UnicastRemoteObject.exportObject() line. You only need that if your remote object doesn't extend UnicastRemoteObject. You can bind server instead of the stub.

Upvotes: 6

Related Questions