gpt4 book ai didi

java - RMI 类未找到异常

转载 作者:行者123 更新时间:2023-12-02 08:37:33 28 4
gpt4 key购买 nike

尝试在 Netbeans 6.5/windows xp 中撤销远程方法时,在 RMI 中遇到 UnmarshalException ClassNotFoundException。 rmi客户端和服务器程序都运行在同一台机器上。

RMI 客户端程序:

源代码位置

  • F:\NewRMIClient\src\newrmiclient\RMIClient.java

  • F:\NewRMIClient\src\newrmiclient\RemoteInterface.java

类文件位置

  • F:\NewRMIClient\build\classes\newrmiclient\RemoteInterface.class

  • F:\NewRMIClient\build\classes\newrmiclient\RMIClient.class

构建的 JAR 文件位置

  • F:\NewRMIClient\dist\NewRMIClient.jar

    RemoteInterface.java

    封装 newrmiclient;

    导入java.rmi.Remote; 导入java.rmi.RemoteException;

    公共(public)接口(interface)RemoteInterface扩展了Remote { void sendMessageFilePath(String messagePath) 抛出 RemoteException; }

RMIClient.java

package newrmiclient;

import java.rmi.*;
import java.rmi.registry.*;

public class RMIClient
{
static public void main(String args[])
{
RemoteInterface rmiServer;
Registry registry;
String serverAddress="192.169.4.11";
String serverPort="3232";
String text="hello";
System.out.println("sending "+text+" to "+serverAddress+":"+serverPort);

System.setProperty("java.security.policy","file:/F:/PolicyFiles/rules.policy");

if(System.getSecurityManager() == null)
{
System.setSecurityManager(new RMISecurityManager());
}

try{
// get the “registry”
registry=LocateRegistry.getRegistry(serverAddress,(new Integer(serverPort)).intValue());
// look up the remote object
rmiServer=(RemoteInterface)(registry.lookup("MessagePath"));
// call the remote method
rmiServer.sendMessageFilePath(text);
}
catch(RemoteException e){
e.printStackTrace();
}
catch(NotBoundException e){
e.printStackTrace();
}
}
}

RMI 服务器程序:

源代码位置

  • F:\RMIProj\src\rmiproj\RemoteInterface.java

  • F:\RMIProj\src\rmiproj\RmiServer.java

类文件位置

  • F:\RMIProj\build\classes\rmiproj\RemoteInterface.class

  • F:\RMIProj\build\classes\rmiproj\RmiServer.class

  • F:\RMIProj\build\classes\rmiproj\RmiServer_Skel.class

  • F:\RMIProj\build\classes\rmiproj\RmiServer_Stub.class

构建的 JAR 位置

  • F:\RMIProj\dist\RMIProj.jar

RemoteInterface.java

 package rmiproj;

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface RemoteInterface extends Remote
{
void sendMessageFilePath(String messagePath) throws RemoteException;
}

RmiServer.java

package rmiproj;

import java.rmi.RMISecurityManager;
import java.rmi.server.UnicastRemoteObject;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class RmiServer extends UnicastRemoteObject implements RemoteInterface
{
int thisPort = 3232;

Registry registry; // rmi registry for lookup the remote objects.

public RmiServer() throws RemoteException
{
try
{
System.setProperty("java.rmi.server.codebase","file:/F:/RMIProj/");
System.setProperty("java.rmi.server.hostname","RMIServer");
System.setProperty("java.security.policy","file:/F:/PolicyFiles/rules.policy");

if(System.getSecurityManager() == null)
{
System.setSecurityManager(new RMISecurityManager());
}

// create the registry and bind the name and object.
registry = LocateRegistry.createRegistry( thisPort );
registry.rebind("MessagePath", this);
}
catch(RemoteException re)
{
re.printStackTrace();
}
}

// This method is called from the remote client by the RMI.
// This is the implementation of the “RemoteInterface”.
public void sendMessageFilePath(String messageFilePath) throws RemoteException
{
System.out.println(messageFilePath);
}

public static void main(String args[]) throws RemoteException
{
RmiServer s=new RmiServer();
}
}

策略文件的位置: F:\PolicyFiles\rules.policy

规则.政策 授予 { 权限 java.security.AllPermission;};

当尝试从客户端调用远程方法时,控制台上抛出以下异常:

sending hello to 192.169.4.11:3232
java.rmi.UnmarshalException: error unmarshalling return; nested exception is:
java.lang.ClassNotFoundException: rmiproj.RemoteInterface
at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
at newrmiclient.RMIClient.main(RMIClient.java:28)
Caused by: java.lang.ClassNotFoundException: rmiproj.RemoteInterface
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:247)
at sun.rmi.server.LoaderHandler.loadProxyInterfaces(LoaderHandler.java:711)
at sun.rmi.server.LoaderHandler.loadProxyClass(LoaderHandler.java:655)
at sun.rmi.server.LoaderHandler.loadProxyClass(LoaderHandler.java:592)
at java.rmi.server.RMIClassLoader$2.loadProxyClass(RMIClassLoader.java:628)
at java.rmi.server.RMIClassLoader.loadProxyClass(RMIClassLoader.java:294)
at sun.rmi.server.MarshalInputStream.resolveProxyClass(MarshalInputStream.java:238)
at java.io.ObjectInputStream.readProxyDesc(ObjectInputStream.java:1531)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1493)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1732)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)

请帮我解决这个问题。

最佳答案

您需要在客户端 JVM 中提供可用的 rmiproj.RemoteInterface 类。尝试将 rmiproj.RemoteInterface 包含在 NewRMIClient.jar(或客户端类路径中包含的另一个 JAR)中。

另请注意,您调用的远程对象实现 rmiproj.RemoteInterface 而不是 newrmiclient.RemoteInterface。这意味着即使在客户端 JAR 中使用 rmiproj.RemoteInterface,您也需要对代码进行一些工作才能正常工作。 rmiServer 需要为 rmiproj.RemoteInterface 类型。

关于java - RMI 类未找到异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1081712/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com