'Eclipse 4.23.0 Connection refused to host: localhost; nested exception is: java.net.ConnectException: Connection refused: connect

I'm working through the proxy pattern section in Head First Design Patterns 2nd edition, by Freeman and Robson. I'm running Windows 10 and I've determined that localhost is functioning properly as the Windows IIS screen comes up when i navigate to localhost or http://127.0.0.1/. rmiregistry is started beforehand from the cmd line with no issue. I'm running the test with the firewall turned off and I'm still getting the error stack trace listed below. I'm executing the GumballMachineTestDrive class directly in Eclipse by right clicking on the .java file and selecting "Run As" "Java Application". The screenshot below shows the run configurations for GumballMachineTestDrive, where args[0] == "localhost" and args 1 == 112. I've pinged localhost and 127.0.0.1, both of which show no issues. I previously tried to set the rmi server hostname as can be seen in the line "System.setProperty("java.rmi.server.hostname", "127.0.0.1");", however this did not make a difference. Any guidance in the right direction would be greatly appreciated.

GumballMachineTestDrive-Run Configurations

Service code:

package GumballMachineStatePattern;

import java.rmi.Naming;

public class GumballMachineTestDrive {

    public static void main(String[] args) {
        GumballMachineRemote gumballMachine = null;
        
    //  System.setProperty("java.rmi.server.hostname", "127.0.0.1");
        
        int count;
        
        if (args.length < 2) {
            System.out.println("GumballMachine <name> <inventory>");
            System.exit(1);
        }
        
        try {
        
        count = Integer.parseInt(args[1]);
        
        gumballMachine = new GumballMachine(args[0], count);
        Naming.rebind("//" + args[0] + "/gumballmachine", gumballMachine);
        
        } catch (Exception e) {
            e.printStackTrace();
        }
}
}

Code for GumballMachine class:

package GumballMachineStatePattern;
import java.rmi.*;
import java.rmi.server.*;

public class GumballMachine extends UnicastRemoteObject implements GumballMachineRemote {
    
    private static final long serialVersionUID = 2L;

State soldOutState;
State noQuarterState;
State hasQuarterState;
State soldState;
State winnerState;

State state = soldOutState;
int count = 0;

String location;
    
    public GumballMachine(String location, int count) throws RemoteException {
        soldOutState = new SoldOutState(this);
        noQuarterState = new NoQuarterState(this);
        hasQuarterState = new HasQuarterState(this);
        soldState = new SoldState(this);
        winnerState = new WinnerState(this);
        
        this.location = location;
        
        this.count = count;
        if (count > 0) {
            state = noQuarterState;
        } else {
            state = soldOutState;
        }
    }
    
    public void insertQuarter() {
        state.insertQuarter();
    }
    
    public void ejectQuarter() {
        state.ejectQuarter();
    }
    
    public void turnCrank() {
        state.turnCrank();
        state.dispense();
    }
    
    void setState(State state) {
        this.state = state;
    }
    
    void releaseBall() {
        System.out.println("A gumball comes rolling out of the slot...");
        if (count > 0) {
            count = count - 1;
        }
    }
    
    public int getCount() {
        return count;
    }
    
    void refill(int count) {
        this.count += count;
        System.out.println("The gumball machine was just refilled; its new count is: " + this.count);
        state.refill();
    }
    
    public State getState() {
        return state;
    }
    
    public State getSoldOutState() {
        return soldOutState;
    }
    
    public State getNoQuarterState() {
        return noQuarterState;
    }
    
    public State getHasQuarterState() {
        return hasQuarterState;
    }
    
    public State getSoldState() {
        return soldState;
    }
    
    public State getWinnerState() {
        return winnerState;
    }
    
    public String getLocation() {
        return location;
    }
        
    public String toString() {
        StringBuffer result = new StringBuffer();
        result.append("\nMighty Gumball, Inc.");
        result.append("\nJave-enabled Standing Gumball Model #2004\n");
        result.append("Inventory: " + count + " gumball");
        
        if (count != 1) {
            result.append("s");
        }
        result.append("\n");
        result.append("Machine is " + state + "\n");
        
        return result.toString();
    }
}

GumballMachineTestDrive stack trace where args[0] == "localhost" and args1 = 112

java.rmi.ConnectException: Connection refused to host: localhost; nested exception is: 
    java.net.ConnectException: Connection refused: connect
    at java.rmi/sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:626)
    at java.rmi/sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:217)
    at java.rmi/sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:204)
    at java.rmi/sun.rmi.server.UnicastRef.newCall(UnicastRef.java:344)
    at java.rmi/sun.rmi.registry.RegistryImpl_Stub.rebind(RegistryImpl_Stub.java:150)
    at java.rmi/java.rmi.Naming.rebind(Naming.java:177)
    at GumballMachineStatePattern.GumballMachineTestDrive.main(GumballMachineTestDrive.java:24)
Caused by: java.net.ConnectException: Connection refused: connect
    at java.base/sun.nio.ch.Net.connect0(Native Method)
    at java.base/sun.nio.ch.Net.connect(Net.java:579)
    at java.base/sun.nio.ch.Net.connect(Net.java:568)
    at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:588)
    at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327)
    at java.base/java.net.Socket.connect(Socket.java:633)
    at java.base/java.net.Socket.connect(Socket.java:583)
    at java.base/java.net.Socket.<init>(Socket.java:507)
    at java.base/java.net.Socket.<init>(Socket.java:287)
    at java.rmi/sun.rmi.transport.tcp.TCPDirectSocketFactory.createSocket(TCPDirectSocketFactory.java:40)
    at java.rmi/sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:620)
    ... 6 more


Solution 1:[1]

I solved the problem. I needed to re-do the project as a fresh java project to clear up what were probably some dependency conflicts. It also served as a good learning lesson rmi and xampp local server configuration as well.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 jlakes85