1

I have a button to open a URL in browser:

URI uri = new URI("http://google.com/");
Desktop dt = Desktop.getDesktop();
dt.browse(uri.toURL()); // has error

but i get following error in last statement:

The method browse(URI) in the type Desktop is not applicable for the arguments (URL)

thanks for any suggestion.

3 Answers 3

3

Found solution:
1. Remove .toURL()
2. use try catch block

try
{
    URI uri = new URI("http://google.com/");
    Desktop dt = Desktop.getDesktop();
    dt.browse(uri);
}
catch(Exception ex){}
Sign up to request clarification or add additional context in comments.

1 Comment

Do avoid empty catch blocks like that. It's a cause of silent bugs which are hard to track.
2

What it tells you is that you are sending an URL object while it expects an URI.

just change

dt.browse(uri.toURL()); // has error

to

dt.browse(uri); // has error

before being able to use Desktop, you have to consider if it is supported

if (Desktop.isDesktopSupported()) {
        desktop = Desktop.getDesktop();
        // now enable buttons for actions that are supported.
        enableSupportedActions();
}

and the enableSupportedActions

private void enableSupportedActions() {
    if (desktop.isSupported(Desktop.Action.BROWSE)) {
        txtBrowserURI.setEnabled(true);
        btnLaunchBrowser.setEnabled(true);
    }
}

that shows that you have to check also, if BROWSE action is also supported.

Comments

1

use some thing like this

try {Desktop.getDesktop().browse(new URI("http://www.google.com"));
} catch (Exception e) 
{JOptionPane.showMessageDialog(null,e);}
} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.