Getting Hostname in Java

F

frankgerlach22

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

Dov Wasserman

I need to get the hostname from a Java application. java.lang.System
doesn't seem to have a method for that.
Any hints ?
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
 
W

Wim Hoogendam

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()
 
D

Dov Wasserman

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
 
Joined
Aug 4, 2009
Messages
3
Reaction score
0
Hi,

I have solved my problems many folds using the solution u have provided
Kindly help me one more step

There could be many instance running on the host system, so to get the name of those instaance have we got any in built utility, if yes kindly tellme

Regards Tarun
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top