15125

Jetty Server not starting: Unable to establish loopback connection

so I am trying to setup an embedded jetty 9 server. However when I try:

mvn clean compile exec:java

The execution halts at :

INFO:oejs.Server:com.main.SimpleServer.main(): jetty-9.0.0.RC2

After a while I get exceptions like:

WARN:oejuc.AbstractLifeCycle:com.main.SimpleServer.main(): FAILED org.eclipse.jetty.io.SelectorManager$ManagedSelector@45a0110e keys=-1 selected=-1: java.io.IOException: Unable to establish loopback connection

java.io.IOException: Unable to establish loopback connection java.net.ConnectException: Connection refused: connect

My code looks like this:

public class HelloHandler extends AbstractHandler { final String _greeting; final String _body; public HelloHandler() { _greeting = "Hello World"; _body = null; } public HelloHandler(String greeting) { _greeting = greeting; _body = null; } public HelloHandler(String greeting, String body) { _greeting = greeting; _body = body; } public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html;charset=utf-8"); response.setStatus(HttpServletResponse.SC_OK); baseRequest.setHandled(true); response.getWriter().println("<h1>" + _greeting + "</h1>"); if (_body != null) response.getWriter().println(_body); } }

and

public class SimpleServer { public static void main(String[] args) throws Exception { Server server = new Server(8080); server.setHandler(new HelloHandler()); server.start(); server.join(); } }

my pom.xml looks like:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>TestJetty</groupId> <artifactId>TestJetty</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <jettyVersion>9.0.0.RC2</jettyVersion> </properties> <dependencies> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-server</artifactId> <version>${jettyVersion}</version> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-websocket</artifactId> <version>7.4.4.v20110707</version> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-util</artifactId> <version>${jettyVersion}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>3.0-alpha-1</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>${jettyVersion}</version> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.1</version> <executions> <execution><goals><goal>java</goal></goals></execution> </executions> <configuration> <mainClass>com.main.SimpleServer</mainClass> </configuration> </plugin> </plugins> </build> </project>

Answer1:

This means something about your localhost loopback is either misconfigured (such as reporting as a non loopback IP address), or being prevented from being used by something (most common cause being microsoft windows policy decisions and firewall rules).

Less common is a machine that does not have IPv4 available (only IPv6).

Try compiling and running this...

package stackoverflow.jetty;

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;

public class AddrList
{
    public static void main(String[] args)
    {
        try
        {
            InetAddress loopback = InetAddress.getLoopbackAddress();
            dump("Loopback",loopback);
        }
        catch (Throwable t)
        {
            System.out.println("Unable to getLoopbackAddress()");
            t.printStackTrace();
        }

        try
        {
            InetAddress localhost = InetAddress.getLocalHost();
            dump("LocalHost",localhost);
        }
        catch (Throwable t)
        {
            System.out.println("Unable to getLocalHost()");
            t.printStackTrace();
        }

        try
        {
            InetAddress alladdr = InetAddress.getByName("0.0.0.0");
            dump("0.0.0.0",alladdr);
        }
        catch (Throwable t)
        {
            System.out.println("Unable to getLocalHost()");
            t.printStackTrace();
        }

        try
        {
            Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
            for (NetworkInterface iface : Collections.list(nets))
            {
                try
                {
                    System.out.println("DisplayName = " + iface.getDisplayName());
                    System.out.println("Name = " + iface.getName());

                    List<InetAddress> addrs = Collections.list(iface.getInetAddresses());
                    int i = 0;
                    for (InetAddress addr : addrs)
                    {
                        dump(Integer.toString(i++),addr);
                    }
                }
                catch (Throwable t)
                {
                    System.out.println("Unable to InetAddress for NetworkInterface: " + iface.getDisplayName());
                    t.printStackTrace();
                }
            }
        }
        catch (SocketException e)
        {
            System.out.print("Unable to get all network interfaces");
            e.printStackTrace();
        }
    }

