Question

I want a RMI server / client system where you can browse the files on the server with a client. In this case the server is a Debian and the client runs on Windows.

I tried to have the server hold a File object pointing to the currently seen directory and show all files in that directory in a List on the client.

The problem is, when I call a method returning me file.listFiles(), I don't get the files on the server but on the client or FileNotFoundException as if the server would run on the client. The Java File API seems to use the root directories on the computer where the client runs on and not the server.

Simpler said: I want a file explorer on the client showing me the server file system.

Edit:

public class ClientMain {

    /**
     * @param args
     */
    public static void main(String[] args) {
        if (System.getSecurityManager() == null) {
            System.setSecurityManager(new SecurityManager());
        }
        View view = new View();
        view.setVisible(true);
        try {
            String name = "Remote";
            Registry registry = LocateRegistry.getRegistry("127.0.0.1");
            IRemote model = (IRemote) registry.lookup(name);
            view.setModel(model);
            view.update();
        } catch (Exception e) {
            System.err.println("Remote Exception");
            e.printStackTrace();
        }
    }

}

public class View extends JFrame implements IView{
    JList list;
    IRemote model;
    public View() {
        super();
        this.setVisible(true);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        list = new JList();
        this.add(list);
    }
    public IRemote getModel() {
        return model;
    }
    public void setModel(IRemote model) {
        this.model = model;
    }

    public void update(){
        try {
            this.list.setListData(model.getFileList());
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}


public interface IRemote extends Remote {
    public String[] getFileList() throws RemoteException;
}

public class Model implements IRemote{
    File current;

    public Model() {
        super();
        current = new File(".");
    }

    public String[] getFileList() {
        return current.list();
    }

    public void setCurrentDirectory(String current) {
        this.current = new File(current);
    }




}
public class ServerMain {
    public static void main(String[] args) {
        new ServerMain();
    }

    public ServerMain() {
        super();
        Model model = new Model();
        if (System.getSecurityManager() == null) {
            System.setSecurityManager(new SecurityManager());
        }
        try {
            String name = "Remote";
            IRemote stub = (IRemote) UnicastRemoteObject.exportObject(model, 0);
            Registry registry = LocateRegistry.getRegistry();
            registry.rebind(name, stub);
        } catch (Exception e) {
            System.err.println("Controller exception:");
            e.printStackTrace();
        }
    }


}

This is what i am trying to do. Here the Server binds a Model to the Registry. The Client looks up the Model give the Model to the View and the view calls getFileList() from the Model. With the "." File I get the Directory where the Programm is located. Since it's relative I get all the File on the Client where the Client Programm is running. If I use a non-relative Directory I get the FileNotFoundException because the Client does not have this path. Hope that makes it even clearer.

Was it helpful?

Solution

File is not a remote object. Whatever it tells the server applies at the server. You can ship it to the client via RMI as it is serializable, and it doesn't take any external state with it such as the state of the server's file system.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top