Home Java Java-get-example Get computer name in java



Get computer name in java
Posted on: October 23, 2008 at 12:00 AM
We can get the computer name by the java code program. For getting computer name we have used java.net.InetAddress class.

Get computer name in java

     

We can get the computer name by the java code program. For getting computer name we have used java.net.InetAddress class. We will use static method getLocalHost() for fetching the localhost and after having localhost we will be able to have host name by using getHostName() method of InetAddress class.

Here is the full example code of GetComputerName.java as follows:

 

 

 

GetComputerName.java

import java.util.*;
import java.lang.*;
import java.net.*;

public class GetComputerName
{
  public static void main(String args[]) {
  try{
  String computername=InetAddress.getLocalHost().getHostName();
  System.out.println(computername);
  }catch (Exception e){
  System.out.println("Exception caught ="+e.getMessage());
  }
  }
}

 

Output:

C:\javaexamples>javac GetComputerName.java

C:\javaexamples>java GetComputerName
roseindia

 

Download Source Code

 

Related Tags for Get computer name in java:
.netjavaccomclassstaticmethodsedgetvilocalhostnameusingaddressetllocalcomputerforaddprogramtohostramfetchavieilhostnamecanputuseincalasstamhavingnetoscaddnetosjadafterclescomputelocmeprosessathalletchetchandstatcodcodevinvattssthavstchiabablatiaffeetcetcprndodeomogro


More Tutorials from this section

Ask Questions?    Discuss: Get computer name in java   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.