JAVA RMI无法远程调用
我把一个RMI的CLIENT调用类放在SERVER的工程里执行可以正常调用,但是如果把它拷到一个新项目中却无法执行,代码如下:Javacode//服务器代码package...
我把一个RMI的CLIENT调用类放在SERVER的工程里执行可以正常调用,但是如果把它拷到一个新项目中却无法执行,代码如下:
Java code
//服务器代码
package server;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface SayHello extends Remote {// 需要从Remote继承
public String sayHello(String info) throws RemoteException; // 需要抛出remote异常n;
}
package server;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class SayHelloImpl extends UnicastRemoteObject implements SayHello {
protected SayHelloImpl() throws RemoteException {
super();
// TODO Auto-generated constructor stub
}
public String sayHello(String info) throws RemoteException {
return "hello=" + info;
}
}
package server;
import java.rmi.*;
import java.net.*;
public class SayRmiServer {
public SayRmiServer() {
}
public static void main(String args[]) throws RemoteException,
MalformedURLException {
SayHelloImpl add = new SayHelloImpl();
Naming.rebind("RMI_Test_2", add);
}
}
//客户端代码,接口文件就不重复拷了
package client;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import server.SayHello;
public class SayRmiClient {
public SayRmiClient() {
}
public static void main(String args[]) throws RemoteException,
MalformedURLException, NotBoundException {
String url = "rmi://110.64.89.140" +
"/RMI_Test_2";
SayHello add = (SayHello) Naming.lookup(url);
String result = add.sayHello("hubin");
System.out.println(result);
}
}
执行后抛出如下异常:
Exception in thread "main" java.rmi.UnmarshalException: error unmarshalling return; nested exception is:
java.lang.ClassNotFoundException: server.SayHello (no security manager: RMI class loader disabled)
at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
at java.rmi.Naming.lookup(Unknown Source)
at client.SayRmiClient.main(SayRmiClient.java:22)
Caused by: java.lang.ClassNotFoundException: server.SayHello (no security manager: RMI class loader disabled)
at sun.rmi.server.LoaderHandler.loadProxyClass(Unknown Source)
at java.rmi.server.RMIClassLoader$2.loadProxyClass(Unknown Source)
at java.rmi.server.RMIClassLoader.loadProxyClass(Unknown Source)
at sun.rmi.server.MarshalInputStream.resolveProxyClass(Unknown Source)
at java.io.ObjectInputStream.readProxyDesc(Unknown Source)
at java.io.ObjectInputStream.readClassDesc(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
... 3 more 展开
Java code
//服务器代码
package server;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface SayHello extends Remote {// 需要从Remote继承
public String sayHello(String info) throws RemoteException; // 需要抛出remote异常n;
}
package server;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class SayHelloImpl extends UnicastRemoteObject implements SayHello {
protected SayHelloImpl() throws RemoteException {
super();
// TODO Auto-generated constructor stub
}
public String sayHello(String info) throws RemoteException {
return "hello=" + info;
}
}
package server;
import java.rmi.*;
import java.net.*;
public class SayRmiServer {
public SayRmiServer() {
}
public static void main(String args[]) throws RemoteException,
MalformedURLException {
SayHelloImpl add = new SayHelloImpl();
Naming.rebind("RMI_Test_2", add);
}
}
//客户端代码,接口文件就不重复拷了
package client;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import server.SayHello;
public class SayRmiClient {
public SayRmiClient() {
}
public static void main(String args[]) throws RemoteException,
MalformedURLException, NotBoundException {
String url = "rmi://110.64.89.140" +
"/RMI_Test_2";
SayHello add = (SayHello) Naming.lookup(url);
String result = add.sayHello("hubin");
System.out.println(result);
}
}
执行后抛出如下异常:
Exception in thread "main" java.rmi.UnmarshalException: error unmarshalling return; nested exception is:
java.lang.ClassNotFoundException: server.SayHello (no security manager: RMI class loader disabled)
at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
at java.rmi.Naming.lookup(Unknown Source)
at client.SayRmiClient.main(SayRmiClient.java:22)
Caused by: java.lang.ClassNotFoundException: server.SayHello (no security manager: RMI class loader disabled)
at sun.rmi.server.LoaderHandler.loadProxyClass(Unknown Source)
at java.rmi.server.RMIClassLoader$2.loadProxyClass(Unknown Source)
at java.rmi.server.RMIClassLoader.loadProxyClass(Unknown Source)
at sun.rmi.server.MarshalInputStream.resolveProxyClass(Unknown Source)
at java.io.ObjectInputStream.readProxyDesc(Unknown Source)
at java.io.ObjectInputStream.readClassDesc(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
... 3 more 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询