mercredi 26 septembre 2018

Close or modify the current tab url in browser Java

I want to close or modify the current tab url in browser, with a Java program. Here is my code to open my file in a browser (I want it to be crossplateform) :

public static void openInWebBrowser(String url) {

    File tmpfile = null;
    try {
        tmpfile = File.createTempFile("tempBrwsr", ".html");
        System.out.println(tmpfile.getAbsolutePath());
        tmpfile.deleteOnExit();
        BufferedWriter bw = new BufferedWriter(new FileWriter(tmpfile));
        bw.write("<html><head><meta http-equiv=\"refresh\" content=\"0;url=file:///" + url + "\" /></head></html>");
        bw.close();
    } catch (IOException e) {
        System.err.println("[ err. ] " + e);
    }

    String os = System.getProperty("os.name").toLowerCase();
    Runtime rt = Runtime.getRuntime();
    if (os.indexOf("win") >= 0) {
        try {
            rt.exec("rundll32 URL.dll,FileProtocolHandler \"" + tmpfile.toURI().toString() + "\"");
        } catch (IOException e) {
            System.err.println("[ err. ] " + e);
        }
    } else if (os.indexOf("nix") >= 0 || os.indexOf("nux") >= 0) {
        try {
            rt.exec("xdg-open " + url);
        } catch (IOException e) {
            System.err.println("[ err. ] " + e);
        }
    } else {
        JOptionPane.showMessageDialog(null, "Not supported operating system : " + os, "Error",
                JOptionPane.ERROR_MESSAGE);
    }

}

Can someone help me ?




Aucun commentaire:

Enregistrer un commentaire