user3621841 user3621841 - 14 days ago 14
Java Question

How to force Jetty to use IPv4

I am using standalone jetty 9.1.5 to deploy a java webapp.
I want jetty to use IPv4 instead of IPv6 but I cannot manage to do it!
When I type

netstat -tnlp
I can see the following result:

tcp6 0 0 127.0.0.1:554 :::* LISTEN 27535/java


... So that tells me jetty is using IPv6 even when on my
jetty-http.xml
file I have:

<Set name="host"><Property name="jetty.host" default="127.0.0.1" /></Set>


I am also starting jetty with the following java options:

-Djava.net.preferIPv4Stack=true -Djava.net.preferIPv4Addresses


... but to no avail; Jetty keeps using IPv6 and I have not been able to force it to use IPv4.
Any ideas?

Answer

Java is making the decision to use IPv4 or IPv6, not Jetty.

Jetty 9.1.5 is very old, consider upgrading to at least the Jetty 9.2.x series.

The example 127.0.0.1:554 is not an IPv6 Local Address, something is off with your netstat output.

Specify the jetty.host in your ${jetty.base}/start.ini, do not edit the jetty-http.xml.

Important Note: Do not edit, change, delete, rename, move, etc any content in your ${jetty.home}. Specify your configuration in your ${jetty.base}.

Did you try to connect to your running server using 127.0.0.1:554? (it will probably work)

If you don't want Java to default to IPv6, then the java command line option would look like this ...

$ cd /path/to/mybase
$ java -Djava.net.preferIPv4Stack=true -jar /path/to/jetty-dist/start.jar

However, if you don't have an IPv4 stack on your machine (such as not having a network interface with an IPv4 address, which is a surprisingly common setup), then this java command line option/argument is pretty much meaningless.