• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to find out the local machine name using a java program?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I want to find out the machine name by using a java program. Can somebody help me out?

Thanks
Java_learner
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

should get the localhost name
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
welcome to the Ranch, Rajesh Madhira.

I couldn't get InetAddress.getLocalHost().getHostName() to work; it threw an exception. I tried InetAddress.getByName("localhost") and got the result of localhost/127.0.0.1,

I should have known that result before trying!
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bit difficult, this one. You can also try: -

InetAddress.getByName(addr.getHostAddress()).getHostName()

You can also use NetworkInterface.getNetworkInterfaces() then call getInetAddresses() on each NetworkInterface object, then try some InetAddress methods on those ...
 
rajesh madhira
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to everyone ..Just to add the following code worked fine for me.

import java.net.*;

public class Test {

public static void main(String args[]) {
try {
String name = InetAddress.getLocalHost().getHostName();
System.out.println(name);
} catch(Exception ex) { ex.printStackTrace();}
}
}
 
Skool. Stay in. Smartness. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic