Getting Hostname in Java

Discussion in 'Java' started by frankgerlach22@gmx.de, Dec 27, 2004.

  1. Guest

    I need to get the hostname from a Java application. java.lang.System
    doesn't seem to have a method for that.
    Any hints ?
     
    , Dec 27, 2004
    #1
    1. Advertising

  2. 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
     
    Dov Wasserman, Dec 27, 2004
    #2
    1. Advertising

  3. <> wrote in message
    news:...
    >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()
     
    Wim Hoogendam, Dec 27, 2004
    #3
  4. Re: Getting Hostname in Java (repost)

    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 ?

    >
     
    Dov Wasserman, Dec 27, 2004
    #4
  5. tarun_b

    Joined:
    Aug 4, 2009
    Messages:
    3
    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
     
    tarun_b, Aug 28, 2009
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Murko
    Replies:
    0
    Views:
    890
    David Murko
    Dec 2, 2004
  2. hiwa

    Getting Hostname in Java

    hiwa, Dec 28, 2004, in forum: Java
    Replies:
    0
    Views:
    461
  3. AWieminer
    Replies:
    0
    Views:
    776
    AWieminer
    Jul 12, 2005
  4. Stone

    Hostname verifier in JAVA

    Stone, Aug 4, 2011, in forum: Java
    Replies:
    0
    Views:
    1,096
    Stone
    Aug 4, 2011
  5. phil swenson

    getting hostname from ruby

    phil swenson, Jan 31, 2006, in forum: Ruby
    Replies:
    3
    Views:
    139
    Dave Baldwin
    Jan 31, 2006
Loading...

Share This Page