Я создал простое приложение Java RMI в netbeans 7.0.1 (win 7 + jdk 7u1). У меня есть два отдельных проекта:
RMI_Client содержит:
Класс MyClient:
package rmi_client;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class MyClient
{
public static void main(String[] args)
{
String msg = "Hello RMI";
rmiInterface stub;
Registry reg;
try
{
reg = LocateRegistry.getRegistry("localhost");
stub = (rmiInterface) reg.lookup("Hello");
try
{
stub.hello(msg);
}
catch(Exception e)
{
System.out.println("Remote method exception thrown: " +e.getMessage());
}
}
catch(Exception e)
{
System.err.println("Client exception thrown: " + e.toString());
e.printStackTrace();
}
}
}
Интерфейс rmiInterface:
package rmi_client;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface rmiInterface extends Remote
{
void hello(String message) throws RemoteException;
}
RMI_Server содержит:
Интерфейс rmiInterface - точно такой же, как и выше
Класс rmiImpl:
package rmi_server;
import java.rmi.RemoteException;
public class rmiImpl implements rmiInterface
{
@Override
public void hello(String message) throws RemoteException
{
System.out.println("Server:" + message);
}
}
Класс MyServer:
package rmi_server;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
public class MyServer
{
public static void main(String[] args)
{
try
{
rmiImpl robj = new rmiImpl();
rmiInterface stub = (rmiInterface) UnicastRemoteObject.exportObject(robj, 0);
Registry reg = LocateRegistry.createRegistry(1099);
reg.rebind("Hello", stub);
System.out.println("Server is ready to listen: ");
}
catch (Exception e)
{
System.err.println("Server exception thrown: " + e.toString());
}
}
}
Если я делаю что-то не так, пожалуйста, дайте мне знать. Сначала я запускаю приложение RMI_Server, затем при запуске RMI_Client я получаю ошибки:
Client exception thrown: java.rmi.UnmarshalException: error unmarshalling return; nested exception is:
java.lang.ClassNotFoundException: rmi_server.rmiInterface (no security manager: RMI class loader disabled)
java.rmi.UnmarshalException: error unmarshalling return; nested exception is:
java.lang.ClassNotFoundException: rmi_server.rmiInterface (no security manager: RMI class loader disabled)
at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
at rmi_client.MyClient.main(MyClient.java:28)
Caused by: java.lang.ClassNotFoundException: rmi_server.rmiInterface (no security manager: RMI class loader disabled)
at sun.rmi.server.LoaderHandler.loadProxyClass(LoaderHandler.java:554)
at java.rmi.server.RMIClassLoader$2.loadProxyClass(RMIClassLoader.java:646)
at java.rmi.server.RMIClassLoader.loadProxyClass(RMIClassLoader.java:311)
at sun.rmi.server.MarshalInputStream.resolveProxyClass(MarshalInputStream.java:257)
at java.io.ObjectInputStream.readProxyDesc(ObjectInputStream.java:1549)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1511)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1750)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1347)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369)
... 2 more
Где проблема?