Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Getting Hostname in Java

Reply
Thread Tools

Getting Hostname in Java

 
 
frankgerlach22@gmx.de
Guest
Posts: n/a
 
      12-27-2004
I need to get the hostname from a Java application. java.lang.System
doesn't seem to have a method for that.
Any hints ?

 
Reply With Quote
 
 
 
 
Dov Wasserman
Guest
Posts: n/a
 
      12-27-2004
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 ?
>
>

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
 
Reply With Quote
 
 
 
 
Wim Hoogendam
Guest
Posts: n/a
 
      12-27-2004

<> wrote in message
news: ups.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()


 
Reply With Quote
 
Dov Wasserman
Guest
Posts: n/a
 
      12-27-2004
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 ?

>

 
Reply With Quote
 
tarun_b tarun_b is offline
Junior Member
Join Date: Aug 2009
Posts: 3
 
      08-28-2009
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Hostname verifier in JAVA Stone Java 0 08-04-2011 12:29 PM
getting hostname from ruby phil swenson Ruby 3 01-31-2006 04:22 PM
RMI: take ip/hostname what client was using and give it back as aremote objects hostname AWieminer Java 0 07-12-2005 08:05 PM
Getting Hostname in Java hiwa Java 0 12-28-2004 01:42 AM
java.rmi.server.hostname letter-figure problem David Murko Java 0 12-02-2004 10:47 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57