简体   繁体   English

使用Java中的命令行工具进行通信

[英]Communicating with a command line tool in Java

I want to use a linux command line tool from my Java program. 我想从我的Java程序中使用linux命令行工具。 I start the program and get the output using the Process class ( http://download.oracle.com/javase/6/docs/api/java/lang/Process.html ): 我启动程序并使用Process类( http://download.oracle.com/javase/6/docs/api/java/lang/Process.html )获取输出:

 /* @param args
  * @throws IOException 
  */
 public static void main(String[] args) throws IOException {
    Process proc = Runtime.getRuntime().exec("octave");

    BufferedReader reader = 
        new BufferedReader(new InputStreamReader(proc.getInputStream()));

    BufferedReader errorReader = 
        new BufferedReader(new InputStreamReader(proc.getInputStream()));

    BufferedWriter writer = 
        new BufferedWriter(new OutputStreamWriter(proc.getOutputStream()));

    int c;
    while((c = proc.getInputStream().read()) != -1) {
       System.out.print((char)c);
    }
    System.out.println("End");
 }

I get the following output: 我得到以下输出:

GNU Octave, version 3.0.5 Copyright (C) 2008 John W. Eaton and others. GNU Octave,版本3.0.5版权所有(C)2008 John W. Eaton等。 This is free software; 这是免费软件; see the source code for copying conditions. 查看复制条件的源代码。 There is ABSOLUTELY NO WARRANTY; 绝对无保证; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 甚至不适用于适销性或特定用途的适用性。 For details, type `warranty'. 有关详细信息,请键入“保修”。

Octave was configured for "i486-pc-linux-gnu". Octave配置为“i486-pc-linux-gnu”。

Additional information about Octave is available at http://www.octave.org . 有关Octave的更多信息,请访问http://www.octave.org

Please contribute if you find this software useful. 如果您发现此软件有用,请提供帮助。 For more information, visit http://www.octave.org/help-wanted.html 有关更多信息,请访问http://www.octave.org/help-wanted.html

Report bugs to (but first, please read http://www.octave.org/bugs.html to learn how to write a helpful report). 报告错误(但首先请阅读http://www.octave.org/bugs.html以了解如何编写有用的报告)。

For information about changes from previous versions, type `news'. 有关以前版本更改的信息,请键入“news”。

The strange thing is the normal output if I run octave in the Terminal is the following: 奇怪的是,如果我在终端中运行八度音程,则正常输出如下:

:~/workspace/Console/src/c$ octave :〜/ workspace / Console / src / c $ octave
GNU Octave, version 3.0.5 Copyright (C) 2008 John W. Eaton and others. GNU Octave,版本3.0.5版权所有(C)2008 John W. Eaton等。 This is free software; 这是免费软件; see the source code for copying conditions. 查看复制条件的源代码。 There is ABSOLUTELY NO WARRANTY; 绝对无保证; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 甚至不适用于适销性或特定用途的适用性。 For details, type `warranty'. 有关详细信息,请键入“保修”。

Octave was configured for "i486-pc-linux-gnu". Octave配置为“i486-pc-linux-gnu”。

Additional information about Octave is available at http://www.octave.org . 有关Octave的更多信息,请访问http://www.octave.org

Please contribute if you find this software useful. 如果您发现此软件有用,请提供帮助。 For more information, visit http://www.octave.org/help-wanted.html 有关更多信息,请访问http://www.octave.org/help-wanted.html

Report bugs to (but first, please read http://www.octave.org/bugs.html to learn how to write a helpful report). 报告错误(但首先请阅读http://www.octave.org/bugs.html以了解如何编写有用的报告)。

For information about changes from previous versions, type `news'. 有关以前版本更改的信息,请键入“news”。

octave:1> 八度:1>

So the characters in the line where the input is requested are not sent in my input stream. 因此,请求输入的行中的字符不会在我的输入流中发送。 Why? 为什么? Isn't it possible to detect whether input is requested? 是否可以检测是否请求输入?

Thanks for your answers! 谢谢你的回答!

Heinrich 海因里希

Programs on *nix can detect if they are talking to a terminal or another stream. * nix上的程序可以检测它们是否正在与终端或其他流进行通信。 And many interactive-shell-type programs react differently based on this (by setting different prompts, not reading some init files or even not starting at all). 许多交互式shell类型的程序基于此做出不同的反应(通过设置不同的提示,而不是读取一些init文件,甚至根本不启动)。

You may be running into one of those situations. 您可能遇到其中一种情况。

Also, maybe using a Java API for octave may be the easier way: joPAS , for example. 此外,也许使用Java API进行八度音阶可能是更简单的方法: 例如joPAS

Since your problem appears octave specific, I suggest using the --silent option for octave and passing all parameters on the command line in a single hit. 由于你的问题看起来是八度特定的,我建议使用--silent选项进行八度音阶并在命中行中传递所有参数。 This will work around the problems highlighted earlier about starting a terminal session. 这将解决早先强调的有关启动终端会话的问题。

You don't get the prompt, "octave:1> ", because octave's output is being buffered. 你没有得到提示“octave:1>”,因为octave的输出正在被缓冲。 Many programs that use stdio on Unix/Linux will do the same thing if the output is not to an interactive device. 如果输出不是交互式设备,那么在Unix / Linux上使用stdio的许多程序都会做同样的事情。 You won't receive the output until the buffer fills (automatically flushed) or the buffer is explicitly flushed by the program calling fflush(3). 在缓冲区填满(自动刷新)或缓冲区被调用fflush(3)的程序显式刷新之前,您将不会收到输出。

If you really want to interact with a command line program, then you need to use a pty (something that I don't know is possible with java since I've never tried it). 如果你真的想与命令行程序进行交互,那么你需要使用一个pty(因为我从来没有尝试过,所以我不知道这是不可能的。)

Excerpt from "man stdio" that explains what is going on: 摘自“man stdio”,解释发生了什么:

  At program startup, three text streams are predefined and need not be opened explicitly -- standard input (for reading conventional input), standard output (for writing conventional input), and standard error (for writing diagnostic output). These streams are abbreviated stdin,stdout and stderr. When opened, the standard error stream is not fully buffered; the standard input and output streams are fully buffered if and only if the streams do not to refer to an interactive device. Output streams that refer to terminal devices are always line buffered by default; pending output to such streams is written automatically whenever an input stream that refers to a terminal device is read. In cases where a large amount of computation is done after printing part of a line on an output terminal, it is necessary to fflush(3) the stan- dard output before going off and computing so that the output will appear. 

Could it be that it opens a new file descriptor for this prompt? 可能是它为此提示打开了一个新的文件描述符?

You might find that in the octave source code (or the readline source code if octave uses that). 您可能会在八度音源代码中找到它(如果八度音程使用该代码,则为readline源代码)。

I could finally solve the problem: Under Linux, use Octave with the --interactive and eventually the --no-line-editing optionand it worked :) 我终于可以解决这个问题:在Linux下,使用带有--interactive的Octave,最后使用--no-line-editing选项,它有效:)

Heinrich 海因里希

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

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