服务器:
package server;
import java.rmi.*;
public interface iCSServer extends Remote
{
public int Findnumber(int num) throws RemoteException;
}
package server;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.UnicastRemoteObject;
和
public class CSServer extends UnicastRemoteObject implements iCSServer {
private static final long serialVersionUID = 01; // version 01
int N=1000; // number of ids
int[] Ids=new int[N]; // data structure for storing the Ids
public CSServer() throws RemoteException
{
}
public void Add_Ids() // add ids to data
{
for(int i=0;i<N;i++)
{
Ids[i]=i+ (i+1)*10;
}
}
public int Findnumber(int num) throws RemoteException
{
for(int i=0;i<N;i++)
{
if(num==Ids[i])
{
return i;
}
}
return -1;
}
public static void main(String[] args) throws RemoteException {
System.setProperty("java.rmi.server.hostname","127.0.0.1");
LocateRegistry.createRegistry(1099);
// System.out.println(LocateRegistry.getRegistry(1099).toString());
CSServer obj=new CSServer();
obj.Add_Ids();
try {
Naming.rebind ("CSServer", obj);
System.out.println ("CS218 Server is running.");
}
catch (Exception e) {
System.out.println ("CS218 server cannot run: " + e);
}
}
}
服务器启动并运行良好。
客户端:
package client;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.*;
public interface iCSServer extends Remote
{
public int Findnumber(int num) throws RemoteException;
}
并且
package client;
import java.rmi.Naming;
import java.rmi.registry.*;
public class clientApp {
public static void main(String[] args)
{
try{
iCSServer obj = (iCSServer) Naming.lookup("//localhost:1099/CSServer");
//Registry registry = LocateRegistry.getRegistry("127.0.0.1");
//iCSServer obj=(iCSServer) registry.lookup("CSServer");
System.out.println("pass");
System.out.println(obj.Findnumber(100));
}
catch(Exception ex)
{
System.out.println("fail");
System.out.println(ex.getMessage());
}
}
}
但是,当我运行客户端时,我收到此错误:
fail
error unmarshalling return; nested exception is:
java.lang.ClassNotFoundException: server.iCSServer (no security manager: RMI class loader disabled)
然后我添加了一个jar的版本的server.ICSServer,我相信修复了这个问题。但是,我再次运行它,现在我收到了这个错误:
fail
com.sun.proxy.$Proxy0 cannot be cast to client.iCSServer
关于如何修复的任何想法?
答案 0 :(得分:0)
您已将界面重命名为不同的包。必须是一样的。