0

I wrote a simple implementation class:

public interface MyRemote extends Remote {
    public String sayHello() throws RemoteException;
}

Then a remote server class

public class MyRemoteImpl extends UnicastRemoteObject implements MyRemote{

    protected MyRemoteImpl() throws RemoteException {

    }

    public String sayHello() throws RemoteException {
        return "Server says 'Hello World!'";
    }

    public static void main(String args[]) {
        try {
            MyRemote service = new MyRemoteImpl();
            Naming.rebind("RemoteHello", service);
        } catch (RemoteException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (MalformedURLException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }
}

Now I read the rmiregistry needs to be started in the same path where the original classes are (and their stubs) so I just compiled using intllij IDEA and auto generated the Stub classes, then copied all the classes to my java path that has the rmiregistry.exe file and then ran the rmiregistry (I know this must be a pretty dumb method but I was just trying to get it to work as a test). I still get an error as listed at the bottom of this post whenever I try and compile the server file.

The book I am working from says to use java's rmic command to generate the stubs and skeletons but I can't find this file (maybe it's from an older version of JAVA?) so I'm just using Intellij IDEA to generate the stub file instead...but no skeleton file is generated.

I'm sure there are several things about RMI I don't understand but I looked up some tutorials online and can't figure out what it is, thought maybe someone on stackoverflow could point me in the right direction. Any help would be much appreciated.

java.rmi.ServerError: Error occurred in server thread; nested exception is: java.lang.UnsupportedClassVersionError: MyRemote : Unsupported major.minor version 51.0 at sun.rmi.server.UnicastServerRef.oldDispatch(Unknown Source) at sun.rmi.server.UnicastServerRef.dispatch(Unknown Source) at sun.rmi.transport.Transport$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at sun.rmi.transport.Transport.serviceCall(Unknown Source) at sun.rmi.transport.tcp.TCPTransport.handleMessages(Unknown Source) at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(Unknown Source) at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source) at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:273) at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:251) at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:377) at sun.rmi.registry.RegistryImpl_Stub.rebind(Unknown Source) at java.rmi.Naming.rebind(Naming.java:177) at MyRemoteImpl.main(MyRemoteImpl.java:27) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

Anthony
  • 2,411
  • 6
  • 26
  • 23

3 Answers3

3

I've ran into the same problem and spent quite some time trying to figure out what went wrong. The same UnsupportedClassVersionError was thrown from the server thread, it perplexed me as to why the exception was thrown at the naming.rebind() line. Both the implementation and interface of my RMI are compiled using the same compiler and settings as the server's application.

Turns out that the rmiregistry version that I was using is of a lower version (1.6) than the compilation of my jars (1.7). I don't even know how/why the exception was thrown in the server thread instead of being displayed at the terminal that runs rmiregistry.

Conclusion: If you run rmiregistry from terminal, type "man rmiregistry" to figure out what version your default rmiregistry is meant for, make sure that the class included in rmiregistry's classpath are compiled in that same version.

Keith Lee
  • 31
  • 2
  • Not sure what the last instruction means, but on Ubuntu make sure the correct version is chosen when using `sudo update-alternatives --config rmiregistry` – mz496 Feb 22 '16 at 09:47
3

1) Generate stubs

After compiling we have two classes:

    d:\htdocs\java\NetBeansProjects\RMIserver\build\classes\rmi\

        MyRemote.class
        MyRemoteImpl.class
Run rmic

    rmic -classpath d:\htdocs\java\NetBeansProjects\RMIserver\build\classes\  
                 -d d:\htdocs\java\NetBeansProjects\RMIserver\build\classes\rmi\ 
                    rmi.MyRemoteImpl

The rmic tool creates a new class:

    /build/classes/rmi/rmi/MyRemoteImpl_Stub.class

2) Start rmiregistry

Be sure you start it from a directory that has access to your classes.

    cd d:\htdocs\java\NetBeansProjects\RMIserver\build\classes\

    %rmiregistry

5) Start the service (command or IDE)

%java MyRemoteImpl
catalinp
  • 131
  • 2
  • 5
2

The error basically means you have compiled the classes in a higher version of Java than you are running them on. Make sure you are running the same JRE version or make sure you compile them against the version you want to run them on (using -target option of javac)

Read this article for more details.

Aravind Yarram
  • 78,777
  • 46
  • 231
  • 327