Search code examples
javaconstructorrmi

no suitable constructor found for UnicastRemoteObject()


As of javase api 8, rmic is deprecated. So generating stubs dynamically is preferred by way of exporting objects as:

  1. Subclassing UnicastRemoteObject and calling the UnicastRemoteObject() constructor.
  2. Subclassing UnicastRemoteObject and calling the UnicastRemoteObject(port) constructor.

So I first called the UnicastRemoteObject(port) constructor but got compilation error: no suitable constructor found for UnicastRemoteObject(int) Then I tried calling UnicastRemoteObject() constructor and still got the same compilation error. What is possibly going wrong?

EDIT: My code:

import java.rmi.*;
import java.rmi.server.*;
public class MyServer extends UnicastRemoteObject implements MyRemote {
    MyServer()throws RemoteException {
        new UnicastRemoteObject(5000);
    }
    //Other methods...
}

Exact quoted error:

no suitable constructor found for UnicastRemoteObject(int) constructor java.rmi.server.UnicastRemoteObject.UnicastRemoteObject() is not applicable (actual and formal argument lists differ in length) constructor java.rmi.server.UnicastRemoteObject.UnicastRemoteObject(int,java.rmi.server.RMIClientSocketFactory,java.rmi.server.RMIServerSocketFactory) is not applicable (actual and formal argument lists differ in length)

My question is, when java.rmi.server.UnicastRemoteObject is present in javase8 api then why is the compiler giving error?


Solution

    1. Subclassing UnicastRemoteObject and calling the UnicastRemoteObject() constructor.

    Wrong. Doing that requires rmic.

    1. Subclassing UnicastRemoteObject and calling the UnicastRemoteObject(port) constructor.

    Correct. You've left out several cases:

    1. Subclassing UnicastRemoteObject and calling the UnicastRemoteObject(int, RMIClientSocketFactory, RMIServerSocketFactory) constructor (possibly it is the other way round).

    2. Not subclassing UnicastRemoteObject and calling the UnicastRemoteObject.exportObject(Remote, int) method.

    3. Not subclassing UnicastRemoteObject and calling the UnicastRemoteObject.exportObject(Remote, int, RMIClientSocketFactory, RMIServerSocketFactory) method (again it is possibly the other way round).

    EDIT

    MyServer()throws RemoteException {
        new UnicastRemoteObject(5000);
    }
    

    This is not how you call a base class constructor. The correct form is:

    MyServer() throws RemoteException {
        super(5000);
    }
    

    This is rather basic.