Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中与命令行工具通信_Java_Linux_Process - Fatal编程技术网

在Java中与命令行工具通信

在Java中与命令行工具通信,java,linux,process,Java,Linux,Process,我想使用Java程序中的linux命令行工具。我启动程序并使用Process类()获得输出: 我得到以下输出: GNU倍频程,版本3.0.5版权所有 (C) 2008年约翰·W·伊顿等人。 这是自由软件;见出处 复制条件的代码。有 绝对没有保证;甚至对我来说都不是 适销性,适销性 特殊目的。有关详细信息,请键入 `保修” 倍频程配置为 “i486 pc linux gnu” 有关倍频程的附加信息如下所示 可于 如果你发现这个,请贡献 软件很有用。欲了解更多信息, 参观 向(但是)报告错误 首先

我想使用Java程序中的linux命令行工具。我启动程序并使用Process类()获得输出:

我得到以下输出:

GNU倍频程,版本3.0.5版权所有 (C) 2008年约翰·W·伊顿等人。 这是自由软件;见出处 复制条件的代码。有 绝对没有保证;甚至对我来说都不是 适销性,适销性 特殊目的。有关详细信息,请键入 `保修”

倍频程配置为 “i486 pc linux gnu”

有关倍频程的附加信息如下所示 可于

如果你发现这个,请贡献 软件很有用。欲了解更多信息, 参观

向(但是)报告错误 首先,请阅读 到 学习如何编写有用的报告)

有关更改的信息,请参见 以前的版本,键入“新闻”

奇怪的是,如果在终端中运行倍频程,正常输出如下:

:~/workspace/Console/src/c$octave
GNU倍频程,版本3.0.5 版权所有(C)2008约翰·W·伊顿等人。 这是自由软件;有关复制条件的信息,请参阅源代码。 绝对没有保证;甚至不考虑适销性或 适合某一特定目的。有关详细信息,请键入“保修”

Octave是为“i486 pc linux gnu”配置的

有关倍频程的更多信息,请访问

如果您觉得此软件有用,请提供帮助。 有关更多信息,请访问

将错误报告给(但首先,请阅读 学习如何编写有用的报告)

有关以前版本更改的信息,请键入“新闻”

倍频程:1>

因此,请求输入的行中的字符不会发送到我的输入流中。为什么?难道不能检测是否请求输入吗

谢谢你的回答


Heinrich

在*nix上的程序可以检测它们是否正在与终端或另一个流通话。许多交互式shell类型的程序基于此做出不同的反应(通过设置不同的提示,不读取某些init文件,甚至根本不启动)

你可能会遇到这样的情况


另外,可能对octave使用Java API可能是更简单的方法:,例如。

由于您的问题与octave有关,我建议对octave使用--silent选项,并在命令行上一次性传递所有参数。这将解决前面强调的有关启动终端会话的问题。

是否会为此提示打开一个新的文件描述符

您可能会在octave源代码(或者如果octave使用该源代码,则在readline源代码)中找到它。

您不会得到提示“octave:1>”,因为octave的输出正在被缓冲。许多在Unix/Linux上使用stdio的程序,如果输出不到交互设备,也会做同样的事情。直到缓冲区填满(自动刷新)或调用fflush(3)的程序显式刷新缓冲区,您才会收到输出

如果您真的想与命令行程序交互,那么您需要使用pty(我不知道java可以实现这一点,因为我从未尝试过它)

节选自“man stdio”,解释了发生的事情:


我终于可以解决这个问题了:在Linux下,使用Octave和--interactive,最终是--no-line编辑选项,它可以工作:)


Heinrich

不幸的是,这对我来说是不可能的,因为我想在Java中以交互方式使用octave。感谢链接到joPAS。我来看看API。无论如何,我仍然很好奇为什么我的Java程序中没有“octave:1>”字符。
 /* @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");
 }
  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.