How to set java.net.preferIPv4Stack=true at runtime?

前端 未结 6 1658
半阙折子戏
半阙折子戏 2020-12-13 12:56

I need to disable IPv6. For that the java documentation indicates setting jvm property java.net.preferIPv4Stack=true.

But I don\'t understand how to do

6条回答
  •  囚心锁ツ
    2020-12-13 13:30

    I ran into this very problem trying to send mail with javax.mail from a web application in a web server running Java 7. Internal mail server destinations failed with "network unreachable", despite telnet and ping working from the same host, and while external mail servers worked. I tried

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

    in the code, but that failed. So the parameter value was probably cached earlier by the system. Setting the VM argument

    -Djava.net.preferIPv4Stack=true
    

    in the web server startup script worked.

    One further bit of evidence: in a very small targeted test program, setting the system property in the code did work. So the parameter is probably cached when the first Socket is used, probably not just as the JVM starts.

提交回复
热议问题