Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java RMI应用程序无法运行_Java_Rmi - Fatal编程技术网

Java RMI应用程序无法运行

Java RMI应用程序无法运行,java,rmi,Java,Rmi,我在NetBeans7.0.1(Win7+JDK7U1)中创建了一个简单的JavaRMI应用程序。我有两个独立的项目: RMI_客户端包含: MyClient类: package rmi_client; import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; public class MyClient { public static void main(String[] args)

我在NetBeans7.0.1(Win7+JDK7U1)中创建了一个简单的JavaRMI应用程序。我有两个独立的项目:

RMI_客户端包含:

MyClient类:

package rmi_client;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class MyClient 
{
     public static void main(String[] args) 
     {
          String msg = "Hello RMI";
          rmiInterface stub;
          Registry reg;

          try
          {
               reg = LocateRegistry.getRegistry("localhost");
               stub = (rmiInterface) reg.lookup("Hello");

               try
               {
                    stub.hello(msg);
               }
               catch(Exception e)
               {
                    System.out.println("Remote method exception thrown: " +e.getMessage());   
               }

          }
          catch(Exception e)
          {
               System.err.println("Client exception thrown: " + e.toString());
               e.printStackTrace();
          }
     }
 }
RMI接口:

package rmi_client;
import java.rmi.Remote;
import java.rmi.RemoteException;

public interface rmiInterface extends Remote 
{
     void hello(String message) throws RemoteException;
}
RMI_服务器包含:

RMI接口-与上面完全相同

RMIMPL类:

package rmi_server;
import java.rmi.RemoteException;

public class rmiImpl implements rmiInterface 
{
     @Override
     public void hello(String message) throws RemoteException 
     {
          System.out.println("Server:" + message);
     }
}
MyServer类:

package rmi_server;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;

public class MyServer   
{
     public static void main(String[] args) 
     {
          try
          {
               rmiImpl robj = new rmiImpl();
               rmiInterface stub = (rmiInterface) UnicastRemoteObject.exportObject(robj, 0);
               Registry reg = LocateRegistry.createRegistry(1099);
               reg.rebind("Hello", stub);
               System.out.println("Server is ready to listen: ");
          } 
          catch (Exception e)
          {
               System.err.println("Server exception thrown: " + e.toString());
          }

     }
}
如果我做错了以上的事情,请让我知道。首先启动RMI_服务器应用程序,然后在运行RMI_客户端时出现错误:

Client exception thrown: java.rmi.UnmarshalException: error unmarshalling return; nested exception is: 
    java.lang.ClassNotFoundException: rmi_server.rmiInterface (no security manager: RMI class loader disabled)
java.rmi.UnmarshalException: error unmarshalling return; nested exception is: 
    java.lang.ClassNotFoundException: rmi_server.rmiInterface (no security manager: RMI class loader disabled)
    at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
    at rmi_client.MyClient.main(MyClient.java:28)
Caused by: java.lang.ClassNotFoundException: rmi_server.rmiInterface (no security manager: RMI class loader disabled)
    at sun.rmi.server.LoaderHandler.loadProxyClass(LoaderHandler.java:554)
    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:257)
    at java.io.ObjectInputStream.readProxyDesc(ObjectInputStream.java:1549)
    at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1511)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1750)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1347)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369)
    ... 2 more
问题在哪里

RMI_服务器包含:

RMI接口-与上面完全相同

不,不是。此消息:

java.lang.ClassNotFoundException: rmi_server.rmiInterface
表示
rmi接口
位于
rmi\u服务器
包中,该包与上面的包不同

它必须是完全相同的类。不同包中没有类似的类

因此,您需要三个包:服务器、客户端和共享。共享包需要包含远程接口及其依赖的任何应用程序类

RMI_服务器包含:

RMI接口-与上面完全相同

不,不是。此消息:

java.lang.ClassNotFoundException: rmi_server.rmiInterface
表示
rmi接口
位于
rmi\u服务器
包中,该包与上面的包不同

它必须是完全相同的类。不同包中没有类似的类


因此,您需要三个包:服务器、客户端和共享。共享包需要包含远程接口及其依赖的任何应用程序类。

但是,当客户端和服务器在两台不同的机器上工作时,如何解决该问题?@user1052836共享的.jar文件必须部署到两台机器上。我创建了包含rmi接口的rmi_shared.jar,并将其添加到两个项目中。现在应用程序可以工作了,但我在RMI_服务器netbeans控制台的“服务器已准备好侦听:”下看到了一个消息“Server:Hello RMI”。消息“Server:Hello RMI”应该在RMI_客户端netbeans控制台中打印。我认为来自RMIMPL的函数hello应该返回一些值(在本例中为字符串),然后我的客户端可以打印一条消息(System.out.println(remoteObject.hello(msg));@user1052836完全正确。远程方法在服务器上执行,这就是它们的全部意义。没有合理的理由期望服务器的控制台输出神奇地出现在客户端。但是当客户端和服务器在两台不同的机器上工作时,如何解决这个问题呢?@user1052836共享的.jar文件必须部署到两台机器上。I cr创建了包含rmi接口的rmi_shared.jar,我将其添加到两个项目中。现在应用程序可以运行了,但我在rmi_服务器netbeans控制台中的“服务器已准备好侦听:”.消息“服务器:Hello rmi”应该在RMI_客户机netbeans控制台中打印。因此出现了一些问题。我认为来自RMIMPL的函数hello应该返回一些值(在本例中为字符串),然后我的客户机可以打印消息(System.out.println(remoteObject.hello(msg));@user1052836完全正确。远程方法在服务器上执行,这就是它们的全部意义。没有合理的理由期望服务器的控制台输出神奇地出现在客户端。