Sorry about poor formatting. HTML -> text converter ;-( Should be
fixed here.
The java.net package has the methods you want. From
http://www.devx.com/tips/Tip/13284:
try {
java.net.InetAddress localMachine =
java.net.InetAddress.getLocalHost();
System.out.println("Hostname of local machine: " +
localMachine.getHostName());
}
catch (java.net.UnknownHostException uhe) { // [beware typo in
code sample -dmw]
// handle exception
}
An alternate method listed at
http://javaalmanac.com/egs/java.net/GetHostname.html:
try {
// Get hostname by textual representation of IP address
InetAddress addr = InetAddress.getByName("127.0.0.1"); //
[127.0.0.1 is always localhost -dmw]/
// Get hostname by a byte array containing the IP address
byte[] ipAddr = new byte[]{127, 0, 0, 1};
addr = InetAddress.getByAddress(ipAddr);
// Get the host name from the address
String hostname = addr.getHostName();
// Get canonical host name
String hostnameCanonical = addr.getCanonicalHostName();
}
catch (UnknownHostException e) {
// handle exception
}
Note that any such method will first check Java's Security Manager to
see if the hostname/address lookup is permitted. It should be allowed by
default on most standard environments, though.
Good luck,
-Dov Wasserman
> wrote:
>
>> I need to get the hostname from a Java application. java.lang.System
>> doesn't seem to have a method for that.
>> Any hints ?
>