简体   繁体   中英

ClassNotFoundException with RMI

i wrote a program in netbeans with RMI that client has error

error :

java.rmi.UnmarshalException: error unmarshalling return; nested exception is: java.lang.ClassNotFoundException: rmiserver.Message (no security manager: RMI class loader disabled) at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)

but sever does not any error!

interfaace code:

 package rmiclient;

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

public interface Message extends Remote {
    void sayHello(String name) throws RemoteException;
}

interface implementation is:

 package rmiserver;

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

public class MessageImpl extends UnicastRemoteObject implements Message {

    public MessageImpl() throws RemoteException {       
    }

    @Override
    public void sayHello(String name) throws RemoteException {
        System.out.println("hello "+name);
    }

}

server code is:

package rmiserver;  
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class Main {

    private void startServer(){
        try {
            // create on port 1099
            Registry registry = LocateRegistry.createRegistry(1099);

            // create a new service named myMessage
            registry.rebind("myMessage", new MessageImpl());
        } catch (Exception e) {
            e.printStackTrace();
        }     
        System.out.println("system is ready");
    }

    public static void main(String[] args) {
        Main main = new Main();
        main.startServer();
    }
}

client code is:

package rmiclient;
import java.rmi.RMISecurityManager;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class Main {

    private void doTest(){

        try {

            // fire to localhost port 1099
            Registry myRegistry = LocateRegistry.getRegistry("127.0.0.1", 1099);


            // search for myMessage service
            Message impl = (Message) myRegistry.lookup("myMessage");

            // call server's method        
            impl.sayHello("edwin");

            System.out.println("Message Sent");
        } catch (Exception e) {
            e.printStackTrace();
        }       
    }

    public static void main(String[] args) {
        Main main = new Main();
        main.doTest();
    }
}

thanks :).

In your stacktrace:

java.lang.ClassNotFoundException: rmiserver.Message

but as per data you have provided, your Message interface is declated with package package rmiclient; and You haven't created any rmiserver.Message class.

Correct the package name.

I have two package. rmiclient and rmiserver. that in rmiclient are "message" and "main" . in rmiserver are "message" and "main" and "messageimpl"

That's your problem. This doesn't satisfy the contract. You can't just copy Message to another package and expect to have it be treated the same as the original. The remote stub implements the same remote interfaces that the remote object does. Not another interface with the same name in another package.

You have to deploy rmiserver.Message to the client. Just like the error message says, really.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM