Home arrow static arrow Java Programming [Archive] - API to get computer name
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - API to get computer name
This topic has 4 replies on 1 page.

Posts:112
Registered: 8/9/01
API to get computer name  
Jul 16, 2004 1:19 PM



 
i want to get computer name in Java program. I tried to use System.getProperty(), but seems like there is no appropriate key.

please advise. thanks!!
 

Posts:24,036
Registered: 2/3/03
Re: API to get computer name  
Jul 16, 2004 1:32 PM (reply 1 of 4)



 
I think that's a JNI thing, but I could be wrong...
 

Posts:8,813
Registered: 10/4/00
Re: API to get computer name  
Jul 16, 2004 1:45 PM (reply 2 of 4)



 
Try
java.net.InetAddress.getLocalHost().getHostName()
 

Posts:10,967
Registered: 4/7/01
Re: API to get computer name  
Jul 16, 2004 1:49 PM (reply 3 of 4)



 
Here's some different ways using java.net package to get various names and numbers. Of course, this requires a network id.
import java.net.*; public class GetIPAddress {    public static void main(String[] args) throws UnknownHostException {         System.out.println(InetAddress.getLocalHost());        System.out.println(InetAddress.getLocalHost().getHostName());        System.out.println(InetAddress.getLocalHost().getHostAddress());         System.out.println            ("+++ If there is more than 1 IP address for HostName +++");         InetAddress[] inet = InetAddress.getAllByName(InetAddress.getLocalHost().getHostAddress());        for (int x = 0; x < inet.length; x++)        {            System.out.println                ("IP " + (x + 1) + ": " + inet[x].toString().substring(1));        }    }}

The following one takes an IP address argument.
import java.net.*; public class GetIPName{     public static void main(String[] args)    {        try        {            // Look up ip address, eg 123.123.123.123            InetAddress local = InetAddress.getByName(args[0]);            System.out.println(local.getHostAddress());            System.out.println(local.getHostName());            System.out.println(local.getCanonicalHostName());        }        catch(Exception e)        {            e.printStackTrace();        }    }}
 

Posts:122
Registered: 6/23/03
Re: API to get computer name  
Jul 16, 2004 6:51 PM (reply 4 of 4)



 
thanks!

so we just use networking api to get the computer name, there is nothing to do with JNDI, correct?
 
This topic has 4 replies on 1 page.