简体   繁体   中英

How to open an external program in java

I'm looking to create a desktop helper program with Java, a couple JOptionPane option dialogues, and in that I would like to open programs (both internal and external, internal being something like notepad, external being a game like league of legends, something that doesn't come with Windows if that makes any sense whatsoever) I also want to open webpages, but I've got that worked out with this code:

try {
    Desktop.getDesktop().browse(new URI("http://www.gamestop.com/wii-u/games/super-smash-bros/114504"));
    giftinfo();
} catch (IOException e) {
    e.printStackTrace();
} catch (URISyntaxException e) {
    e.printStackTrace();
}

Is there a way to open other (non Java) programs from a Java program?

Is short, yes you can open every inbuilt application has a command that it can be ran from run command window. Here is an example for opening notepad.

public static void main(String[] args) {
    try {
        System.out.println("Notepad Opening");
        Runtime runTime = Runtime.getRuntime();
        Process process = runTime.exec("notepad");
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Notepad Closing");
        process.destroy();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM