Valeriu Paloş
Valeriu Paloş

Reputation: 3769

How do I make JVM use the IPv4 stack in isolated scope?

I have an application that runs on JVM/Tomcat and the machine(s) that it runs on all have IPv6 stack enabled. I'm trying to make a request to another machine that only supports IPv4 and the JVM will always prefer the IPv6 stack to make this request (I'm using the Apache HttpClient class to make the request).

I know about the -Djava.net.preferIPv4Stack=true property but for reasons which I won't go into here, I can not pass it when the JVM starts. I can't even set it programatically (for a short period of time) like so:

System.setProperty("java.net.preferIPv4Stack", "true");

...since this would potentially affect other threads running on the JVM/Tomcat instance (as I understood from this question and others).

So the question is, how do I make this single request go through the IPv4 stack (meaning to actually use the IPv4 interface, not only to connect to an IPv4 endpoint)?

I thought about using cURL (via exec or via the libcurl binding) since curl doesn't use the JVM at all.

Any other ideas?

Upvotes: 1

Views: 1728

Answers (1)

Valeriu Paloş
Valeriu Paloş

Reputation: 3769

Flexo's mention this is valid!

Eventually I realized that the real problem was not that the JVM would make the request form an IPv6 interface, that was impossible since the proxy would only have an A DNS entry (as Flexo pointed out).

The real problem was a typo in the proxy URL, which made the JVM fail to resolve the proxy host; what threw me off was the fact that the exception was thrown from the Inet6Address class (probably because the JVM did not yet know the fact that the proxy was IPv4 only).

I fixed the typo! Now the proxy is properly resolved and the request is made via IPv4. Thank you!

Upvotes: 1

Related Questions