Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Getting Hostname in Java

59 views
Skip to first unread message

frankge...@gmx.de

unread,
Dec 27, 2004, 2:53:32 PM12/27/04
to
I need to get the hostname from a Java application. java.lang.System
doesn't seem to have a method for that.
Any hints ?

Dov Wasserman

unread,
Dec 27, 2004, 3:36:56 PM12/27/04
to
frankge...@gmx.de wrote:

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

Wim Hoogendam

unread,
Dec 27, 2004, 3:40:09 PM12/27/04
to

<frankge...@gmx.de> wrote in message
news:1104177212....@f14g2000cwb.googlegroups.com...

>I need to get the hostname from a Java application. java.lang.System
> doesn't seem to have a method for that.
> Any hints ?
>

Hi,

try this:
java.net.InetAddress.getLocalHost().getHostName()


Dov Wasserman

unread,
Dec 27, 2004, 3:41:41 PM12/27/04
to
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
}

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

> frankge...@gmx.de wrote:

hiwa

unread,
Dec 27, 2004, 8:42:36 PM12/27/04
to
You can use methods of java.net.InetAddress class or
java.net.NetworkInterface class.
0 new messages