Open in default browser
I am using Eclipse
Java Desktop for developing applications. The class Desktop
has a useful method browse()
that opens the URI in the default browser. My problem is that this function no longer works in Eclipse
, but still works fine outside Eclipse
, eg. when running an executable file Jar
that contains code.
Here's a short compiled example:
import java.awt.Desktop;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
public class OpenExample
{
public static void main(String[] arguments) throws URISyntaxException, IOException
{
Desktop.getDesktop().browse(new URI("https://www.google.com/"));
}
}
I am getting the following exception:
Exception in thread "main" java.io.IOException: Failed to open https://www.google.com/. Error message: A device attached to the system is not functioning.
at sun.awt.windows.WDesktopPeer.ShellExecute(Unknown Source)
at sun.awt.windows.WDesktopPeer.browse(Unknown Source)
at java.awt.Desktop.browse(Unknown Source)
at OpenExample.main(OpenExample.java:10)
What could be the problem? Obviously the code is Java
correct, and since it worked before, something must be broken down into, Eclipse
or maybe even into Windows
.
source to share
It might be related to this JDK bug: https://bugs.openjdk.java.net/browse/JDK-8064934
those. a real error may occur, but the JDK reports an incorrect error code due to these errors.
source to share
import java.awt.Desktop;
import java.net.URI;
public class OpenExample {
public static void main(String[] args) throws Exception {
String url = "http://google.com";
if (Desktop.isDesktopSupported()) { // for windows
Desktop.getDesktop().browse(new URI(url));
} else { // for linux
Runtime runtime = Runtime.getRuntime();
runtime.exec("/usr/bin/firefox -new-window " + url);
}
}
}
- try it. Sometimes it depends on the OS.
-
Check the printout of the Statement indicating the URL or not.
if it works please leave your feedback
source to share