Welcome to my blog, hope you enjoy reading
RSS

Thursday, 22 November 2012

Exception in thread "main" java.lang.UnsupportedOperationException: Desktop API is not supported on the current platform

Troubles with java.awt.Desktop browse() method

PROBLEM: To execute the following class

import java.awt.Desktop;
import java.net.URI;

public class mainclass {

public static void main(String[] args) {
if (Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
try {
Desktop.getDesktop().browse(new URI("http://www.sun.com"));
}
catch (Exception e) {
e.printStackTrace();
}
}
}

}

Exception:

Exception in thread "main" java.lang.UnsupportedOperationException: Desktop API is not supported on the current platform
at java.awt.Desktop.getDesktop(Desktop.java:144)
at com.dropbox.mainclass.main(mainclass.java:12)

Solution:

Step 1: Download BrowserLauncher2.jar from SourceForge.click here.
Step 2: Place BrowserLauncher2.jar in your classpath.
Step 3: Create an instance of BrowserLauncher.BrowserLauncher launcher = new BrowserLauncher();
Step 4: Launch a browser with a url.launcher.openURLinBrowser("http://www.google.com");

Example:

import edu.stanford.ejalbert.BrowserLauncher;
import edu.stanford.ejalbert.exception.BrowserLaunchingInitializingException;
import edu.stanford.ejalbert.exception.UnsupportedOperatingSystemException;

public class mainclass {

public static void main(String[] args)
{
try {
BrowserLauncher launcher = new BrowserLauncher();
launcher.openURLinBrowser("http://www.google.com");
} catch (BrowserLaunchingInitializingException e) {
e.printStackTrace();
} catch (UnsupportedOperatingSystemException e) {
e.printStackTrace();
}
}
}

0 comments: