如何使用java程序在我的电脑中打开任何已安装的软件?

时间:2017-10-31 20:13:12

标签: java

我想使用java程序在我的电脑上打开已安装的软件。例如 - 如果我想使用java程序打开Microsoft Outlook,我该怎么办?在此先感谢!!

1 个答案:

答案 0 :(得分:0)

您可以使用Java ProcessBuilder启动任何程序。

来自this site

的示例
package com.javacodegeeks.process;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class ProcessBuilderExample {
    public static void main(String[] args) throws InterruptedException,
            IOException {
        ProcessBuilder pb = new ProcessBuilder("echo", "This is ProcessBuilder Example from JCG");
        System.out.println("Run echo command");
        Process process = pb.start();
        int errCode = process.waitFor();
        System.out.println("Echo command executed, any errors? " + (errCode == 0 ? "No" : "Yes"));
        System.out.println("Echo Output:\n" + output(process.getInputStream()));    
    }

    private static String output(InputStream inputStream) throws IOException {
        StringBuilder sb = new StringBuilder();
        BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(inputStream));
            String line = null;
            while ((line = br.readLine()) != null) {
                sb.append(line + System.getProperty("line.separator"));
            }
        } finally {
            br.close();
        }
        return sb.toString();
    }
}