Thursday 13 March 2014

Open URL in JAVA


Desktop class Of awt package supports browse() method to open a URL.

public void browse(URI uri) throws IOException
Launches the default browser to display a URI. If the default browser is not able to handle the specified URI, the application registered for handling URIs of the specified type is invoked. The application is determined from the protocol and path of the URI, as defined by the URI class. 


import java.awt.*;
import java.io.*;
 
public class OpenURL {
  public static void main(String args[])throws Exception{
    String url;
    url = "https://self-learning-java-tutorial.blogspot.com/2014/03/generic-class.html";           
    try {
      Desktop.getDesktop().browse(java.net.URI.create(url));
    }
    catch (IOException e) {
        System.out.println(e.getMessage());
    }
  }
}
  

                                                             Home

No comments:

Post a Comment