-1

I can not understand how to use the codebase.

I have the following structure:

CLIENT:
Client.java
client.policy

SERVER:
Server.java
Hello.java
HelloInterfaces.java

HelloInterfaces.java

public interface HelloInterfaces extends Remote {
}

Hello.java

public class Hello extends UnicastRemoteObject implements HelloInterfaces{

    public Hello() throws RemoteException{
    }

}

Server.java

public class Server {

    public static void main(String[] arg) throws RemoteException {
        Registry reg = LocateRegistry.createRegistry(1099);
        reg.rebind("Hello", new Hello());
        System.out.println("Ready");
    }
}

Client.java

public class Client {

    public static void main(String[] args) throws RemoteException, NotBoundException, MalformedURLException {
  String host="192.168.1.227";  //IP SERVER
        System.setProperty("java.rmi.server.codebase", "http://" + host + ":8080");
        System.setProperty("java.security.policy", "client.policy");
        System.setSecurityManager(new RMISecurityManager());
        Registry reg = LocateRegistry.getRegistry(host);
        System.out.println("Regitry Found!");
        System.out.println(reg.list().length);
        System.out.println(reg.list()[0]);
        HelloInterfaces h;
        h = (HelloInterfaces) reg.lookup("Hello");
        System.out.println(h);
    }
}

client.policy

grant {
permission java.security.AllPermission;
};

Execute on server (Virtual Machine with Linux) directly from Netbeans: Output Server: Ready!

On the client, I have compiled the source with option --classpath .:Server.jar (where Server.jar contains HelloInterfaces.class).

execute Client:

  Output Client:
  Registry Found!
  1
  Hello
  (Running......)

and after two minutes:

Exception in thread "main" java.rmi.UnmarshalException: error unmarshalling return; nested exception is: 
    java.lang.ClassNotFoundException: HelloInterfaces
    at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
    at Client.main(Client.java:36)
Caused by: java.lang.ClassNotFoundException: HelloInterfaces
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.rmi.server.LoaderHandler$Loader.loadClass(LoaderHandler.java:1206)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:270)
    at sun.rmi.server.LoaderHandler.loadClassForName(LoaderHandler.java:1219)
    at sun.rmi.server.LoaderHandler.loadProxyInterfaces(LoaderHandler.java:729)
    at sun.rmi.server.LoaderHandler.loadProxyClass(LoaderHandler.java:673)
    at sun.rmi.server.LoaderHandler.loadProxyClass(LoaderHandler.java:610)
    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:255)
    at java.io.ObjectInputStream.readProxyDesc(ObjectInputStream.java:1558)
    at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1514)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1771)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1350)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:370)
    ... 2 more

Also, on directory server that contains .class, I run a python httpserver on port 8080. (same result with a NanoHTTP, http server written in java)

The app client contacts the http server because python httpserver output:

192.168.1.24 - -  "GET / HTTP/1.1" 200 -
Stackuser
  • 140
  • 1
  • 12
  • maybe I am not clear a concept ... in this case, specifying the properties of the codebase, as the class in the classpath local HelloInterfaces is not present, it should download automatically from the code base that is located on the server machine? – Stackuser May 07 '14 at 00:18

1 Answers1

1

A codebase is a list of URLs of JAR files or directories that contain package-structured hierarchies of .class files. It doesn't seem likely that a host-and-port-only URL meets that description.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • but I'm using as a reference to a codebase url to a httpserver. – Stackuser May 05 '14 at 22:59
  • maybe I am not clear a concept ... in this case, specifying the properties of the codebase, as the class in the classpath local HelloInterfaces is not present, it should download automatically from the code base that is located on the server machine? – Stackuser May 07 '14 at 00:19
  • A type that is mentioned by name in a class must be present on the CLASSPATH when the class is loaded. You can use the codebase feature for many things but not for types that are named in your code. Your new stacktrace shows this clearly. The exception wasn't thrown from `lookup():` it was thrown when the class was first loaded. – user207421 May 07 '14 at 01:14
  • the exception is thrown during the execution of lookup(). it is allowable to do something like: java -cp .:http://ipserver:port/ client.Client and so, the output is: Exception in thread "main" javax.naming.CommunicationException [Root exception is java.rmi.UnmarshalException: error unmarshalling return; nested exception is: java.lang.ClassNotFoundException: base.HelloInterfaces] at com.sun.jndi.rmi.registry.RegistryContext.lookup(RegistryContext.java:118) – Stackuser May 07 '14 at 13:31