Search code examples
javarmi

ClassNotFoundException in RMI Java


I can't make compilator finds the class I want Exactly this: ctx.rebind("MyInterfaceImplementacja", ref);. I Could you please correct me?

package Pakiet;
import java.rmi.Remote;
import java.rmi.RemoteException;

public interface MyInterface extends Remote {
public String echo(String napis) throws RemoteException;
public int dodaj(int wrt1, int wrt2) throws RemoteException;
}

package Pakiet;

import java.rmi.RemoteException;
import javax.rmi.PortableRemoteObject;

public class MyInterfaceImplementacja extends PortableRemoteObject implements MyInterface {
  protected MyInterfaceImplementacja() throws RemoteException {
    super();
  }

  @Override
  public String echo(String napis) throws RemoteException {
    return "echo" + napis;
  }

  @Override
  public int dodaj(int wrt1, int wrt2) throws RemoteException {
    return wrt1 + wrt2;
  }
}

public class MyInterfaceSerwer {
  public static void main(String[] args) {
    try{
      MyInterfaceImplementacja ref = new MyInterfaceImplementacja();
      Context ctx = new InitialContext();
      ctx.rebind("MyInterfaceImplementacja", ref);      
    }catch(Exception e){e.printStackTrace();}
  }
}

Solution

  • Try this

    1. Extend the Implementation class to UnicastRemoteObject instead of PortableRemoteObject.

    2. use rmi in your rebind method like this

      ctx.rebind("rmi:MyInterfaceImplementacja", ref);

    Keep this below guidelines for future use

    down vote

    If you are trying to implement the RMI. Please follow these steps Try this.... On Server Side:

    1. Create Interface which extends java.rmi.Remote interface // shared by client and server

    2. Create Implementation class of the Interface, and also extend it to UnicastRemoteObject

    3. Now create the Server class which has the rebind method.

      On Client Side:

    4. Create Interface which extends java.rmi.Remote interface // shared by client and server

    5. Create the Client class which has the lookup method.

      eg: On SERVER

      Car.java // Interface

      CarImpl.java // Implementation Class

      CarServer.java // Server Class

      On CLIENT

      Car.java // Interface

      CarClient.java // Client Class