我正在使用RMI创建客户端和服务器应用程序,以便我的服务器将自定义GUI发送到客户端应用程序。我的客户端,服务器和rmiregistry在不同的桌面上但在同一台PC上打开。我的客户端似乎无法连接到服务器,并不断给我我不理解的异常。
这是《 Head First Java Book》中的最后一个项目,我收到了一些我不理解的异常。我尝试使用Google搜索解决方案,但找不到任何帮助。我不知道客户端是否无法连接到服务器,因为客户端源代码没有正确地在rmiregistry上查找存根,或者客户端在执行RMI查找时无法访问存根类。另外,我研究了什么是“ NotBoundException”,但我得出的结论是,存根无法连接到远程服务器。
摘要:
这是服务器代码:
import java.rmi.*;
import java.util.*;
import java.rmi.server.*;
public class RemoteServer extends UnicastRemoteObject implements RemoteInterface {
HashMap servicelist;
public RemoteServer () throws RemoteException {
setUpServices ();
}
//SET UP SERVICES METHOD
private void setUpServices () { //THIS METHOD PUTS THE DIFFERENT GUI CLASS IN A HASHMAP
servicelist = new HashMap <String, Service> ();
servicelist.put ("Dice Rolling Service", new DiceService ());
servicelist.put("Day of the week service", new DayOfTheWeekService ());
servicelist.put("Visual Music Service", new MiniMusicService ());
}
//GET SERVICE LIST METHOD
public Object [] getServiceList () throws RemoteException { //THIS METHOD IS CALLED WHEN THE CLIENT WANTS TO DISPLAY A LIST OF GUIS IN THE BROWSER
System.out.println ("in Remote");
return servicelist.keySet().toArray();
}
//GET SERVICE METHOD
public Service getService (Object serviceKey) throws RemoteException { //THIS METHOD IS CALLED AFTER THE USER SELECTS A GUI FROM THE DISPLAYED LIST OF GUIS ON THE BROWSER
Service theService = (Service) servicelist.get(serviceKey);
return theService;
}
public static void main (String [] args) {
try {
Naming.rebind("RemoteServer", new RemoteServer ()); //CREATE THE REMOTE OBJECT THEN BIND IT TO THE RMIREGISTRY
} catch (Exception ex) {
ex.printStackTrace ();
}
System.out.println("Remote Service is running");
}
}
这是客户端代码:
import java.awt.*;
import javax.swing.*;
import java.rmi.*;
import java.awt.event.*;
public class Client {
JPanel mainPanel;
JComboBox serviceList;
ServiceServer server;
//BUILDGUI METHOD
public void buildGUI () {
JFrame frame = new JFrame ("RMI Browser");
mainPanel = new JPanel ();
frame.getContentPane().add(BorderLayout.CENTER, mainPanel);
Object [] services = getServicesList();
serviceList = new JComboBox (services); //ADD THE GUIS TO THE JCOMBOBOX (WHICH IS THE DISPLAYABLE LIST)
frame.getContentPane().add(BorderLayout.NORTH, serviceList);
serviceList.addActionListener (new MyListListener ());
frame.setSize (500, 500);
frame.setVisible (true);
}
//LOADSERVICE METHOD
void loadService (Object serviceSelection) { //THIS METHOD ADDS THE SELECTED GUI TO THE BROWSER
try {
Service svc = server.getService(serviceSelection);
mainPanel.removeAll();
mainPanel.add(svc.getGuiPanel ());
mainPanel.validate ();
mainPanel.repaint ();
} catch (Exception ex) {
ex.printStackTrace ();
}
}
//GETSERVICESLIST METHOD
Object [] getServicesList () { //THIS METHOD DOES THE RMI LOOKUP, GETS THE STUB AND CALLS THE GETSERVICELIST ()
Object obj = null;
Object [] services = null;
try {
obj = Naming.lookup("rmi://127.0.0.1/RemoteInterface"); //DOES THE RMI LOOKUP AND GETS THE STUB
}
catch (Exception ex) {
ex.printStackTrace();
}
server = (RemoteInterface) obj; //CAST THE STUB TO THE REMOTE INTERFACE SO THAT WE CAN CALL GETSERVICELIST ()
try {
service = server.getServiceList(); //THIS GIVES US THE ARRAY OF OBJECTS (WHICH ARE THE GUIS) THAT WILL BE DISPLAYED IN THE JCOMBOBOX FOR THE USER TO SELECT FROM
} catch (Exception ex) {
ex.printStackTrace();
}
return service;
}
//MYLISTLISTENER INNER CLASS
class MyListListener implements ActionListener {
public void actionPerformed (ActionEvent ev) {
Object selection = serviceList.getSelectedItem();
loadService(selection); //AFTER THE USER HAS MADE A SELECTION FROM THE JCOMBOBOX, TAKE THE SELECTION THEY MADE & LOAD THE APPROPIATE GUI
}
}
//MAIN METHOD
public static void main (String [] args) {
new Client().buildGUI();
}
}
这是RemoteInterface界面:
import java.rmi.*;
public interface RemoteInterface extends Remote { //THIS INTERFACE DEFINES TWO METHODS THAT THE REMOTE SERVER WILL IMPLEMENT
Object [] getServiceList () throws RemoteException;
Service getService (Object serviceKey) throws RemoteException;
}
这些是我运行客户端类时遇到的异常:
java.rmi.NotBoundException: serviceServer
at sun.rmi.registry.RegistryImpl.lookup(RegistryImpl.java:227)
at sun.rmi.registry.RegistryImpl_Skel.dispatch(RegistryImpl_Skel.java:115)
at sun.rmi.server.UnicastServerRef.oldDispatch(UnicastServerRef.java:468)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:300)
at sun.rmi.transport.Transport$1.run(Transport.java:200)
at sun.rmi.transport.Transport$1.run(Transport.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:573)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:834)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:688)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:687)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(Unknown Source)
at sun.rmi.transport.StreamRemoteCall.executeCall(Unknown Source)
at sun.rmi.server.UnicastRef.invoke(Unknown Source)
at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
at java.rmi.Naming.lookup(Unknown Source)
at ServiceBrowser.getServicesList(ServiceBrowser.java:46)
at ServiceBrowser.buildGUI(ServiceBrowser.java:16)
at ServiceBrowser.main(ServiceBrowser.java:69)
java.lang.NullPointerException
at ServiceBrowser.getServicesList(ServiceBrowser.java:54)
at ServiceBrowser.buildGUI(ServiceBrowser.java:16)
at ServiceBrowser.main(ServiceBrowser.java:69)
Exception in thread "main" java.lang.NullPointerException
at javax.swing.DefaultComboBoxModel.<init>(Unknown Source)
at javax.swing.JComboBox.<init>(Unknown Source)
at ServiceBrowser.buildGUI(ServiceBrowser.java:18)
at ServiceBrowser.main(ServiceBrowser.java:69)
在此问题上待了几天,我真的很乐意提供任何帮助。感谢您抽出宝贵的时间阅读本文。