InetAddress.getLocalHost().getHostName()  throwing unknownHostException

When i am executing this statement from a simple java program its working fine. But when I call the statement from JSP or servlet, its throwing UnknownhostException.
I am using tomcat3.3.1 as web server running on Sun Solaris.
Is there problem with setting of tomcat ???
Please help me out.

I tried nslookup at the shell prompt. It was showing unknown host for the ip address. I contacted our network people and came to know that the server is on internet and thats why it was not showing the local ip address host name in nslookup. Then I tried the internet ip address and host name of the server in the stetement and it worked out. Hope i am clear to u. Now the statement is running fine.
Actually I was trying to send mail using Smtp class and while dong that it was throwing UnknownHostException. Then i found out the Intaddress.getLocalHost() was the stmt throwing this error. Now as I am giving the internet address for constructor of new SmtpClient() method, it is not giving this error and my mail is getting delivered. But it processis taking lot of time. It takes @ 15 minutes to send a mail.
Can u help me out in this????
Thanx for help.......

Similar Messages

  • GetLocalhost() always throws UnknownHostException

    Hi,
    On my computer the method InetAddress.getLocalHost() always throws an UnknownHostException (JRE: jre-1_5_0_05-windows-i586-p.exe).
    I found a topic for this trouble (http://forum.java.sun.com/thread.jspa?forumID=11&threadID=495321) but it doesn't help me.
    I think there is a problem in my network configuration. Can you help me?
    My OS is XP service pack 2.
    >>>> Result of the command "ipconfig /all" (sorry it's in french, and I changed some number with "XXXX' for confidentiality)
    Configuration IP de Windows
            Nom de l'h�te . . . . . . . . . . : myhost
            Suffixe DNS principal . . . . . . :
            Type de noud . . . . . . . . . . : Inconnu
            Routage IP activ� . . . . . . . . : Non
            Proxy WINS activ� . . . . . . . . : Non
    Carte Ethernet Connexion au r�seau local:
            Suffixe DNS propre � la connexion :
            Description . . . . . . . . . . . : 3Com Gigabit LOM (3C940)
            Adresse physique . . . . . . . . .: 00-0C-6E-XX-XX-XX
            DHCP activ�. . . . . . . . . . . : Oui
            Configuration automatique activ�e . . . . : Oui
            Adresse IP. . . . . . . . . . . . : 192.168.0.2
            Masque de sous-r�seau . . . . . . : 255.255.255.0
            Adresse IP. . . . . . . . . . . . : XXXX::XXXX:XXXX:fe85:ebb4%4
            Passerelle par d�faut . . . . . . : 192.168.0.1
            Serveur DHCP. . . . . . . . . . . : 192.168.0.1
            Serveurs DNS . . . . . . . . . .  : 192.168.0.1
                                                fec0:0:0:ffff::1%1
                                                fec0:0:0:ffff::2%1
                                                fec0:0:0:ffff::3%1
            Bail obtenu . . . . . . . . . . . : mardi 18 octobre 2005 18:16:51
            Bail expirant . . . . . . . . . . : vendredi 21 octobre 2005
    18:16:51
    Carte Tunnel Teredo Tunneling Pseudo-Interface :
            Suffixe DNS propre � la connexion :
            Description . . . . . . . . . . . : Teredo Tunneling
    Pseudo-Interface
            Adresse physique . . . . . . . . .: FF-FF-FF-FF-FF-FF-FF-FF
            DHCP activ�. . . . . . . . . . . : Non
            Adresse IP. . . . . . . . . . . . : XXXX::5445:5245:444f%5
            Passerelle par d�faut . . . . . . :
            NetBIOS sur TCPIP. . . . . . . .  : D�sactiv�
    Carte Tunnel Automatic Tunneling Pseudo-Interface :
            Suffixe DNS propre � la connexion :
            Description . . . . . . . . . . . : Automatic Tunneling
    Pseudo-Interface
            Adresse physique . . . . . . . . .: C0-A8-00-02
            DHCP activ�. . . . . . . . . . . : Non
            Adresse IP. . . . . . . . . . . . : fe80::5efe:192.168.0.2%2
            Passerelle par d�faut . . . . . . :
            Serveurs DNS . . . . . . . . . .  : fec0:0:0:ffff::1%1
                                                fec0:0:0:ffff::2%1
                                                fec0:0:0:ffff::3%1
            NetBIOS sur TCPIP. . . . . . . .  : D�sactiv�
    Result of the "ping localhost" command:
    ping localhost
    Envoi d'une requ�te 'ping' sur myhost [::1] avec 32 octets de donn�es :
    R�ponse de ::1 : temps<1ms
    R�ponse de ::1 : temps<1ms
    R�ponse de ::1 : temps<1ms
    R�ponse de ::1 : temps<1ms
    >>>> Result of the "route print" command
    ===========================================================================
    Liste d'Interfaces
    0x1 ........................... MS TCP Loopback interface
    0x2 ...00 0c 6e XX XX XX ...... 3Com Gigabit LOM (3C940) - Miniport
    d'ordonnance
    ment de paquets ===========================================================================
    ===========================================================================
    Itin�raires actifs :
    Destination r�seau    Masque r�seau  Adr. passerelle   Adr. interface
    M�trique
              0.0.0.0          0.0.0.0      192.168.0.1     192.168.0.2       20
         64.4.XXX.XXX  255.255.255.255      192.168.0.1     192.168.0.2       20
        65.54.XXX.XXX  255.255.255.255      192.168.0.1     192.168.0.2       20
            127.0.0.0        255.0.0.0        127.0.0.1       127.0.0.1       1
          192.168.0.0    255.255.255.0      192.168.0.2     192.168.0.2       20
          192.168.0.2  255.255.255.255        127.0.0.1       127.0.0.1       20
        192.168.0.255  255.255.255.255      192.168.0.2     192.168.0.2       20
       207.46.XXX.XXX  255.255.255.255      192.168.0.1     192.168.0.2       20
       207.68.XXX.XXX  255.255.255.255      192.168.0.1     192.168.0.2       20
            224.0.0.0        240.0.0.0      192.168.0.2     192.168.0.2       20
      255.255.255.255  255.255.255.255      192.168.0.2     192.168.0.2       1
    Passerelle par d�faut :       192.168.0.1
    ===========================================================================
    Itin�raires persistants :
      AucunThank you!

    Just a guess, but try setting "Suffixe DNS propre � la connexion"
    On my machine, I bring up the window Local Area Connection Status, then click on Properties, that brings up the Local Area Connection Properties window. Then I click on Internet Protocol (TCP/IP) in the list, then click the Properties button. That brings up the Internet Protocol (TCP/IP) Properties window; in there click Advanced button. That brings up the Advanced TCP/IP Settings window. In it click on the DNS tab. Down near the bottom is a box labeled "DNS suffix for this connection" and in it I have the suffix for my domain (berkeley.edu). I've never understood what the two boxes below are for/what they do.
    This is just a stab in the dark; I have no idea if it will help.

  • GetLocalHost throws UnknownHostException

    We have a situation where #getLocalHost will intermittently throw an UnknownHostException. I've checked etc/hosts and everything is normal, additionally, the machine is not using DNS.
    The exception is a little odd in that the local host name is listed twice:
    Caused by: java.net.UnknownHostException: myhost: myhost
          at java.net.InetAddress.getLocalHost(InetAddress.java:1191)If I look at the source, it seems that the actual exception is being thrown from #getAddressFromNameService on this line:
              byte_array = nameService.lookupAllHostAddr(host);Anyone run into this? What's odd is that it is intermittent, not predictable.
    R.

    Everyone gets annoyed at this one (especially when developing on a laptop). Here's a snippet of code I often use when testing code locally
        * Best attempt to get the local hostname, with or without the existence of a DNS entry.
        * @author [email protected]
        * @return Short version of the local hostname.
        * @throws UnknownHostException There is a problem with the DNS entry for this host and/or this JVM implementation
        * does not return the standard error string.
       public static String getLocalHostName() throws UnknownHostException {
          try {
               return (InetAddress.getLocalHost()).getHostName();
          } catch ( UnknownHostException uhe ) {
              String host = uhe.getMessage(); // host = "hostname: hostname"
              if ( host!=null ) {
                  int colon = host.indexOf(':');
                  if ( colon>0 ) {
                     return host.substring(0, colon); // Dangerous, this is JVM dependant code.
              throw uhe;
       }

  • InetAddress.getLocalHost() logic with virtual IPs

    Hello.
    I have an application that is using:
    this.serverName = java.net.InetAddress.getLocalHost().getHostName();
    to determine the local machine's hostname. My Solaris box has a physical IP (obviously), and a virtual IP. Here's 'ifconfig -a':
    lo0: flags=849<UP,LOOPBACK,RUNNING,MULTICAST> mtu 8232
    inet 127.0.0.1 netmask ff000000
    hme0: flags=863<UP,BROADCAST,NOTRAILERS,RUNNING,MULTICAST> mtu 1500
    inet 207.240.14.15 netmask ffffff00 broadcast 207.240.14.255
    hme0:1: flags=843<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
    inet 207.240.14.16 netmask ffffff00 broadcast 207.240.14.255
    The getLocalHost() call is always returning the IP address of the virtual IP, not the physical. Does anyone happen to know the logic that getLocalHost() uses to determine the IP of the local machine? I sure would think it would pick the physical over any virtuals, but that doesn't seem to be the case.
    I tried the 'last interface brought up' theory, but that didn't pan out either.
    Any help would be appreciated!
    Thanks.

    You may need to get your virutal hostname first.
    String hostn = InetAddress.getByName(hostNm).getHostAddress();

  • InetAddress.getLocalHost retrieving 127.0.0.1, not the actual IP Address

    I am using the following code :
    try{
              InetAddress iaddr = InetAddress.getLocalHost();
              String ipaddr = iaddr.getHostAddress();
              System.out.println("IP= "+ipaddr);
              }catch(UnknownHostException ex){
                   ex.getMessage();
    to get the IP address of the server on which this code is running.
    We are actually trying to set up different machines (other than the Production machine) for Failover of the Application. So, we need to get the IP address of the machine on which the application is running in the code.
    For one machine, the above code works perfectly OK.
    But, for the three Production machines, the IP address that we are retrieving using the getLocalHost function is fetching the localhost and not the IP address of the machine. That is instead of fetching the IP � a.b.c.d (I am not writing the actual IP in this mail :) ) ... it is fetching the localhost 127.0.0.1 .
    So we need to find out a way to fetch the actual IP address so that failover can work.
    FYI, all of the machines are Solaris machines.
    Regards,
    Anjan

    I am putting up the contents of the /etc/hosts files of the 3 machines I am using.
    @@@@ Machine #1 (Code running OK for this, retrieving a.b.c.155 for InetAddress.getLocalhost().getHostAddress()) @@@@
    Excerpt from /etc/hosts file :
    ==========================
    # Internet host table
    127.0.0.1 localhost
    a.b.c.155 rating-DR loghost
    ==========================
    Host name for Machine #1
    ===================
    bash>hostname
    rating-DR
    @@@@ Machine #2 (Code returning 127.0.0.1 in this server, for InetAddress.getLocalhost().getHostAddress()) @@@@
    Excerpt from /etc/hosts file :
    ==========================
    # Internet host table
    127.0.0.1 localhost
    a.b.c.235 rating1 rating1.xyz.com loghost
    a.b.c.239 rating2
    ==========================
    Hostname for Machine #2
    ===================
    bash>hostname
    rating1
    @@@@ Machine #3 (Code returning 127.0.0.1 in this server, for InetAddress.getLocalhost().getHostAddress()) @@@@
    Excerpt from /etc/hosts file :
    ==========================
    # Internet host table
    127.0.0.1 localhost
    a.b.c.239 rating2 rating2.xyz.com loghost
    192.168.210.235 rating1
    192.168.210.155 rating-DR rating-DR.fastlink.com.jo
    ==========================
    Hostname for Machine #3
    ===================
    bash>hostname
    rating2

  • InetAddress.getLocalHost works differently with jdk1.4.0 beta65 and rc

    I am using jdk1.4.0 on Linux with two network interfaces, one of them with a private IP.
    When using jdk1.4.0beta65
    InetAddress.getLocalHost seems to return the IP of the first entry in /etc/hosts corresponding to my machine's hostname.
    With jdk1.4.0 rc I get the last entry in /etc/hosts.
    I changed the order of these entries in /etc/hosts to check if it is really the reason.
    I had problems with an application when getLocalHost returned the private IP.
    Of course changing the order would solve my problem.
    Is there any reason why this changed?
    cu
    Achim

    I don't have the answer for you but why don't you try the following trick and see if you get a more consistent result:
    try {
        Socket socket=new Socket("java.sun.com",80);
        InetAddress addr=socket.getLocalAddress();
        String HostName=addr.getHostName();
        String HostAddr=addr.getHostAddress();
    } catch (Throwable e) {}
    V.V.

  • Windows 98 hangs on InetAddress.getLocalHost()

    Has anyone seen an application hang and hang windows 98 when java executes InetAddress.getLocalHost(). It doesn't happen every time but about 1 out of 15.
    thanks
    -cb

    I would question whether you're totally certain that it hangs at that particular line of code.

  • InetAddress.getLocalHost on multiple IP addresses

    Hi,
    I would like to know how the method getLocalHost() of java.net.InetAddress behaves in case there are mutiple IP addresses assigned to a server. On what basis the JRE decides to return one of the IP addresses?
    Any information on this would be much appreciated.
    Thanks,
    Pritesh

    see
    http://java.sun.com/developer/JDCTechTips/2005/tt0816.html#1

  • Surprise

    I used java.net.InetAddress to get the IP Address by the following applet in TestIP.class file:
    import java.applet.*;
    import java.net.*;
    import java.awt.*;
    public class TestIP extends Applet {
    String ip;
    public void init() {
    try {
    ip=InetAddress.getLocalHost().toString();
    catch(UnknownHostException e) {
    ip="Unknown";
    public void paint(Graphics g) {
    g.drawString(ip, 50, 25);
    And run it with a HTML file in IE5.5 and also in NetScape 4.7, but the results was different:
    Result In IE5.5:
    localhost/127.0.0.1
    Result in NetScape4.7:
    Azarnoosh/217.29.208.69
    Do you have any java applet solution to getting IP address of client in IE5.5 & NetScape4.7?
    Regards
    Ali Azarnoosh

    This should display all IP addresses, but requires your applet to be signed.
    import java.applet.*;
    import java.net.*;
    import java.awt.*;
    public class TestIP extends Applet {
         InetAddress[] addresses;
         public void init() {
              try {
                   addresses = InetAddress.getAllByName(InetAddress.getLocalHost().getHostName());
              catch(UnknownHostException e) {
         public void paint(Graphics g) {
              for (int i=0; i<addresses.length; i++) {
                   g.drawString(addresses.getHostAddress(), 50, 25*i + 25);

  • InetAddress UnknownHostException

    Hello,
    InetAddress.getLocalHost() is throwing a UnknownHostException when i'm running an app from within another app using Runtime.exec() and the command string "cmd /k appname". However it returns the hostname fine when the app is run from the command prompt!
    I'm guessing it's a environment veraiable or another configuration problem but have not been able to figure of what to missing so far.
    Any help would be appreicated

    Fixed the problem,
    requires SystemRoot to be set

  • Gethostname

    hi,all.....i've just created an application,i want to ask how to get the computer name / get host name of the client computer ? but i dont user java servlet....
    thanxxss

    ok...........it works......i understood now....
    import java.net.InetAddress;
    import java.net.UnknownHostException;
    public class testh {
         * @param args
         public static void main(String[] args) {
              // TODO Auto-generated method stub
              try {
                   String test = InetAddress.getLocalHost().getHostName();
                   System.out.println(test);
              } catch (UnknownHostException e) {
                   // TODO Auto-generated catch block
                   e.printStackTrace();
    thanxxss...
    then i want to ask..........is there a way to make a session without using servlet ?

  • UnknownHostException on Japanese Windows XP - Please Help!!!

    Hello,
    I am running the following code on Japanese Windows XP:
    String localHostName = java.net.InetAddress.getLocalHost().getHostName();
    If the Computer name is in English, this works fine.
    If the Computer name is in Japanese, I get an UnknownHostException in InetAddress.java, line 1191.
    If I run this same code on a Japanese Windows 2K, with a Japanese computer name, it works fine.
    I tried 3 different JREs: 1.3.1_11, 1.4.2_01, and 1.4.2._04 - same result. This seems to be a difference between Windows 2K and XP.
    I searched the web and the forums and I can't see any reference to this specific problem. Is this a known issue?
    Thanks in advance!
    Dave Raskin
    [email protected]

    I searched the bug database for UnknownHostException and got 0 results.

  • How to get system Hostname in java

    Hi,
    I am a java developer trying to get the hostname of the system from java class. Meaning want the value when i run the $hostname command in linux and my server is Linux server.
    I am searching in the System Properties for the hostname but i could not get it
    I am running IBM Websphere 5.2 and JDK 1.4.2 in my linux server.
    Thanks for input
    -AP

    Try
    package testing;
    import java.net.*;
    public class Test
      public static void main(String[] args) throws UnknownHostException
        System.out.println(InetAddress.getLocalHost().getHostName());
    }

  • Smtp mail client socket problem

    As soon i enter the smtp server address (smtp.mail.yahoo.com) i get the following:
    Exception in thread "main" java.net.UnknownHostException: smtp.mail.yahoo.com: smtp.mail.yahoo.com
            at java.net.InetAddress.getAllByName0(InetAddress.java:1128)
            at java.net.InetAddress.getAllByName0(InetAddress.java:1098)
            at java.net.InetAddress.getAllByName(InetAddress.java:1061)
            at java.net.InetAddress.getByName(InetAddress.java:958)
            at MailTest.<init>(MailTest.java:20)
            at MailTest.main(MailTest.java:97)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 11 seconds)
    import java.util.*;
    import java.net.*;
    import java.io.BufferedReader;
    import java.io.*;
    public class MailTest
        String from;
        String to;
        String smtp; //smtp.mail.yahoo.com
        String hostName;
        BufferedReader brin;
        PrintWriter pwout;
        String communication;   
        public MailTest() throws UnknownHostException,IOException
            readMailData();       
            Socket s=new Socket(InetAddress.getByName(smtp), 25);
            //tried also smtp.plus.mail.yahoo4.akadns.net and with the
            //corresponding ip
            hostName=InetAddress.getLocalHost().getHostName();
            brin=new BufferedReader(new InputStreamReader(s.getInputStream()));       
            pwout=new PrintWriter(s.getOutputStream());               
            receive();
            send("HELO "+ hostName);
            receive();
            send("MAIL FROM: <"+from+">");
            receive();
            send("DATA");
            receive();       
            send(readMessageBody());
            send(".");
            receive();
            s.close();
        private void receive() throws IOException
            System.out.println("Incoming Communication");
            System.out.println("-----------------------------------------");
            while (!(communication=brin.readLine()).equals(null))
                System.out.println(communication);
            System.out.println("-----------------------------------------");
        private void send(String s) throws IOException
            System.out.println("Outcoming Communication");
            System.out.println(s);
            pwout.write(s);
            pwout.flush();
            System.out.println("-----------------------------------------");
        private String readMessageBody() throws IOException
            String q="";
            String msgBuffer="";
            BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
            System.out.println("--------------Message--Body--------------");
            System.out.println("enter -Enter-quit-Enter- to exit");
            System.out.print(">");       
            q=in.readLine();       
            while (!q.equals("quit"));
                msgBuffer+=q;
                System.out.print(">");
                q=in.readLine();           
            System.out.println("-----------------------------------------");
            return msgBuffer;
        private void readMailData() throws IOException
            BufferedReader in= new BufferedReader(new InputStreamReader(System.in));                       
            System.out.println("From: ");
            from=in.readLine().trim();
            System.out.println("To: ");
            to=in.readLine().trim();
            System.out.println("SMTP server: ");
            smtp=in.readLine();       
        public static void main(String[] args) throws UnknownHostException, IOException           
            MailTest mt=new MailTest();
    }

    I would say you have a DNS problem, or perhaps you aren't connected to the Internet. Can you ping smtp.mail.yahoo.com? or at least can ping resolve the name?

  • Toplink Cache Sync problem

    Hello All,
    I have configured RCM like this in my code, we are using JBoss 4.0.5
    public void configureRemoteCommandManager(String hostname,
    int portNumber,
    String connectionFactoryName,
    String schemaName,
    Server session) {
         //define the Command Manager
         RemoteCommandManager commandMgr = new RemoteCommandManager((CommandProcessor)session);
         //set local host URL. In JMS, the local host URL is optional and is used by logging
         //to trace the message sender for debugging purposes
         String localHostName = "";
              try {
                   localHostName = InetAddress.getLocalHost().getHostName().toLowerCase();
              } catch (UnknownHostException e) {
                   e.printStackTrace();
         commandMgr.setUrl("jnp://" + localHostName + ":" + portNumber);
         //define the JMS transport
         JMSTopicTransportManager transportMgr = new JMSTopicTransportManager(commandMgr);
         transportMgr.setTopicHostUrl("jnp://" + hostname + ":" + portNumber);
         //transportMgr.setTopicConnectionFactoryName("jms/TopLinkTopicConnectionFactory");
         transportMgr.setTopicConnectionFactoryName(connectionFactoryName);
         transportMgr.setTopicName( "topic/testTopic");
         transportMgr.setUserName("user");
         transportMgr.setPassword("pass");
              //Initial context factory name is default to
         transportMgr.setInitialContextFactoryName("org.jnp.interfaces.NamingContextFactory");
         //hook up the components
         commandMgr.setTransportManager(transportMgr);
         session.setCommandManager(commandMgr);
         //turn on distributed cache synchronization with RCM
         session.setShouldPropagateChanges(true);
         //Start joining the cluster
         if(session.isConnected ()) {
              session.getCommandManager().initialize();
              logger.info("RemoteCommandManager initialized");
         } else {
              session.login();
              logger.info("RemoteCommandManager login successful");
    This code doesn't throw any exception, but when I am running Toplink in Cluster, objects are not getting refreshed.
    I am seeing following in my log.
    2007.11.13 15:41:10.768--ServerSession(32434103)--Thread[Thread-86,5,jboss]--received_updates_from_remote_server
    2007.11.13 15:41:10.784--ServerSession(32434103)--Thread[Thread-87,5,jboss]--received_updates_from_remote_server
    2007.11.13 15:41:11.128--ServerSession(32434103)--Thread[Thread-88,5,jboss]--received_updates_from_remote_server
    2007.11.13 15:41:11.143--ServerSession(32434103)--Thread[Thread-89,5,jboss]--received_updates_from_remote_server
    2007.11.13 15:41:11.346--ServerSession(32434103)--Thread[Thread-90,5,jboss]--received_updates_from_remote_server
    2007.11.13 15:41:11.362--ServerSession(32434103)--Thread[Thread-91,5,jboss]--received_updates_from_remote_server
    Looks like updates are coming from remote box, then why objects are not getting refreshed even though they are in memory.
    Any input is really valuable.
    Thanks
    Vik

    From the log it seems that the events are getting processed. What type of cache coordination are you using in your descriptors (cacheSynchronizationType)? If you have configured them to use invalidation, the objects will not be refreshed until the next time they are accessed (read). If you are using change sets then they should get updated in the cache.
    You can check the contents of the change set that is broadcast using the Session event preDistributedMergeUnitOfWorkChangeSet.

Maybe you are looking for

  • [1.1.3.27.66] Drag tables to worksheet no longer works.

    The ability to drag a table from the object browser into a worksheet to create a select statement seems to have stopped working. When I try to drag a table accross, nothing happens - no mouse cursor changes-nothing. There is nothing in the java conso

  • Output condition delete history in a Purchase order

    Hi, I would like to know how best we can find the history or change record of output delted in Purchase order. Here is the detailed scenario. I have created a PO and it populated he message conditon in the PO. I went into the PO using ME22n and delet

  • Multiple JScrollPanes in one window

    Hi. I've been trying to figure out how to make multiple scrollable text areas in one window. I'm trying to use JEditorPanes for the text areas so that they can display html. Does anyone know how I can make multiple scrollable JEditorPanes appear side

  • Payload Update

    In worklist API I have found method: worklistservice.updatePayload(IWorklistContext, IWorklistTask, Map) I have IWorklistContext and IWorklistTask, but I don't know how Map should look like. My payload is: <id>123</id> <name>Tom</name> <job>software

  • Referenced Aperture library larger than iPhoto library?

    My iPhoto library is 232 GB , so to save space, I deciced to imort them to Aperture 3 as a referenced library. A few hours later, 'processing' has stopped and my Aperture Library is now a whopping 283 GB. If Aperture is simply pointing to the picture