Simple JNA library to get the current machine's hostname from Java
Java
Latest commit ae0bb1b Jan 5, 2017 @mattsheppard committed on GitHub Fixed accidental cut-n-paste error in readme
Permalink
Failed to load latest commit information.
src
.gitignore
.travis.yml
LICENSE Initial commit Jan 4, 2017
README.md Fixed accidental cut-n-paste error in readme Jan 5, 2017
appveyor.yml See if we can avoid what looks like a JNA version conflict... Jan 5, 2017
pom.xml Updated version for 0.0.2 development Jan 5, 2017

README.md

gethostname4j

Simple JNA library to get the current machine's hostname from Java

I was frustrated to find that the common-ish Java idiom of InetAddress.getLocalHost().getHostName() was returning localhost in some cases that only seemed solved by tinkering with /etc/hosts. This library uses JNA to directly call the C library's gethostname method (or Kernel32Util.getComputerName() on Windows), and gives you back the hostname it got.

You can get it from maven central with a dependency like this...

    <dependency>
        <groupId>com.kstruct</groupId>
        <artifactId>gethostname4j</artifactId>
        <version>0.0.1</version>
    </dependency>

...and then use it in your code to get the hostname with something like...

    import com.kstruct.gethostname4j.Hostname;

    ...

    String hostname = Hostname.getHostname();

See also

Builds

Travis CI (Linux and Mac) build: Linux and Mac Build Status

Appveyor (Windows) build: Windows Build Status