我编辑的测试类如下:
服务器类:News_register_factory import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;import News_apart_DAOImp.NAprt_affair_noticeDAOImp;
import News_apart_DAOImp.News_apart_DAOImp;
import News_aprt_DAO.News_register_factoryDAO;public class News_register_factory extends UnicastRemoteObject implements
News_register_factoryDAO,Runnable {
private String item;
protected News_register_factory() throws RemoteException {
super();
// TODO Auto-generated constructor stub
} public   boolean register()  throws RemoteException {
if(this.item == "newsDAOImp"){
    LocateRegistry.createRegistry(8808);
News_apart_DAOImp news = new News_apart_DAOImp();
try {
Naming.rebind("//localhost:8808/news" , news);
System.out.println("\n news registered");
//registry.unbind("news");
          /*  UnicastRemoteObject.unexportObject(registry, true);
            System.out.println(">>>>>INFO:服务停止!");
            System.exit(0);  //终止RMI服务程序的运行
            */
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();

return true;
}
if(this.item == "NApart_affairImp"){
LocateRegistry.createRegistry(8809);
NAprt_affair_noticeDAOImp affair = new NAprt_affair_noticeDAOImp();
try {
Naming.rebind("//localhost:8809/Naffair" , affair);
System.out.println("\n affair registered");
/* //registry.unbind("Naffair");
            UnicastRemoteObject.unexportObject(registry, true);
            System.out.println(">>>>>INFO:服务停止!");
            //System.exit(0);  //终止RMI服务程序的运行
             * 
             */
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();

return true;
}
if(this.item == "BApart_affairImp"){
LocateRegistry.createRegistry(8810);
NAprt_affair_noticeDAOImp affair = new NAprt_affair_noticeDAOImp();
try {
Naming.rebind("//localhost:8810/Baffair" , affair);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();

return true;
}
return false;
} public synchronized void  setItem(String it) throws RemoteException {
// TODO Auto-generated method stub
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.item = it;
} public void run() {
// TODO Auto-generated method stub
try {
 System.out.println(Thread.currentThread().getName() + " is saling ticket " + register()); } catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}其中Imp类及其stub全部部署在服务端本地。
客户端类:package LookUpImp;import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;import NewsDAO.News_register_factoryDAO;public class NLookUp {
public boolean lookup(String item) {
String url = "//localhost:8807/factory";
try {
News_register_factoryDAO factory = (News_register_factoryDAO) Naming
.lookup("demo");
if (item == "news") {
factory.setItem("news");
new Thread((Runnable) factory).start();
return true;
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NotBoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}}codebase设置的就是classPath
客户端提示
News_register_factoryDAO factory = (News_register_factoryDAO) Naming.lookup("demo");找不到 News_register_factory_stub
求解

解决方案 »

  1.   

    stub类要在客户端也保留一份。
      

  2.   

    老大,codebase就是让客户端可以找到stub才设置的,实际应用时候,怎么会在客户端也保留呢,这样还远程调用什么呀
      

  3.   

    知道了, NLookUp 需要加setSecurity...
      

  4.   

    不好意思,没注意看到下面这句话。误导LZ了。
    codebase设置的就是classPath
      

  5.   

    本地时候codebase是classPath这里并非设置path错误找不到,而是没有设置setSecurity...找到也没有访问权限,因此会返回这个错误。。
      

  6.   

    好久没用RMI了,学习了。你设置了System.setSecurityManager吧。