Thursday, September 28, 2023

Fixing java.net.BindException: Cannot assign requested address: JVM_Bind in Tomcat, Jetty

One of the most dreaded errors in Java-based client server-based applications is a networking-related error, e.g. java.net.BindException: Cannot assign requested address: JVM_Bind. I have faced this issue while working with web servers like Tomcat, Jetty, and Weblogic before, but yesterday it came again when one of my colleagues faced this issue in Windows. As soon as Java programmers see java.net.BindException, they come to the conclusion that it's an issue with two processes listening on the same port and often mistook it for Java.net.BindException: Address already in use: JVM_Bind:8080, which is slightly different than this.

If you look at Java documentation for java.net.BindExcpetion, you will find this "Signals that an error occurred while attempting to bind a socket to a local address and port. Typically, the port is in use, or the requested local address could not be assigned."

It's the second part, which is more interesting in the case of java.net.BindException: Cannot assign requested address: JVM_Bind.

When you start a web server or application server, which typically listens on a port e.g. Tomcat or Jetty listens on 8080 and 8084 for HTTP and HTTPS traffic, they bind a socket to a local address and port. 

If you give them hostnames like localhost or devhost, then they used /etc/host in both Windows and Linux to resolve the domain name into IP address, if this mapping is incorrect then you will get java.net.BindException: Cannot assign requested address: JVM_Bind.

This host to IP address mapping file can be found at C:\Windows\System32\Drivers\etc\hosts, where C:\Windows is where you have installed Windows operating system. If you look at this file, you will see it contains IP address and hostname as shown below:



#127.0.0.1 localhost
192.168.52.1 localhost

If this mapping is changed and localhost cannot be resolve to 192.168.52.1 then you will get java.net.BindException: Cannot assign requested address: JVM_Bind. You can try by following program to reproduce this issue, remember, you need admin rights to change /etc/host settings in Windows.
How to fix java.net.BindException: Cannot assign requested address: JVM_Bind in Tomcat, Jetty

import java.io.IOException;
import java.net.ServerSocket;

public class ServerSocketTesting {

    public static void main(String args[]) throws IOException {

        ServerSocket server = new ServerSocket(8080);
        System.out.println("Server is started, listening connections on port :8080 ");
        server.accept();
    }
}

If your /etc/host mapping is incorrect then you will see something like

Exception in thread "main" java.net.BindException: 
Cannot assign requested address: JVM_Bind
    at java.net.PlainSocketImpl.socketBind(Native Method)
    at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:383)
    at java.net.ServerSocket.bind(ServerSocket.java:328)
    at java.net.ServerSocket.(ServerSocket.java:194)
    at java.net.ServerSocket.(ServerSocket.java:106)



Just, correct the mapping, or add 127.0.0.1 against the localhost to resolve this issue.  That's all about how to fix java.net.BindException: Cannot assign requested address: JVM_Bind error in Java-based client-server application like Minecraft, a popular game in Java, which also communicates with the server and other machines using TCP and sockets.

 It could also occur when you are using web and application servers like Tomcat, Jetty, or Weblogic as well.

Next time, instead of thinking that two processes are listening on the same port, also think about hostname to IP address resolution issue and verify contents of /etc/host file in both Windows and Linux. 

Let me know if you are facing this issue and not able to resolve it, pasting the error message and what you are trying to do will help to solve your error quickly and accurately.



How to resolve java.net.BindException: Cannot assign requested address: JVM_Bind in Tomcat

If you are getting this issue in the Tomcat web server then open your server.xml, which contains host and port information for Tomcat connectors. You can find server.xml in location (Windows OS) C:\Program Files\Apache Software Foundation\Apache Tomcat 7.0.41\conf\server.xml

Now look for your connector, if you are using default settings then your connector will look like this :

 <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />

but if you have modified it to include address="TestServer", then look for TestServer mapping in the/etc/hosts file. Try ping with the IP address and see if it is up or not, you will find that it's incorrect. Just update with the right IP and restart Tomcat.

Similarly, you can resolve java.net.BindException: Cannot assign requested address: JVM_Bind in Jetty or any other web server. The key thing is not to confuse this error with an address already in use error.


