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;
}