为何用Runtime.getRuntime().exec ping ip,无论什么情况都返回 true

请教一下,为什么使用 Runtime.getRuntime().exec("ping -c 1 "+ ipAddress).waitFor() 时,哪怕是输入无存在的域名,他总是返回 true

img

而我又输入内网 ip,它却返回 false【我已经连接了内网,不是直接返回 false,而是超时 5 秒后还未连通导致的】

我一开始用的【InetAddress.getByName(ipAddress).isReachable(1500)】这个方法,但是一直是 false,查了下是授权问题,我搞不来,就改用的 Runtime 这种方式。

因为你的代码返回的结果是ping命令是否正确运行,而不是ping命令是否返回是否接收到对方返回,所以总是true

检查下api文档,可以看到true和false不是用来表示命令执行结果的:
https://docs.oracle.com/javase/8/docs/api/java/lang/Process.html#waitFor-long-java.util.concurrent.TimeUnit-

Returns:
true if the subprocess has exited and false if the waiting time elapsed before the subprocess has exited.