Thursday 3 October 2013

Open URL In Default System Browser : Java : BlueJ

Objective :

Opening an URL from your Java Program in the User-Default browser, with Java SE 6 there is. Unless your platform doesn't support this. Well Most Platform Running Latest Version of Java and With A Default Browser Would Do It Fine !

In Java SE 6 a new class has been added to the java.awt package: Desktop. This class has a public class method isDesktopSupported() which returns true if the class is supported on the current platform, and false otherwise.


If the class is supported, you can retrieve an instance using the class method getDesktop().


You can use the browse() method to display a URI in the default browser. If this browser is not able to handle the specified URI, the application registered for handling the URIs of the specified type is invoked. For example, the URI news:comp.lang.perl.misc might invoke a Usenet client.


However, before you call the browse() method make sure to check if this action is supported on the current platform by calling the isSupported() method with java.awt.Desktop.Action.BROWSE as the value of the first and only parameter.


Note that even though this method returns true, it's still possible that the platform doesn't support the URI - for example if there is no application registered for the given scheme. In this case the browse() method will throw an IOException.

Besides the browse() method there are 5 more methods, providing support for the 4 following Desktop actions:

EDIT : The edit() method launches the associated editor application and opens a file for editing.
MAIL : There are two mail() methods. The first one doesn't take any parameters and launches the mail composing window of the user-default mail client. The second method takes a single parameter: a "mailto" URI. : It also launces the mail composing window of the user-default mail client, but additionally fills in the message fields specified by the "mailto" URI (for example, to, subject, etc.).
OPEN : The open() method launches the associated application to open the file. If the specified file is a directory, the file manager of the current platform is launched to open it.
PRINT : Prints a file with the native desktop printing facility, using the associated application's print command.

BlueJ Program Screenshot :




Java Program Source Code :

/**
 * The Program Runs in Command Prompt Or in IDE using URLs
 * in the form of Parameters. All URLs must be in http:// form.
 * © SHANTANU KHAN 
 * @ shantanukhan1995@gmail.com
 * @website 0code.blogspot.com 
 * Program Type : BlueJ Program - Java
 */
import java.net.URI;
import java.awt.Desktop;
public class OpenURL
{
    public static void main(String[] args)
    {
        if(!java.awt.Desktop.isDesktopSupported())
        {
            System.err.println("Desktop is not supported (fatal)");
            System.exit(1);
        }
        if(args.length==0)
        {
            System.out.println( "Usage: OpenURI [URI [URI ... ]]" );
            System.exit( 0 );
        }
        java.awt.Desktop desktop = java.awt.Desktop.getDesktop();
        if(!desktop.isSupported(java.awt.Desktop.Action.BROWSE))
        {
            System.err.println( "Desktop doesn't support the browse action (fatal)" );
            System.exit( 1 );
        }
        for(String arg:args)
        {
            try{
                java.net.URI uri = new java.net.URI(arg);
                desktop.browse( uri );
            }
            catch(Exception e){
                System.err.println(e.getMessage());
            }
        }
    }

No comments:

Post a Comment