使用进程.exec

发布于 2024-09-13 22:09:34 字数 285 浏览 6 评论 0原文

我想在我的程序中间执行一个批处理文件(这将启动另一个java应用程序)。 我不想等待或查看它是否成功执行,也不想捕获执行该批处理文件时出现的错误。在启动该批处理文件后,我想做其他事情,而不是在执行该批处理后等待它。

我需要处理 stdout 和 stderr 吗? 有什么方法可以摆脱对 stdout 和 stderr 的照顾。

这是我的第二篇文章,旨在消除我对此主题的困惑,因此请具体说明问题,不要抛出问题 有关如何 process.exe 或 processbuilder 的链接。

任何帮助表示赞赏。

I want to execute a batch file (this will start another java app) in the middle of my program.
I dont want to wait or see whether it executed successfully nor I wanted to capture erros from executing that batch file. After I started that batch file , I want to do other stuff rather than waiting for it after i execute that batch.

Do I need to take care of stdout and stderr?
IS there any way to get rid of taking care of stdout and stderr.

This is my second post to clear my confusion on this topic so please be specific to the question and just dont throw the
link for how to process.exe or processbuilder.

Any help is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

寻找一个思念的角度 2024-09-20 22:09:34

简短回答:否。根据流程' javadocs ,

由于某些原生平台只为标准输入输出流提供有限的缓冲区大小,未能及时写入子进程的输入流或读取子进程的输出流可能会导致子进程阻塞,甚至死锁。

因此,如果您希望程序具有远程鲁棒性,则必须注意 stderr 和 stdout 处理。

话虽如此,如果您真的不关心它们的内容(这是一个坏主意 - 如果批处理失败并显示您丢弃的有用错误消息怎么办?),您可以直接关闭线程按照 Jay R. 的回答阅读它们。这将使您的逻辑线程继续运行,而不必担心流的状态,并且流消耗者将在后台运行,直到流耗尽。如果您发现自己经常这样做,您甚至可能想创建一个围绕 Runtime.exec() 的包装器来触发线程来为您执行此操作。

但是,如果您不打算在代码中解释它,我至少会记录批处理过程的输出。当批次出现问题时,通过仔细研究流程的输出来分析问题会容易得多。

Short answer: No. As per Process' javadocs,

Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, and even deadlock.

So you have to take care of the stderr and stdout handling if you want your program to be remotely robust.

Having said that, if you really don't care about their content (which is a bad idea - what if the batch fails with a useful error message that you discard?), you can just fire off threads to read from them, as per Jay R.'s answer. This will let your logic thread continue without worrying about the state of the streams, and the stream eaters will run in the background until the streams are exhausted. You might even want to create a wrapper around Runtime.exec() that fires off threads to do this for you, if you find yourself doing this a lot.

I would, however, at least log the output from the batch process, if you're not going to intepret it in your code. When something goes wrong with the batch, analyzing the problem will be much easier with the process' output to pore over.

节枝 2024-09-20 22:09:34

我发现如果你不至少吃掉 stdout 和 stderr 最终你会耗尽内存。它还阻止我运行多个 同时处理

我一直在使用一个名为 ProcessStreamEater 的类来执行此操作。

public class ProcessStreamEater implements Runnable
{
   private final Process proc;

   public ProcessStreamEater(Process proc)
   {
      this.proc = proc;
   }

   @Override
   public void run()
   {
      InputStreamReader r = new InputStreamReader(proc.getInputStream());
      try
      {
         while(r.read() != -1)
         {  // put stuff here if you want to do something with output
            // otherwise, empty
         }
      }
      catch(IOException e)
      {
         // handle IO exception
      }
      finally
      {
         if(r != null)
         {
            try
            {
               r.close();
            }
            catch(IOException c)
            {}
         }
      }
   }
}

然后当我用它来吃东西时...

   ProcessBuilder pb = new ProcessBuilder(args);
   pb.redirectErrorStream(true);
   final Process proc = pb.start();
   executorService.execute(new ProcessStreamEater(proc));

executorService 是用 Executors.newCachedThreadPool()

I have found that if you don't at least eat the stdout and stderr eventually you will run out of memory. It was also preventing me from running more than one Process simultaneously.

I've been using a class I called ProcessStreamEater to do this.

public class ProcessStreamEater implements Runnable
{
   private final Process proc;

   public ProcessStreamEater(Process proc)
   {
      this.proc = proc;
   }

   @Override
   public void run()
   {
      InputStreamReader r = new InputStreamReader(proc.getInputStream());
      try
      {
         while(r.read() != -1)
         {  // put stuff here if you want to do something with output
            // otherwise, empty
         }
      }
      catch(IOException e)
      {
         // handle IO exception
      }
      finally
      {
         if(r != null)
         {
            try
            {
               r.close();
            }
            catch(IOException c)
            {}
         }
      }
   }
}

Then when I use it to eat stuff...

   ProcessBuilder pb = new ProcessBuilder(args);
   pb.redirectErrorStream(true);
   final Process proc = pb.start();
   executorService.execute(new ProcessStreamEater(proc));

where executorService was created with Executors.newCachedThreadPool()

私藏温柔 2024-09-20 22:09:34

如果您的进程没有向 stdout 或 stderror 产生任何输出,那么您可能不需要处理它。如果您的子进程确实产生输出,它在尝试写入标准输出时可能会永远阻塞,这取决于操作系统管道缓冲的大小。这里有一个流刷新器的例子——每个人都使用它。

If your process does not produce any output to stdout or stderror then you probably don't need to handle it. If your sub process does produce output it may block forever while trying to write to stdout, it depends on how much the OS pipes buffer. There is an example somewhere here of a stream flusher -- which everyone uses.

朱染 2024-09-20 22:09:34

我认为你只需要这个:

Runtime run = Runtime.getRuntime();  
Process p = null;  
String cmd = "D:\\a.bat";     
try {     
  p = run.exec(cmd);
}catch(Exception e){
  //do handling
}

//your code

请记住稍后在代码中调用 p.destroy() 。
您还可以在单​​独的线程中生成上面的代码。
希望这有帮助。

I think you require only this :

Runtime run = Runtime.getRuntime();  
Process p = null;  
String cmd = "D:\\a.bat";     
try {     
  p = run.exec(cmd);
}catch(Exception e){
  //do handling
}

//your code

Remember to call p.destroy() later in your code.
You could also spawn above code in separate thread.
Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文