MrAlex
MrAlex

Reputation: 1

JAVA RMI problems on eclipse

I'm trying to run a simple Java RMI on my eclipse, but every time I get different errors. If I run my sources through CMD (I'm on Windows) it works, but I'd prefer to run my code on Eclipse since it's more comfortable!

This is the code:

Interface

package rmiproject;

import java.rmi.*;

public interface HelloIn extends Remote {

    public String ciao(String name) throws RemoteException;
}

Server

package rmiproject;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class Server extends UnicastRemoteObject implements HelloIn {

    protected Server() throws RemoteException {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * 
     */
    @Override
    public String ciao(String name) throws RemoteException {
        // TODO Auto-generated method stub
        String output= "Server dice ciao "+name;
        return output;
    }

    private static final long serialVersionUID = 1L;

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.setProperty("java.rmi.server.hostname","myipaddress");
        try {
            Naming.rebind("//localhost/Server", new Server());
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

Client

package rmiproject;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;

public class Client {
    private static HelloIn lookup;

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try {
            lookup= (HelloIn) Naming.lookup("//localhost/Server");
            String s= lookup.ciao("peppe");
            System.out.println(s);
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NotBoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

This is the error I get if I run it on Eclipse:

Exception in thread "main" java.lang.reflect.InaccessibleObjectException: Unable to make public abstract java.lang.String rmiproject.HelloIn.ciao(java.lang.String) throws java.rmi.RemoteException accessible: module Rmi does not "exports rmiproject" to module java.rmi
    at java.base/java.lang.reflect.AccessibleObject.throwInaccessibleObjectException(AccessibleObject.java:391)
    at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:367)
    at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:315)
    at java.base/java.lang.reflect.Method.checkCanSetAccessible(Method.java:203)
    at java.base/java.lang.reflect.Method.setAccessible(Method.java:197)
    at java.rmi/sun.rmi.server.UnicastServerRef$HashToMethod_Maps$1.run(UnicastServerRef.java:598)
    at java.rmi/sun.rmi.server.UnicastServerRef$HashToMethod_Maps$1.run(UnicastServerRef.java:596)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:319)
    at java.rmi/sun.rmi.server.UnicastServerRef$HashToMethod_Maps.computeValue(UnicastServerRef.java:595)
    at java.rmi/sun.rmi.server.UnicastServerRef$HashToMethod_Maps.computeValue(UnicastServerRef.java:574)
    at java.rmi/sun.rmi.server.WeakClassHashMap.get(WeakClassHashMap.java:74)
    at java.rmi/sun.rmi.server.UnicastServerRef.exportObject(UnicastServerRef.java:236)
    at java.rmi/java.rmi.server.UnicastRemoteObject.exportObject(UnicastRemoteObject.java:473)
    at java.rmi/java.rmi.server.UnicastRemoteObject.exportObject(UnicastRemoteObject.java:358)
    at java.rmi/java.rmi.server.UnicastRemoteObject.<init>(UnicastRemoteObject.java:230)
    at java.rmi/java.rmi.server.UnicastRemoteObject.<init>(UnicastRemoteObject.java:212)
    at Rmi/rmiproject.Server.<init>(Server.java:10)
    at Rmi/rmiproject.Server.main(Server.java:29)

When I run it on my CMD, these are the steps I follow:

javac rmiproject/Server.java
java rmiproject.Server

Same for the client on a different window.

Upvotes: 0

Views: 427

Answers (2)

Mirjana Mirjana
Mirjana Mirjana

Reputation: 1

// add this in module-info.java
module NameOfTheJavaProject {
    exports nameOfThePackage; // package where classes use java.rmi
    requires java.rmi;
}

It worked for me

Upvotes: 0

MrAlex
MrAlex

Reputation: 1

Fixed in this way :

  • added exports rmiproject(name of the package) to module-info.java
  • start rmiregistry from cmd (in your src path)

Upvotes: 0

Related Questions