Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Java 8, how do I get my hostname without hard-coding it in my environment?

Tags:

We just upgraded to Java 8 on Amazon Linux. We are using Spring 4.3.8.RELEASE. It used to be that we could get our machine hostname by setting up beans in our application context file like so ...

<bean id="localhostInetAddress" class="java.net.InetAddress" factory-method="getLocalHost" /> <bean id="hostname" factory-bean="localhostInetAddress" factory-method="getHostName" /> 

But with Java 8, the bean "hostname" now contains the string

localhost 

Before Java 8, it used to contain the "hostname" value as run on the command line, which is

[myuser@machine1 ~]$ hostname machine1.mydomain.org 

How can I reconfigure our bean so that it gets the hostname that the command line lists out? I don't want to hard-code anything anywhere.

like image 848
Dave Avatar asked Jan 18 '18 15:01

Dave


People also ask

How to fetch hostname in Java?

In Java, you can use InetAddress. getLocalHost() to get the Ip Address of the current Server running the Java app and InetAddress. getHostName() to get Hostname of the current Server name.

Which method will display hostname?

The getHostName() method Java InetAddress returns the host name of a corresponding IP address. If this InetAddress was created with a host name, this host name will be remembered and returned else a reverse name lookup will be performed and the result will be returned based on the system configured name lookup service.

Which is the proper method to retrieve the hostname of local machine?

The following methods are used to get the Host Name. getHostName(): This function retrieves the standard hostname for the local computer. getHostByName(): This function retrieves host information corresponding to a hostname from a host database.

How do I find the hostname for an app?

The corresponding command is uname(1) . Thus to obtain the so-called "hostname", really the "nodename" one can use the command uname --nodename too. This should (generally) be the string that can be found in /etc/hostname .


2 Answers

From InetAddress java 8 is not getting the hostname :

There was similar bug fired in JDK.

What I understand is that they changed default resolution process.

They honor configuration in /etc/nsswitch.conf where hosts are configured for /etc/hosts that gives it main priority for name resolution.

Usually /etc/hosts has record for 127.0.0.1 localhost that provide name for host localhost

like image 128
mikep Avatar answered Oct 20 '22 09:10

mikep


There are a similar question in the OpenJDK bugs

The newer calls respect the localhosts /etc/nsswitch.conf configuration files. In the case of this machine that file tells these calls to look in files before referencing other naming services.

Since the /etc/hosts file contains an explicit mapping for this hostname / IP combination, that is what is returned.

In the older JDK's the gethostbyname actually ignored the local machines settings and immediately delegated to the naming service.

You can always use the Runtime class for that :

Process hostname = Runtime.getRuntime().exec("hostname");  BufferedReader stdInput = new BufferedReader(new             InputStreamReader(hostname.getInputStream())); String s; while ((s = stdInput.readLine()) != null) {         System.out.println(s); } 

But it's not so recommended as you can see

like image 30
Daniel Taub Avatar answered Oct 20 '22 08:10

Daniel Taub