    public static void dump(String type, InetAddress addr)
    {
        String header = String.format("[%s] InetAddress",type);
        try
        {
            System.out.println(header + " = " + addr);
            System.out.println(header + ".isAnyLocalAddress = " + addr.isAnyLocalAddress());
            System.out.println(header + ".isLinkLocalAddress = " + addr.isLinkLocalAddress());
            System.out.println(header + ".isLoopbackAddress = " + addr.isLoopbackAddress());
        }
        catch (Throwable t)
        {
            System.out.printf("[%s] Failed to list InetAddress details%n",type);
            t.printStackTrace();
        }

        try
        {
            header = String.format("[%s] InetSocketAddress",type);
            InetSocketAddress isockaddr = new InetSocketAddress(addr,8080);
            System.out.println(header + " = " + isockaddr);
            System.out.println(header + ".isUnresolved = " + isockaddr.isUnresolved());
        }
        catch (Throwable e)
        {
            e.printStackTrace();
        }
    }
}


This should list all variations of Network Interfaces on your machine. If you have an error or exception you will need to address that first, before you can start Jetty on that machine.

On a normal machine you see the following output:

[Loopback] InetAddress = localhost/127.0.0.1 [Loopback] InetAddress.isAnyLocalAddress = false [Loopback] InetAddress.isLinkLocalAddress = false [Loopback] InetAddress.isLoopbackAddress = true [Loopback] InetSocketAddress = localhost/127.0.0.1:8080 [Loopback] InetSocketAddress.isUnresolved = false [LocalHost] InetAddress = lapetus/127.0.1.1 [LocalHost] InetAddress.isAnyLocalAddress = false [LocalHost] InetAddress.isLinkLocalAddress = false [LocalHost] InetAddress.isLoopbackAddress = true [LocalHost] InetSocketAddress = lapetus/127.0.1.1:8080 [LocalHost] InetSocketAddress.isUnresolved = false [0.0.0.0] InetAddress = /0.0.0.0 [0.0.0.0] InetAddress.isAnyLocalAddress = true [0.0.0.0] InetAddress.isLinkLocalAddress = false [0.0.0.0] InetAddress.isLoopbackAddress = false [0.0.0.0] InetSocketAddress = /0.0.0.0:8080 [0.0.0.0] InetSocketAddress.isUnresolved = false (snip ... long list of other interfaces)

This shows me that all 3 choices: loopback, localhost, and 0.0.0.0 (any interface) work as possible listeners for Jetty.

Lastly, upgrade to final non-RC Jetty 9.0.0.v20130308.

Answer2:

Jetty server was configured and working for me. on one fine day it start giving "Unable to establish loopback connection" error. I have restated my machine and it starts working again .So I think this error is totally related to windows operating system.

Recommend

  • Giving File permissions and running ProcessBuilder in Java Servlets
  • Which additional libraries are required for client compression?
  • Created a maven project using quickstart using intelliJ, didn't seem to generate any dir struct
  • Maven - Transitive dependencies are not resolved for artifact deployed on Artifactory
  • multi artifact id in one pom
  • Cannot find class org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguratio
  • How to make simplest servlet filter respect setted character encoding
  • Multiple folders in Java EE 6 web pages
  • How to run IntelliJ and Maven on OS X?
  • RichFaces 4 autocomplete is not showing suggestions
  • How to debug tests from a Maven build in Eclipse?
  • ClassNotFoundException: com.mysql.jdbc.Driver not found while using service mix
  • Grails 3 - How to publish to Artifactory
  • Best HTML5 structure for a layout where the title/header is outside the article tag
  • jinja2 template not found and internal server error
  • “mvn clean generate-source” could not resolve dependencies
  • If statement skipping right to else after being called once?
  • Floated image with variable width and heading with background image
  • Reading a file into a multidimensional array
  • App restarts from wrong activity
  • The plugin 'org.apache.maven.plugins:maven-jboss-as-plugin' does not exist or no valid ver
  • How to check if every primary key value is being referenced as foreign key in another table
  • How to handle AllServersUnavailable Exception
  • sending/ receiving email in Java
  • Change an a tag attribute in JavaScript based on screen width
  • How to get next/previous record number?
  • python regex in pyparsing
  • Android Google Maps API OnLocationChanged only called once
  • sending mail using smtp is too slow
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • Busy indicator not showing up in wpf window [duplicate]
  • costura.fody for a dll that references another dll
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • How can I use `wmic` in a Windows PE script?
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal
  • How can I use threading to 'tick' a timer to be accessed by other threads?