简体   繁体   English

无法从java程序(Eclipse)运行特定的终端命令

[英]Not able to run specific terminal commands from java program(Eclipse)

Using ProcessBuilder in JAVA I am able to run simple terminal commands like ls,pwd,echo etc..etc... But following code is getting terminated , don't know why?? 在JAVA中使用ProcessBuilder我能够运行简单的终端命令,如ls,pwd,echo等等......但是下面的代码被终止,不知道为什么?

public static void main(String[] args) throws Exception
{
    Runtime r = Runtime.getRuntime();
    Process p = r.exec("echo 'T W O N E I G H T' | /home/saj/g2p/mosesdecoder-master/bin/moses -f /home/saj/g2p/working/binarised-model/moses.ini");
    p.waitFor();

    BufferedReader b = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String line = "";
    while ((line = b.readLine()) != null) 
    {
      System.out.println(line);
    } 
}

This command works perfectly from terminal and takes around 15 seconds to run and gives output. 此命令在终端上运行良好,大约需要15秒才能运行并提供输出。 I have gone through similar topics on stackoverflow but did not found any help. 我在stackoverflow上经历了类似的主题,但没有找到任何帮助。 Please help in this regard. 请帮忙。 Thanks in advance. 提前致谢。

Refer this code this may help u....use bash.Just replace your command with echo command 请参考此代码,这可能有助于你....使用bash。只需用echo命令替换你的命令

ProcessBuilder b = new ProcessBuilder("bash","-c","echo abc"); ProcessBuilder b = new ProcessBuilder(“bash”,“ - c”,“echo abc”);

    Process termProc = null;

    try {
        termProc = b.start();
        BufferedReader stdInput = new BufferedReader(new 
        InputStreamReader(termProc.getInputStream()));
        BufferedReader stdError = new BufferedReader(new 
        InputStreamReader(termProc.getErrorStream()));

        String s = null;
        while ((s = stdInput.readLine()) != null) 
        {
            System.out.println(s);


         }
        while ((s = stdError.readLine()) != null) 
        {

            System.out.println(s);

        }

    } catch (IOException e) {

        e.printStackTrace();
    }

Consider monitoring both the InputStream and ErrorStream. 考虑监视InputStream和ErrorStream。 Chances are that the output is probably getting written to the ErrorStream and that's why you don't see anything getting displayed. 有可能输出可能被写入ErrorStream,这就是为什么你没有看到任何显示的东西。

Here's a good example from Javaworld regarding some of the common pitfalls of Runtime.exec() and how to go about using it. 以下是 Javaworld关于Runtime.exec()的一些常见缺陷以及如何使用它的一个很好的例子


I missed it at the first look at your code but here's a hint to what you might be doing wrong: 我在第一次看你的代码时错过了它,但这里暗示了你可能做错了什么:

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. 由于某些本机平台仅为标准输入和输出流提供有限的缓冲区大小,因此无法及时写入输入流或读取子进程的输出流可能导致子进程阻塞甚至死锁。

Consider an update to your code along these lines: 考虑按以下方式更新代码:

 public static void main(String[] args) throws Exception { Runtime r = Runtime.getRuntime(); Process p = r.exec("echo 'TWONEIGHT' | /home/saj/g2p/mosesdecoder-master/bin/moses -f /home/saj/g2p/working/binarised-model/moses.ini"); BufferedReader b = new BufferedReader(new InputStreamReader(p.getInputStream())); String line = ""; while ((line = b.readLine()) != null) { System.out.println(line); } BufferedReader b = new BufferedReader(new InputStreamReader(p.getErrorStream())); String line = ""; while ((line = b.readLine()) != null) { System.out.println(line); } p.waitFor(); } 

Note: I would suggest creating threads to handle displaying your output from inputstream and errorstream as in the example link that I posted above. 注意:我建议创建线程来处理输入来自inputstream和errorstream的输出,如我在上面发布的示例链接中所示。

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

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