views:

39

answers:

1

I've wrote simple RMI code to learn how it works. While everything works fine compiling java code, rmic it and run rmiregistry and run client code from console works fine, but when I choose to "run as Java application" on the client code from Eclipse, it throws MarshallException.

My guess is somewhat Eclipse is compiling with different version according to:

http://stackoverflow.com/questions/1578434/java-rmi-marshalexception

I've checked the Eclipse compiler setting and it says 1.6. (right click on Project Explorer and choose "project facets" then you will see what version the Eclipse is compiling the java files)

My Java is 1.6 so regardless of compiling the code from console and Eclipse should result in 1.6 version complied class files. (javap -verbose | grep -i version brings up version 50 which is equivalent to 1.6 )

Has anyone encountered same issue and has explanation for this?

Likely not necessary but the working code is below.

Client code

package com.masatosan.remote.client;

import java.rmi.Naming;
import java.rmi.RemoteException;

import com.masatosan.remote.server.MyRemoteServer;

public class MyRemoteClient  {

    public static void main(String[] args) {
        new MyRemoteClient().go();
    }

    public void go() {
        try {
            MyRemoteServer service = (MyRemoteServer) Naming.lookup("rmi://127.0.0.1/MyFirstRemoteService");
            String s = service.sayHello();

            System.out.println(s);
        }
        catch(Exception ex) {
            ex.printStackTrace();
        }
    }
}

Server code

package com.masatosan.remote.server;

import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class MyRemoteServerImp extends UnicastRemoteObject implements MyRemoteServer {

    public static void main(String[] args) {
        try {
            MyRemoteServer service = new MyRemoteServerImp();
            Naming.rebind("MyFirstRemoteService", service);
        }
        catch(Exception ex) {
            ex.printStackTrace();
        }
    }

    public MyRemoteServerImp() throws RemoteException {}

    @Override
    public String sayHello() throws RemoteException {
        return "Hi I'm server!";
    }
}//

Server code interface for stub

package com.masatosan.remote.server;

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

public interface MyRemoteServer extends Remote {

    public String sayHello() throws RemoteException;
}
+1  A: 

The version in question is not the version of the JRE but the version of the classes being called. This means your client is calling a different set of jars in eclipse. In general if eclipse and the console are giving different results it's almost always a classpath issue.

Check the 'Java Build Path' section of your project and all projects that are related to make sure everything matches up. Keep an eye out for the jar that has 'com.masatosan.remote.server.MyRemoteServer', I believe that is the one that is having the version issue. The jar that eclipse is pointing to might be different and/or stale. Here are some places to check in the build path section of your project

1)Make sure you are using the 'projects' tab correctly.

2)Check to make sure all the jars on the 'libraries' tab are the same as when compiled on the console

3)Make sure that that none of the jars that should be included by the 'projects' tab are in the 'libraries' tab. I have seen many people tare their hair out because eclipse was using jars that were only updated if they compiled from the command line.

4)Play around with the 'Order and Export' tab. If something is above something else it gets loaded first. The version that is loaded first is the version that is used. If both your command line app and eclipse project have the same jars but include them in a different order you can have these issues.

5)Some jars can be installed directly in to the JRE by including them in the JREs directory. This is HIGHLY unlikely but you can check the JRE eclipse is using and make sure it's the same one as the console.

Good luck, classpath problems are very tricky.

AmaDaden