简体   繁体   English

Java在操作系统中执行命令

[英]Java executing commands in Operating System

I have a Java program that executes specific commands into the OS. 我有一个Java程序,可以在OS中执行特定的命令。 Am also using Process.waitfor() as showing in the code below to indicates if the execution completed successfully or failed. 我还使用Process.waitfor(),如下面的代码所示,以指示执行成功完成还是失败。

My question is, is there any other way to avoid using process.waitfor(), Is there a way to use while loop and perform certain action until the process is completed? 我的问题是,还有其他方法可以避免使用process.waitfor(),是否可以使用while循环并执行某些操作直到该过程完成?

            Runtime rt = Runtime.getRuntime();

        Process p = rt.exec(cmdFull);

        BufferedReader inStream = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String inStreamLine = null;
        String inStreamLinebyLine=null;
        while((inStreamLine = inStream.readLine()) == null) {
          inStreamLinebyLine = inStreamLinebyLine+"\n"+inStreamLine;
        }


        try {
            rc = p.waitFor();

        } catch (InterruptedException intexc) {
            System.out.println("Interrupted Exception on waitFor: " +
                               intexc.getMessage());
        }    

Waht I wish to do, is something like this 我想做的是这样的

            Runtime rt = Runtime.getRuntime();

        Process p = rt.exec(cmdFull);

        BufferedReader inStream = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String inStreamLine = null;
        String inStreamLinebyLine=null;
        while((inStreamLine = inStream.readLine()) == null) {
          inStreamLinebyLine = inStreamLinebyLine+"\n"+inStreamLine;
        }


        try {

            while ((rc = p.waitFor()) == true ) { // This is made up, I don't even think it would work
                System.out.println('Process is going on...');
            }


        } catch (InterruptedException intexc) {
            System.out.println("Interrupted Exception on waitFor: " +
                               intexc.getMessage());
        }    

Thanks, 谢谢,

You could spawn a new thread before starting the process. 您可以在开始该过程之前产生一个新线程。

The new thread would be responsible for printing out "Process is going on..." or whatever is needed. 新线程将负责打印“正在处理...”或任何所需的内容。

After p.waitFor() finishes, the main thread that launched the process would indicate to the new thread that it should stop running. 在p.waitFor()完成之后,启动进程的主线程将向新线程指示它应停止运行。

您可以生成一个新线程并在线程中进行等待,并通过共享变量从主线程定期检查等待线程是否已完成。

Maybe something like this would work. 也许这样的事情会起作用。 Create a Thread as @tschaible suggested, then do a join on a that thread with a timeout (which is the part you made up in your code). 按照@tschaible的建议创建一个线程,然后对该线程进行超时连接(这是您在代码中组成的部分)。 It would look something like this: 它看起来像这样:

Thread t = new Thread(new Runnable() { 

  public void run() {
    // stuff your code here
  }

});
t.run();

while (t.isAlive()) {
  t.join(1000); // wait for one second
  System.out.println("still waiting");
}

What this does is launch the code as a separate thread and then test if the tread finished every one second. 这样做是将代码作为单独的线程启动,然后测试胎面是否每秒完成一次。 The while loop should end when thread finishes and is no longer alive. while循环应在线程完成时结束,并且不再活动。 You might have to check for InterruptedException but I can't test that now. 您可能必须检查InterruptedException,但现在无法测试。

Hope this sends you in the right direction. 希望这能给您正确的方向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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