Getting hostname with java fails in latest jdk7

前端 未结 4 1681
别那么骄傲
别那么骄傲 2021-02-14 07:43

I\'ve been getting hostname of the machine as follows:

InetAddress.getLocalHost().getHostName();

However, when I put latest JDK (jdk1.7.0_04),

4条回答
  •  独厮守ぢ
    2021-02-14 08:04

    If you're not against using an external dependency from maven central, I wrote gethostname4j to solve this problem for myself. It just uses JNA to call libc's gethostname function (or gets the ComputerName on Windows) and returns it to you as a string.

    https://github.com/mattsheppard/gethostname4j

    @edward-thomson's answer above makes me thing I might have a bit more work to do to make it work well on MacOS though :)

提交回复
热议问题