InetAddress.getLocalHost()
Erik Poupaert
erik.poupaert@chello.be
Sun Jun 22 12:04:00 GMT 2003
By the way, I've found some kind of rapidly concocted workaround for the problem that
the current implementation of InetAddress.getLocalHost() doesn't work on win32.
The system executable "hostname" ("hostname.exe") seems to work in exactly the same
way on linux as on windows.
So, I can query this executable and finally get hold of the local host name instead
of going through the convoluted, tribal ritual of invoking
"InetAddress.getLocalHost().getHostName()"; which eventually gives birth to the very
correct but absolutely useless answers of "127.0.0.1" or"localhost"; all of which was
instituted in the JDK in order to participate in the so-called SecurityManager
circus, which happens to be everthing but secure.
(By the way, in what circumstances could such answers actually be useful? I am
puzzled...)
import java.io.*;
/**
* This class is a replacement for InetAddress.getLocalHost().
*
* @author Erik Poupaert, June 2003, Brussels, Belgium
*/
public class LocalHost
{
//===========================================================
//GET LOCAL HOST NAME
//===========================================================
/**
* @return localhost name.
*/
public static String getLocalHostName()
{
int exitValue=0;
String hostName=null;
try
{
Process process=Runtime.getRuntime().exec("hostname");
BufferedReader reader=
new BufferedReader(new InputStreamReader(process.getInputStream()));
hostName=reader.readLine();
exitValue=process.waitFor();
}
catch(Exception e)
{
throw new RuntimeException("cannot read hostname");
}
if(exitValue!=0)
{
throw new RuntimeException("cannot read hostname");
}
return hostName;
}
//===========================================================
//MAIN
//===========================================================
public static void main(String[] args)
{
System.out.println("hostname is: " + getLocalHostName());
}
}
More information about the Java
mailing list