使用Java中的命令行程序

时间:2013-05-15 11:13:24

标签: java command-line

如何在Java中使用命令行程序?

我正在尝试通过java将点语言中的图形定义(请参阅Wikipedia)传递给解释程序点(请参阅GraphViz)。

问题是,在我将点图发送到其InputStream之后程序没有回答,因为它不知道,我已经完成了发送描述。

这就是我目前所拥有的:

package exercise4;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;

public class Main {
    public static void main(String[] args) {
        PrintStream out = System.out;
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

        try {
            final String start =
                "strict graph LSR%1$d {\n" +
                " node [shape=circle color=lightblue style=filled];\n\n" +
                " {rank=same; A--B [label=6];}\n" +
                " {rank=same; C--D [label=12]; D--E [label=4];}\n" +
                " A--C [label=4]; B--D [label=4]; B--E [label=9];\n\n" +
                " node [shape=record color=\"#000000FF\" fillcolor=\"#00000000\"];\n}\n";
            Process dot = Runtime.getRuntime().exec("dot -Tsvg");
            in = new BufferedReader(new InputStreamReader(System.in));
            out = new PrintStream(dot.getOutputStream(), false, "UTF-8");
            out.printf(start, 0);

            out.flush();
            out.close();

            while(in.ready()) {
                System.out.println(in.readLine());
            }
            in.close();
            dot.destroy();
        } catch (UnsupportedEncodingException ex) {
        } catch (IOException ex) {
        } finally {
            out.close();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

看起来好像是在读错了输入流。看看这个答案:https://stackoverflow.com/a/4741987/1686330