代码之家  ›  专栏  ›  技术社区  ›  Conor T

客户端线程在联系RMI服务器java时失败。rmi。解组异常:错误解组返回[重复]

  •  0
  • Conor T  · 技术社区  · 7 年前

    我有一个客户端,它从JSP页面获取输入,然后将其提供给线程,线程被添加到线程池中,当它执行线程时,会联系RMI服务器以获取远程对象,然后应返回将显示在新JSP页面上的结果,问题是一旦执行可运行代码,我会得到以下错误:

    java.rmi.UnmarshalException: error unmarshalling return; nested exception is: 
        java.lang.ClassNotFoundException: servertest.DictionaryInterface (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.LookupWorker.run(LookupWorker.java:27)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at java.lang.Thread.run(Unknown Source)
    Caused by: java.lang.ClassNotFoundException: servertest.DictionaryInterface (no security manager: RMI class loader disabled)
        at sun.rmi.server.LoaderHandler.loadProxyClass(Unknown Source)Thread didnt finish its job properly, error: java.lang.NullPointerException
    

    下面是JSP页面背后的Java代码

    if (request.getParameter("word") != null) {
                String word = request.getParameter("word");
                String res;
                System.out.println("The word to find: "+word);
    
                // Get handle on our pool
                WorkerPool pool = WorkerPool.getInstance( );
                // Create Job
                LookupWorker wordWork = new LookupWorker(word);
                // Add job to pool
                res = pool.addJob(wordWork);
                request.getSession().setAttribute("result", res);
                response.sendRedirect("/SearchResults"); 
            }
    

    以下是线程类:

    package Client;
    
    import java.net.MalformedURLException;
    import java.rmi.Naming;
    import java.rmi.NotBoundException;
    import java.rmi.RemoteException;
    
    import DictionaryInterface.DictionaryInterface;
    
    public class LookupWorker implements WorkerPlan {
    
        private String word;
        private String jobName;
        private String serverResult;
        private long threadId;
    
        public LookupWorker(String w) {
            super();
            this.word = w;
            jobName = "Lookup Job";
        }
    
        @Override
        public void run(){
            DictionaryInterface Dictonary = null;
            try {
                Dictonary = (DictionaryInterface) Naming.lookup("rmi://127.0.0.1:1099/DictionaryService");
            } catch (MalformedURLException | RemoteException | NotBoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            try {
                serverResult = Dictonary.lookup(word);
            } catch (RemoteException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
                System.out.println(serverResult); 
                threadId = Thread.currentThread().getId();
        }
    
        public String getServerResult() {
            return serverResult;
        }
    
        public String getWord() {
            return word;
        }
    
        public String getJobName() {
            return jobName;
        }
    
        @Override
        public String toString() {
            return jobName + " [Id= " + threadId + "word= " + word + ", serverResult= " + serverResult + "]";
        }   
    
    }
    

    执行线程时涉及的池代码:

    public String addJob(WorkerPlan job) {
            String result;
            threadCounter++;
            // Adding new lookup job to our queue
            System.out.println("Adding a " + job.getJobName() + " to the queue. Searching for: " + job.getWord() + " Job number: " + threadCounter);
            executor.execute(job);
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } // sleep so we can wait for rmi object to come in
            System.out.println(job.toString());
            result = job.getServerResult();
            return result;
        }
    

    我为VM参数尝试了不同的方法,但无法实现,非常感谢您的帮助。

    1 回复  |  直到 7 年前
        1
  •  1
  •   user207421    7 年前

    联系RMI服务器时客户端线程失败:

    否。它在查找RMI注册表时失败。查看堆栈跟踪。

    java.rmi.UnmarshalException: error unmarshalling return
    

    查看堆栈跟踪。基本错误是

    java.lang.ClassNotFoundException: servertest.DictionaryInterface (no security manager: RMI class loader disabled)
    

    您尚未将此类部署到客户端。你似乎两者都有 servertest.DictionaryInterface DictionaryInterface.DictionaryInterface . 你不能那样做。包名称的两端必须相同。