Does anyone know if it's possible to open a URL in a browser through Greenfoot?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | /** * Let the given URL be shown in a browser window. * * @param url the URL or file path to be shown. * @return true if the web browser could be started, false otherwise. */@OnThread(Tag.Swing)public static boolean openWebBrowser(String url){ if (Config.isWinOS()) { // Windows String cmd; // catering for stupid differences in Windows shells... if (Config.osname.startsWith("Windows 9") || Config.osname.equals("Windows Me")) // win95/98/Me cmd = "command.com"; else // other cmd = "cmd.exe"; try { // more stupid Windows differences... if (Config.osname.startsWith("Windows 98") || Config.osname.equals("Windows Me")) { Runtime.getRuntime().exec(new String[]{cmd, "/c", "start", '"' + url + '"'}); } else { Runtime.getRuntime().exec(new String[]{cmd, "/c", "start", "\"\"", '"' + url + '"'}); } } catch (IOException e) { Debug.reportError("could not start web browser. exc: " + e); return false; } } else { // Mac, Unix and other // The string should be either a URL or a file path try { return openWebBrowser(new URL(url)); } catch (MalformedURLException mfue) { return openWebBrowser(new File(url)); } } return true;} |
1 2 3 4 5 | try { Runtime.getRuntime().exec(new String[] {"cmd.exe", "/c", "start", "\"\"", '"' + "https://www.greenfoot.org/topics/59418/0#post_119190" + '"'});} catch (IOException ex) { System.out.println(ex.getMessage());} |
1 |