How To Open URL In Java

Java has made it easy to communicate with websites w/in applications by including a library of methods and properties.  These open URL applications are useful for programmers when communicating with websites.

How To Open URL In Java:

1 – Import Java libraries.  Add this code in the first part of your code

import java.net.URI;

import java.awt.Desktop;

Now before using the browse function you should import the libraries.

2 – Setup string path to the web site by using this code.  Replace myurladdress.com with the URL address you’re connecting to.

string myURL = “myurladdress.com”;

3 – Create desktop variable by using this code –

java.awt.Desktop myNewBrowserDesktop = java.awt.Desktop.getDesktop();

4 – Load the URL using this code –

java.net.URI myNewLocation = new java.net.URI( myURL);

5 – Use browse method in the desktop class to open and view the URL address.  Use this code to open and navigate to the URL –

myNewBrowserDesktop.browse( myNewLocation );

Need web hosting for Java JSP/servlets?  Try this excellent web host.


Article reference.

{ 0 comments… add one now }

Leave a Comment

Previous post:

Next post: