java - 从 Java 运行 gnuplot 进程时遇到问题

标签 java process gnuplot filepath processbuilder

我在从 Java 运行 gnuplot 进程时遇到一些问题。我正在创建一个 gnuplot 脚本文件,然后从 java 程序中运行它。我尝试过使用 Process Builder 并使用 Runtime.getRuntime().exec("blah blah...") 构建进程,但两者都没有完全的工作能力。有趣的是,只要我通过 gnuplot 创建的图像文件没有保存到名称中没有空格的目录中,使用运行时使该过程几乎完美运行。然而,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 是通过双击运行的

最佳答案

我忘记在文件名两边加上引号。这是一个愚蠢的错误。

关于java - 从 Java 运行 gnuplot 进程时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10855088/

相关文章:

linux - 谁启动操作系统进程调度程序?

project-management - 您的 Web 开发过程由哪些步骤组成,每个阶段需要多长时间?

gnuplot:使用字符组合的热图

java - 从位于java目录中的文件名获取文件路径

java - 删除给定节点上的 XML 内容并重新排列输出文件格式

java - 将Java程序运行到另一个程序中

colors - Gnuplot 颜色渐变

gnuplot - gnuplot中两个(xy)变量函数的平滑梯度显示?

java - 通过反射从View中获取OnTouchListener对象

java - 声明包含整数数组的对象的构造函数