6 Replies Latest reply on Aug 16, 2010 10:52 PM by EJP

    New to Java RMI

      I am having problems with the following code that I am currently trying to understand RMI from Java head First, the following are meant to be part of an universal browser that the browser downloads and displays interactive Java GUIs. Can someone explain what I am doing wrong as I am still new to Java please?
      import java.awt.*;
      import javax.swing.*;
      import java.rmi.*;
      import java.awt.event.*;
      public class ServiceBrowser {
         JPanel mainPanel;
         JComboBox serviceList;
         ServiceServer server;
         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);
            frame.getContentPane().add(BorderLayout.NORTH, serviceList);
            serviceList.addActionListener(new MyListListener());     
         void loadService(Object serviceSelection) {
             try {
                Service svc = server.getService(serviceSelection);
              } catch(Exception ex) {
         Object[] getServicesList() {
            Object obj = null;
            Object[] services = null;
            try {
                obj = Naming.lookup("rmi://");    
           catch(Exception ex) {
           server = (ServiceServer) obj;
            try {
              services = server.getServiceList();
            } catch(Exception ex) {
           return services;
         class MyListListener implements ActionListener {
            public void actionPerformed(ActionEvent ev) { 
                // do things to get the selected service
                Object selection =  serviceList.getSelectedItem();
        public static void main(String[] args) {
           new ServiceBrowser().buildGUI();
      I am able to compile the code but come up with the following error messages at runtime

      java.rmi.ConnectException: Connection refused to host:; nested exception is:
           java.net.ConnectException: Connection refused
           at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:601)
           at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:198)
           at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:184)
           at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:322)
           at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
           at java.rmi.Naming.lookup(Naming.java:84)
           at ServiceBrowser.getServicesList(ServiceBrowser.java:53)
           at ServiceBrowser.buildGUI(ServiceBrowser.java:19)
           at ServiceBrowser.main(ServiceBrowser.java:82)
      Caused by: java.net.ConnectException: Connection refused
           at java.net.PlainSocketImpl.socketConnect(Native Method)
           at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
           at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
           at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
           at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:432)
           at java.net.Socket.connect(Socket.java:529)
           at java.net.Socket.connect(Socket.java:478)
           at java.net.Socket.<init>(Socket.java:375)
           at java.net.Socket.<init>(Socket.java:189)
           at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:22)
           at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:128)
           at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:595)
           ... 8 more
           at ServiceBrowser.getServicesList(ServiceBrowser.java:64)
           at ServiceBrowser.buildGUI(ServiceBrowser.java:19)
           at ServiceBrowser.main(ServiceBrowser.java:82)
      Exception in thread "main" java.lang.NullPointerException
           at javax.swing.DefaultComboBoxModel.<init>(DefaultComboBoxModel.java:53)
           at javax.swing.JComboBox.<init>(JComboBox.java:175)
           at ServiceBrowser.buildGUI(ServiceBrowser.java:21)
           at ServiceBrowser.main(ServiceBrowser.java:82)

      The code for the remote implementation compile and runs, but the other code for services compiles but come back with the following error message at runtime:

      Exception in thread "main" java.lang.NoSuchMethodError: main

      I have included one of the services code below this happens with:
      import javax.swing.*;
      import java.awt.event.*;
      import java.io.*;
      public class DiceService implements Service {
          JLabel label;
          JComboBox numOfDice;
          public JPanel getGuiPanel() {
             JPanel panel = new JPanel();
             JButton button = new JButton("Roll 'em!");
             String[] choices = {"1", "2", "3", "4", "5"};
             numOfDice = new JComboBox(choices);
             label = new JLabel("dice values here");
             button.addActionListener(new RollEmListener());
             return panel;
         public class RollEmListener implements ActionListener {
            public void actionPerformed(ActionEvent ev) {
               // roll the dice
               String diceOutput = "";
               String selection = (String)  numOfDice.getSelectedItem();
               int numOfDiceToRoll = Integer.parseInt(selection);
               for (int i = 0; i < numOfDiceToRoll; i++) {
                  int r = (int) ((Math.random() * 6) + 1);
                  diceOutput += (" " + r);
        • 1. Re: New to Java RMI
          java.rmi.ConnectException: Connection refused to host:
          This means there isn't a server running on that host which will respond to your request. So what are you doing wrong? You aren't running a suitable server.
          • 2. Re: New to Java RMI
            how I do get suitable server running, can I not test the code on a local ip address first, i have included the remote implementation code below, can you advise how I can resolve this please or point me in the right direction?
            import java.rmi.*;
            import java.util.*;
            import java.rmi.server.*;
            public class ServiceServerImpl extends UnicastRemoteObject implements ServiceServer  {
                HashMap<String, Service> serviceList;
                public ServiceServerImpl() throws RemoteException {
                   // start and set up services 
               private void setUpServices() {
                   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());   
                public Object[] getServiceList() {
                   System.out.println("in remote");
                   return serviceList.keySet().toArray();
                public Service getService(Object serviceKey) throws RemoteException {        
                   Service theService = (Service) serviceList.get(serviceKey);       
                   return theService;
                public static void main (String[] args) {
                   try {
                     Naming.rebind("ServiceServer", new ServiceServerImpl());
                    } catch(Exception ex) { }
                    System.out.println("Remote service is running");
            • 3. Re: New to Java RMI
              Well, you could start by asking in the right place.

              While you may be new to Java, RMI is not something Java newbies regularly tackle. And in my opinion if you really are new to Java you should concentrate on learning Java first and advanced things like RMI later once you have a good grasp on Java.

              I know this isn't the answer you are looking for, but you should restate your problem in the proper forum (in this case the RMI forum on these very boards). Please note I said restate. This is VERY IMPORTANT! If you simply cut and past the same question there as you have here, you WILL be outed as a double poster, and no one likes double posters. So to avoid that, restate your question, utalizing what you have learned here, and if you are really smart and savy you will provide a link to this thread in your new thread.

              Mods, please lock or move this thread to RMI area for "better topic alignment".

              • 4. This Thread is now moved
                Note: This thread was originally posted in the [New To Java|http://forums.sun.com/forum.jspa?forumID=54] forum, but moved to this forum for closer topic alignment.
                • 5. Re: This Thread is now moved
                  Look at that fast service by the mods, now you don't even have to worry about restating it.

                  Good luck.

                  • 6. Re: This Thread is now moved
                    The Registry isn't running in the localhost.

                    Probably you should be looking in some other host, if that's where the server is.