加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-14 06:08:22 所属栏目:Java 来源:网络整理
导读:我想从我的Java程序中使用linux命令行工具.我启动程序并使用Process类(http://download.oracle.com/javase/6/docs/api/java/lang/Process.html)获取输出: /* @param args * @throws IOException */ public static void main(String[] args) throws IOExcept

我想从我的Java程序中使用linux命令行工具.我启动程序并使用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");
 }

我得到以下输出:

GNU Octave,version 3.0.5 Copyright
(C) 2008 John W. Eaton and others.
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”.

Additional information about Octave is
available at 07001.

Please contribute if you find this
software useful. For more information,
visit
07002

Report bugs to (but
first,please read
07003 to
learn how to write a helpful report).

For information about changes from
previous versions,type `news’.

奇怪的是,如果我在终端中运行八度音程,则正常输出如下:

:~/workspace/Console/src/c$octave
GNU Octave,version 3.0.5
Copyright (C) 2008 John W. Eaton and others.
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”.

Additional information about Octave is available at 07001.

Please contribute if you find this software useful.
For more information,visit 07002

Report bugs to (but first,please read
07003 to learn how to write a helpful report).

For information about changes from previous versions,type `news’.

octave:1>

因此,请求输入的行中的字符不会在我的输入流中发送.为什么?是否可以检测是否请求输入?

谢谢你的回答!

海因里希

最佳答案
* nix上的程序可以检测它们是否正在与终端或其他流进行通信.许多交互式shell类型的程序基于此做出不同的反应(通过设置不同的提示,而不是读取一些init文件,甚至根本不启动).

您可能遇到其中一种情况.

此外,也许使用Java API进行八度音阶可能是更简单的方法:例如joPAS.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读