Simple JNA library to get the current machine's hostname from Java
Java
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
src
.gitignore
.travis.yml
LICENSE
README.md
appveyor.yml
changes.md
pom.xml

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.2</version>
    </dependency>

or

compile 'com.kstruct:gethostname4j:0.0.2'

in Gradle

...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