有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何知道我的ProcessBuilder start()是否成功执行了这些命令

我正在使用ProcessBuilder类在java代码中执行一个命令。一旦start()被调用,我就完成了执行。每次运行这段代码时,我都会转到output并验证java代码是否成功运行了该命令。问题是,我能在代码本身中验证这一点吗?,而不是每次都验证我的输出。将来我可能无法查看输出,因此我希望确保命令成功运行,或者即使遇到异常,我也应该能够从代码中知道

start()正在返回Process类对象,我几乎找不到Process类下可用的任何有用方法

代码:

File file = new File(getClass().getClassLoader().getResource(fileName).getFile());

List<String> list = new ArrayList<String>();
list.add(my_command);

String absPath = file.getAbsolutePath();
list.add(absPath);
ProcessBuilder pb = new ProcessBuilder(list);
Process process = pb.start();
List<String> commandList = pb.command();

非常感谢你的帮助

编辑:
尝试了process.exitValue();,但每次执行代码时都返回以下错误。需要注意的是,即使我在控制台中看到这个异常,我的输出也不会出现任何问题

Exception in thread "main" java.lang.IllegalThreadStateException: process has not exited
    at java.lang.ProcessImpl.exitValue(ProcessImpl.java:246)
    at com.rwithjava.caller.RWithJavaCaller.processRWithJavaScript(RWithJavaCaller.java:39)
    at com.rwithjava.caller.RWithJavaCaller.main(RWithJavaCaller.java:51)

共 (1) 个答案

  1. # 1 楼答案

    如果你打电话给pb。waitFor,您的程序将等待进程终止并返回其结果代码(如果一切顺利,结果代码应为零)

    如果这还不够,您应该捕获标准输出和标准错误流,并检查是否达到预期效果

    更新

    示例代码:

            Process process = pb.start();
            OutputHandler out
                    = new OutputHandler(process.getInputStream(), "UTF-8");
            OutputHandler err
                    = new OutputHandler(process.getErrorStream(), "UTF-8");
            int status = process.waitFor();
            System.out.println("Status: " + status);
            out.join();
            System.out.println("Output:");
            System.out.println(out.getText());
            System.out.println();
            err.join();
            System.out.println("Error:");
            System.out.println(err.getText());
    

    OutputHandler

    class OutputHandler extends Thread {
        private final StringBuilder buf = new StringBuilder();
        private final BufferedReader in;
    
        OutputHandler(InputStream in, String encoding)
                throws UnsupportedEncodingException {
            this.in = new BufferedReader(new InputStreamReader(
                    in, encoding == null ? "UTF-8" : encoding));
            setDaemon(true);
            start();
        }
    
        String getText() {
            synchronized(buf) {
                return buf.toString();
            }
        }
    
        @Override
        public void run() {
            // Reading process output
            try {
                String s = in.readLine();
                while (s != null) {
                    synchronized(buf) {
                        buf.append(s);
                        buf.append('\n');
                    }
                    s = in.readLine();
                }
            } catch (IOException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                try {
                    in.close();
                } catch (IOException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }