Java calls up the browser to open the specified page

Keywords: Programming Java

Editor of xl_echo, welcome to reprint, reprint please declare the source of the article. Welcome to add echo Wechat (micro signal: t2421499075) for communication and learning. Never lose a hundred battles, never claim to win, never fail, and always strive to move forward. —— This is the real strength!!

This function is really a whim to try whether java has this ability, but the practicability is very poor, and the operation is complex. It is also inconvenient to execute. If you really want to achieve this function, you can write directly using js, simple and fast

package com.example.mybatisplusdemo.temp;

import java.io.IOException;

/**
 * @author xlecho
 */
public class OpenBrowse {

    private static void openBrowse(String[] urls) {
        for (String url : urls) {
            if (java.awt.Desktop.isDesktopSupported()) {
                try {
                    //Create a URI instance, note that it's not a URL
                    java.net.URI uri = java.net.URI.create(url);
                    //Get the current system desktop extension
                    java.awt.Desktop dp = java.awt.Desktop.getDesktop();
                    //Determine whether the system desktop supports the functionality to be performed
                    if (dp.isSupported(java.awt.Desktop.Action.BROWSE)) {
                        //Get the system default browser to open the link
                        dp.browse(uri);
                    }
                } catch (NullPointerException | IOException e) {
                    e.printStackTrace();
                }
            }
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }

    private static void closeBrowse() {
        try {
            Runtime.getRuntime().exec("taskkill /F /IM chrome.exe");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        String[] urls = {"https://shop136815572.taobao.com/shop/view_shop.htm?spm=a211vu.server-home.favorite.d53.627a5e16x7ttNb&mytmenu=mdianpu&user_number_id=1848993149",
                "https://item.taobao.com/item.htm?spm=a1z10.1-c-s.w4004-21735248043.2.5b065e021D00tn&id=598627677953"};
        openBrowse(urls);
        closeBrowse();
    }

}

Welcome to reward.

Posted by homer.favenir on Sun, 13 Oct 2019 10:59:56 -0700