0

我在从 Java 运行 gnuplot 进程时遇到了一些问题。我正在创建一个 gnuplot 脚本文件,然后从 java 程序中运行它。我已经尝试使用 Process Builder 并使用 Runtime.getRuntime().exec("blah blah...") 构建一个进程,但两者都没有完整的工作能力。有趣的是,只要我通过 gnuplot 创建的图像文件没有保存到名称中没有空格的目录中,使用 Runtime 就可以使该过程几乎完美地工作。然而,ProcessBuilder 根本不起作用,并给我错误:“CreateProcess error=2,系统找不到指定的文件”

我花了很长时间才弄清楚这些东西,所以任何帮助都将不胜感激。

我使用的代码在这里:

File script = new File("Demo.plt");    //Script file that outputs to a PNG file

//This works as long as the script file doesn't output to a png with a space in it's filepath
Process aProcess = Runtime.getRuntime().exec("gnuplot " + script.toString());
Thread.currentThread().sleep(1000);
aProcess.waitFor();

//This doesn't work at all
ProcessBuilder builder = new ProcessBuilder("gnuplot " + script.toString());
builder.redirectErrorStream(true);
Process process = builder.start();

而且我知道脚本如果在 Java 之外运行,无论输出行中的空格如何,都可以工作。我什至尝试过使用'\'(空格的转义字符),但这也不起作用。事实上,这是我使用的代码:

String graphName = "DemoGraph";
//Isolate the FilePath
String path = script.getPath();
path = path.replace(script.getName(),"");
path = path.replace(File.separator, "\\\\"); //Gets around any parsing errors in filepaths on Windows
path = path.replace(" ", "\\ ");   //Should get around parsing errors with spaces in gnuplot, but it seems to be irrelevant.

scriptFileWriter.write("set output \"" + path + graphName + ".png\"\r\n");

这一定是 java 的问题,因为脚本从 Windows 命令行和 gnuplot 命令行运行,并且 frun 通过双击运行

4

1 回答 1

1

我忘了在文件名周围加上引号。这是一个愚蠢的错误。

于 2012-06-01T21:14:35.980 回答