我已经创建了一个 Process Builder。我如何 运行 Process builder 中的所有程序?
I've already created a Process Builder. How do I run all of the programs in the Process builder?
这是我目前的代码。我如何获得 miktex-pdftex 运行?
List<String> processes = new ArrayList<String>();
processes.add("miktex-pdftex --output-directory=[Directory] [file_name].tex");
ProcessBuilder processbuild = new ProcessBuilder(processes);
首先,您需要确保您使用的命令在命令中确实有效。如果没有,那么它不会在 Java.
中工作
接下来,使用 ProcessBuilder
的主要原因之一是处理 command/parameters 中的空格比 Runtime#exec
更好。
String command = "/Applications/MiKTeX Console.app/Contents/bin/miktex-pdftex";
String outputDir = System.getProperty("user.dir");
String sourceFile = "Sample.tex";
List<String> commands = new ArrayList<>();
commands.add(command);
commands.add("--interaction=nonstopmode");
commands.add("--output-directory=" + outputDir);
commands.add(sourceFile);
所以上面很简单...
- 我想要 运行 的命令是
/Applications/MiKTeX Console.app/Contents/bin/miktex-pdftex
(我在 MacOS 上 运行ning,我无法在应用程序包外安装命令)
- 我希望
output-directory
与当前工作目录 (System.getProperty("user.dir")
) 相同,但您可以提供所需的一切
- 我 运行宁 "nonstopmode" (
--interaction=nonstopmode
) 因为否则我将需要提供输入,这只是更复杂
- 我的输入文件 (
Sample.tex
) 也在工作目录中。
接下来,我们构建ProcessBuilder
并将错误流重定向到InputStream
,这只是减少了接下来分别读取这两个流...
ProcessBuilder pb = new ProcessBuilder(commands);
pb.redirectErrorStream(true);
接下来,我们 运行 命令,读取 InputStream
的内容(否则你可以停止进程),你可以用这个做任何你想做的事,我只是附和它到屏幕
try {
Process process = pb.start();
InputStream is = process.getInputStream();
int in = -1;
while ((in = is.read()) != -1) {
System.out.print((char)in);
}
int exitValue = process.waitFor();
System.out.println("");
System.out.println("Did exit with " + exitValue);
} catch (IOException | InterruptedException ex) {
ex.printStackTrace();
}
这里使用int exitValue = process.waitFor();
只是为了确保命令已经完成并获得它生成的退出值。通常,0
是成功的,但是您需要阅读命令的文档才能确定
这是我目前的代码。我如何获得 miktex-pdftex 运行?
List<String> processes = new ArrayList<String>();
processes.add("miktex-pdftex --output-directory=[Directory] [file_name].tex");
ProcessBuilder processbuild = new ProcessBuilder(processes);
首先,您需要确保您使用的命令在命令中确实有效。如果没有,那么它不会在 Java.
中工作接下来,使用 ProcessBuilder
的主要原因之一是处理 command/parameters 中的空格比 Runtime#exec
更好。
String command = "/Applications/MiKTeX Console.app/Contents/bin/miktex-pdftex";
String outputDir = System.getProperty("user.dir");
String sourceFile = "Sample.tex";
List<String> commands = new ArrayList<>();
commands.add(command);
commands.add("--interaction=nonstopmode");
commands.add("--output-directory=" + outputDir);
commands.add(sourceFile);
所以上面很简单...
- 我想要 运行 的命令是
/Applications/MiKTeX Console.app/Contents/bin/miktex-pdftex
(我在 MacOS 上 运行ning,我无法在应用程序包外安装命令) - 我希望
output-directory
与当前工作目录 (System.getProperty("user.dir")
) 相同,但您可以提供所需的一切 - 我 运行宁 "nonstopmode" (
--interaction=nonstopmode
) 因为否则我将需要提供输入,这只是更复杂 - 我的输入文件 (
Sample.tex
) 也在工作目录中。
接下来,我们构建ProcessBuilder
并将错误流重定向到InputStream
,这只是减少了接下来分别读取这两个流...
ProcessBuilder pb = new ProcessBuilder(commands);
pb.redirectErrorStream(true);
接下来,我们 运行 命令,读取 InputStream
的内容(否则你可以停止进程),你可以用这个做任何你想做的事,我只是附和它到屏幕
try {
Process process = pb.start();
InputStream is = process.getInputStream();
int in = -1;
while ((in = is.read()) != -1) {
System.out.print((char)in);
}
int exitValue = process.waitFor();
System.out.println("");
System.out.println("Did exit with " + exitValue);
} catch (IOException | InterruptedException ex) {
ex.printStackTrace();
}
这里使用int exitValue = process.waitFor();
只是为了确保命令已经完成并获得它生成的退出值。通常,0
是成功的,但是您需要阅读命令的文档才能确定