Recommended Way to Get Hostname in Java

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

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

Getting the computer name in Java

The computer "name" is resolved from the IP address by the underlying DNS (Domain Name System) library of the OS. There's no universal concept of a computer name across OSes, but DNS is generally available. If the computer name hasn't been configured so DNS can resolve it, it isn't available.

import java.net.InetAddress;
import java.net.UnknownHostException;

String hostname = "Unknown";

try
{
InetAddress addr;
addr = InetAddress.getLocalHost();
hostname = addr.getHostName();
}
catch (UnknownHostException ex)
{
System.out.println("Hostname can not be resolved");
}

Get host name from IP address on network using JAVA

You're not missing anything in the code,but,the source of error is the security manager/firewall of the called system.

The Oracle docs for the InetAddress.getCanonicalHostname() states that :-

Returns:

the fully qualified domain name for this IP address, or if the
operation is not allowed by the security check, the textual
representation of the IP address.

The Oracle docs for the InetAddress.getHostname() states that :-

Returns:

the host name for this IP address, or if the operation is not allowed
by the security check, the textual representation of the IP address.

Solution :-

Whatever system you want to enquire host-name of,just disable the firewall of that system. The security managers will always block system-specific enquiries about the other systems in the network as it is considered not an information to be shared(unsecure). You should always keep this thing in mind.

Java current machine name and logged in user?

To get the currently logged in user:

System.getProperty("user.name"); //platform independent 

and the hostname of the machine:

java.net.InetAddress localMachine = java.net.InetAddress.getLocalHost();
System.out.println("Hostname of local machine: " + localMachine.getHostName());

Get DNS name of local machine as seen by a remote machine

The short answer is that if you really want User A and User B to see the same text, you can't rely on finding out your hostname yourself. You need User B to transmit their view of User A's hostname to User A and vice versa. Due to NAT, you won't be able to just check your own computer's hostname.

Alternatively, (Jonathon beat me to this in the question comments) you can have each user send their own private hostname as part of the connection handshake and use that to print messages on the remote end.



Related Topics



Leave a reply



Submit