我不明白如何使用代码库。
我有以下结构:
CLIENT:
Client.java
client.policy
SERVER:
Server.java
Hello.java
HelloInterfaces.java
HelloInterfaces.java
public interface HelloInterfaces extends Remote {
}
你好.java
public class Hello extends UnicastRemoteObject implements HelloInterfaces{
public Hello() throws RemoteException{
}
}
服务器.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");
}
}
客户端.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);
}
}
客户政策
grant {
permission java.security.AllPermission;
};
直接从 Netbeans 在服务器(带有 Linux 的虚拟机)上执行: 输出服务器:准备就绪!
在客户端,我使用选项编译了源代码--classpath .:Server.jar(其中 Server.jar 包含 HelloInterfaces.class)。
执行客户端:
输出客户端: 发现注册表! 1 你好 (跑步......)
两分钟后:
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
此外,在包含 的目录服务器上.class,我在端口 8080 上运行了一个 python httpserver。(与 NanoHTTP 相同的结果,用 java 编写的 http 服务器)
应用程序客户端联系 http 服务器,因为 python httpserver 输出:
192.168.1.24 - - “GET / HTTP/1.1” 200 -