Search code examples
javarminetbeans-7

RMI Exception RemoteException nested exception


Trying to run RMI example but facing the following exception:

java.rmi.ServerException: RemoteException occurred in server thread; nested exception is: 
    java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is: 
    java.lang.ClassNotFoundException: hejsan.RemoteBuffer

I have started the rmiregristry and it seems to be running fine. But the server is not running, it just builds and stops. /Library/Java/JavaVirtualMachines/jdk1.7_45.jdk/Contents/Home/jre/bin/rmiregistry

Project name: hej
Package name: hejsan
Files: MyBuffer.java and RemoteBuffer.java

MyBuffer.java:

package hejsan;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.LinkedList;

@SuppressWarnings("serial")
public class MyBuffer extends UnicastRemoteObject implements RemoteBuffer {

    LinkedList<Integer> list = new LinkedList<Integer>();

    public MyBuffer() throws RemoteException, MalformedURLException {
        super();
        Naming.rebind("rmi://localhost/buffer", this);
    }

    public synchronized void put(Integer i) throws RemoteException {
        list.addLast(i);
        notifyAll();
    }

    public synchronized Integer get() throws RemoteException {
        while (list.size() == 0) {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return (Integer) list.removeFirst();
    }

    public static void main(String[] args) {
        try {
            new MyBuffer();
        } catch (RemoteException re) {
            System.out.println(re);
            System.exit(1);
        } catch (MalformedURLException me) {
            System.out.println(me);
            System.exit(1);
        }
    }
}

RemoteBuffer.java:

package hejsan;

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface RemoteBuffer extends Remote {
    void put(Integer i) throws RemoteException;
    Integer get() throws RemoteException;
}

Solution

  • I discovered that I had to be on netbeans project path: ~/build/classes and runs the rmiregistry from while on that path.

    The rmiregistry path can be found under Tools-> Java Platforms in netbeans. Caution! Change to jre instead of jdk following this below example:
    /Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home/jre/bin/rmiregistry