title: Detect internet Connection using Java
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/1402005/how-to-check-if-internet-connection-is-present-in-java">How to check if internet connection is present in java?</a>  </p>
  </blockquote>
  
  
  
  <p>I want to see if anyone has an easy way of detecting if there is an internet connection when using Java. The current app used the "InternetGetConnectedState" method in the WinInit DLL for windows, but my app needs to be cross-platform for mac operation and this way will not work. I do not know JNI at all either to use DLLs in Java and it became frustrating fast.</p>
  
  <p>Only ways I could think of were tring to open a URL connection to a website and if that fails, return false. My other way is below, but I didn't know if this was generally stable. If I unplug my network cable i do get an UnknownHostException when trying to create the InetAddress. Otherwise if the cable is connected I get a valid InetAddress object. I didnt test the below code on a mac yet. </p>
  
  <p>Thanks for any examples or advice you can provide.</p>
  
  <p>UPDATE: Final code block is at the bottom. I decided to take the advice of an HTTP request (in this case Google). Its simple and sends a request to the site for data to be returned. If I cannot get any content from the connection, there is no internet.</p>
  
  <pre><code>public static boolean isInternetReachable()
          {
              try {
  
                  InetAddress address = InetAddress.getByName("java.sun.com");
  
                  if(address == null)
                  {
                      return false;
                  }
  
              } catch (UnknownHostException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
                  return false;
              }
              catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
                  return false;
              }
              return true;
          }
  </code></pre>
  
  <p>Final Code Block:</p>
  
  <pre><code>//checks for connection to the internet through dummy request
          public static boolean isInternetReachable()
          {
              try {
                  //make a URL to a known source
                  URL url = new URL("http://www.google.com");
  
                  //open a connection to that source
                  HttpURLConnection urlConnect = (HttpURLConnection)url.openConnection();
  
                  //trying to retrieve data from the source. If there
                  //is no connection, this line will fail
                  Object objData = urlConnect.getContent();
  
              } catch (UnknownHostException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
                  return false;
              }
              catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
                  return false;
              }
              return true;
          }
  </code></pre>