Use win32 ShellExecute API to open URL with default browser

This commit is contained in:
Cristian Maglie 2017-05-22 12:25:58 +02:00
parent 229c32ccad
commit bb41ad791f
1 changed files with 14 additions and 15 deletions

View File

@ -37,6 +37,7 @@ import java.util.List;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.Shell32;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIOptions;
@ -129,11 +130,15 @@ public class Platform extends processing.app.Platform {
if (file.exists()) {
// in this case convert the path to a "file:" url
url = file.toURI().toString();
// this allows to open the file on Windows 10 that
// has a more strict permission policy for cmd.exe
}
}
if (url.startsWith("http") || url.startsWith("file:")) {
// this allows to open the file on Windows 10 that
// has a more strict permission policy for cmd.exe
final int SW_SHOW = 5;
Shell32.INSTANCE.ShellExecute(null, null, url, null, null, SW_SHOW);
return;
}
// this is not guaranteed to work, because who knows if the
// path will always be c:\progra~1 et al. also if the user has
@ -150,18 +155,12 @@ public class Platform extends processing.app.Platform {
// "Access is denied" in both cygwin and the "dos" prompt.
//Runtime.getRuntime().exec("cmd /c " + currentDir + "\\reference\\" +
// referenceFile + ".html");
if (url.startsWith("http") || url.startsWith("file:")) {
// open dos prompt, give it 'start' command, which will
// open the url properly. start by itself won't work since
// it appears to need cmd
Runtime.getRuntime().exec("cmd /c start \"\" \"" + url + "\"");
} else {
// just launching the .html file via the shell works
// but make sure to chmod +x the .html files first
// also place quotes around it in case there's a space
// in the user.dir part of the url
Runtime.getRuntime().exec("cmd /c \"" + url + "\"");
}
// just launching the .html file via the shell works
// but make sure to chmod +x the .html files first
// also place quotes around it in case there's a space
// in the user.dir part of the url
Runtime.getRuntime().exec("cmd /c \"" + url + "\"");
}