Other Java Error and Exception tutorials you may like to read
  • How to solve java.lang.ClassNotFoundException: com.mysql.jdbc.Driver in Java MySQL
  • Spring Boot Error - Error creating a bean with name 'dataSource' defined in class path resource DataSourceAutoConfiguration 
  • How to fix @Autowired - No qualifying bean of type found for dependency in Spring Boot? 
  • How to fix java.lang.ClassNotFoundException: org.postgresql.Driver error in Java? [solution]
  • How to fix java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver Error in Java? [solution]
  • java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory? [solution]
  • How to solve java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver in Java? [solution]
  • How to connect Oracle database from Java Program? (example)
  • How to solve java.lang.unsatisfiedlinkerror no ocijdbc11 in java.library.path? (solution)

  • All the best and let me know if you face similar issues.

    7 comments :

    Anonymous said...

    Hello there I am getting below exception, while running QuickFIXJ based FIX engine :

    Caused by: java.net.BindException: Cannot assign requested address
    at sun.nio.ch.Net.bind0(Native Method)
    at sun.nio.ch.Net.bind(Net.java:444)
    at sun.nio.ch.Net.bind(Net.java:436)
    at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:214)
    at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74)
    at org.apache.mina.transport.socket.nio.SocketAcceptor.registerNew(SocketAcceptor.java:363)
    at org.apache.mina.transport.socket.nio.SocketAcceptor.access$800(SocketAcceptor.java:55)
    at org.apache.mina.transport.socket.nio.SocketAcceptor$Worker.run(SocketAcceptor.java:222)
    at org.apache.mina.util.NamePreservingRunnable.run(NamePreservingRunnable.java:51)
    at java.lang.Thread.run(Thread.java:724)

    It runs fine on my desktop but as soon as I try to run it on Linux, it gives me this exception, I have already check no other instance of program is running and no other process is using this port.

    SARAL SAXENA said...

    @Anonymous ..Most likely related to another process using port . On Windows, run the command:

    netstat -a -n | find "LIST"

    And it should list anything there that's hogging the port. Of course you'll then have to go and manually kill those programs in Task Manager. If this still doesn't work, replace the line:

    serverSocket = new ServerSocket(9999);

    With:

    InetAddress locIP = InetAddress.getByName("192.168.1.20");
    serverSocket = new ServerSocket(9999, 0, locIP);

    Of course replace 192.168.1.20 with your actual IP address, or use 127.0.0.1.

    Pascal said...

    -Djava.net.preferIPv4Stack=true could also help (if worked for me adding this to my tomcat settings)

    Unknown said...

    Verify ipaddress in /etc/init.d/jbossas if Jboss is configured as a service.

    Anonymous said...

    +1

    Unknown said...

    PLS help me im 12, but i know a lot about coding and want to host a minecrt server.... However this error keeps popping up when i try to run it :(
    [12:45:57] [Server thread/INFO]: Using default channel type
    [12:45:59] [Server thread/WARN]: **** FAILED TO BIND TO PORT!
    [12:45:59] [Server thread/WARN]: The exception was: java.net.BindException: Cannot assign requested address: bind
    [12:45:59] [Server thread/WARN]: Perhaps a server is already running on that port?

    i went to C:\Windows\System32\drivers\etc\hosts also and it just says

    # Copyright (c) 1993-2009 Microsoft Corp.
    #
    # This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
    #
    # This file contains the mappings of IP addresses to host names. Each
    # entry should be kept on an individual line. The IP address should
    # be placed in the first column followed by the corresponding host name.
    # The IP address and the host name should be separated by at least one
    # space.
    #
    # Additionally, comments (such as these) may be inserted on individual
    # lines or following the machine name denoted by a '#' symbol.
    #
    # For example:
    #
    # 102.54.94.97 rhino.acme.com # source server
    # 38.25.63.10 x.acme.com # x client host

    # localhost name resolution is handled within DNS itself.
    # 127.0.0.1 localhost
    # ::1 localhost

    please read carefully and help me play minecraft with my friends

    javin paul said...

    Hello there, which port you are trying to run your server? use a port which is greater than 999, also check if another process, previous instance of same is running on that port. The error indicates invalid port or an already running instance.

    Post a Comment