我有两个使用 RMI 连接的服务器,AServer 和 BServer。
AServer的相关代码是:
int port = Integer.parseInt("1234");
String url = "rmi://localhost:" + port + "/Aclass";
System.out.println("binding " + url);
Naming.rebind(url, new AClass());
BServer的相关代码是:
int port = Integer.parseInt("1234");
String url = "//localhost:" + port + "/Bclass";
System.out.println("binding " + url);
Naming.rebind(url, new BClass());
在 ArchLinux/CentOS 上,当我为 AServer 运行以下命令时:
java -cp ".:./protobuf.jar" -Djava.rmi.server.codebase=protobuf.jar -Djava.security.policy=policy AServer
以及BServer对应的代码:
java -cp ".:./protobuf.jar" -Djava.rmi.server.codebase=protobuf.jar -Djava.security.policy=policy BServer
对于 AServer,控制台打印:
binding rmi://localhost:1234/AServer
server rmi://localhost:1234/AServer is running...
BServer 打印:
binding rmi://localhost:1234/BServer
Place server failed:Error unmarshaling return; nested exception is:
java.net.MalformedURLException: no protocol: protobuf.jar
java.rmi.UnmarshalException: Error unmarshaling return; nested exception is:
java.net.MalformedURLException: no protocol: protobuf.jar
at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:247)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:379)
at sun.rmi.registry.RegistryImpl_Stub.rebind(Unknown Source)
at java.rmi.Naming.rebind(Naming.java:177)
at BServer.main(BServer.java:31)
Caused by: java.net.MalformedURLException: no protocol: protobuf.jar
at java.net.URL.<init>(URL.java:586)
at java.net.URL.<init>(URL.java:483)
at java.net.URL.<init>(URL.java:432)
at sun.rmi.server.LoaderHandler.pathToURLs(LoaderHandler.java:770)
at sun.rmi.server.LoaderHandler.getDefaultCodebaseURLs(LoaderHandler.java:141)
at sun.rmi.server.LoaderHandler.loadClass(LoaderHandler.java:170)
at java.rmi.server.RMIClassLoader$2.loadClass(RMIClassLoader.java:637)
这表明错误一定是在 Name.rebind() 命令中,但我很困惑,因为据我所知,URL 的格式是正确的。
两台服务器在 Mac 操作系统中都可以正常工作,运行命令: java -cp ".:./protobuf.jar"-Djava.security.policy=policy AServer
它们按照我想要的方式完美工作,但是在 ArchLinux/CentOS 中我需要添加
-Djava.rmi.server.codebase=... 或者我收到 NoClassDefFoundError。
最佳答案
代码库规范的元素是 URL,而不是文件名。您尚未指定协议(protocol)或绝对路径。
无论如何,这都行不通。 java.rmi.server.codebase
必须以对等方可以理解的形式表示。仅仅重复类路径元素是不够的。
关于java - ArchLinux/CentOS 中 RMI Naming.rebind() 中的 MalformedURLException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26839549/