本文介绍了我已经创建了一个流程构建器.如何运行 Process builder 中的所有程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我到目前为止的代码.我如何运行 miktex-pdftex?

Heere is the code I have so far. How do I have miktex-pdftex run?

List<String> processes = new ArrayList<String>();
    processes.add("miktex-pdftex --output-directory=[Directory] [file_name].tex");
    ProcessBuilder processbuild = new ProcessBuilder(processes);

推荐答案

首先,您需要确保您正在使用的命令实际适用于该命令.如果没有,那么它将无法在 Java 中工作.

First, you need to make sure the command you are using actually works at the command. If it does not, then it's not going to work in Java.

接下来,使用 ProcessBuilder 的主要原因之一是比 Runtime#exec 更好地处理命令/参数中的空格.

Next, one of the main reasons for using ProcessBuilder is to deals with spaces in the command/parameters better then 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 上运行,我无法在外部安装命令应用程序包)
  • 我希望 output-directory 与当前工作目录相同 (System.getProperty("user.dir")),但您可以提供什么您需要的一切
  • 我在nonstopmode"(--interaction=nonstopmode)下运行,否则我将需要提供输入,这只是更复杂
  • 还有我的输入文件 (Sample.tex),它也在工作目录中.
  • The command I want to run is /Applications/MiKTeX Console.app/Contents/bin/miktex-pdftex (I'm running on MacOS and I couldn't get the command installed outside the application bundle)
  • I want the output-directory to be the same as the current working directory (System.getProperty("user.dir")), but you could supply what every you need
  • I'm running in "nonstopmode" (--interaction=nonstopmode) because otherwise I would be required to provide input, which is just more complex
  • And my input file (Sample.tex) which is also in the working directory.

接下来,我们构建ProcessBuilder并将错误流重定向到InputStream中,这只是减少了下一次分别读取这两个流...

Next, we build the ProcessBuilder and redirect the error stream into the InputStream, this just reduces the next to read these two streams separately...

ProcessBuilder pb = new ProcessBuilder(commands);
pb.redirectErrorStream(true);

接下来,我们运行命令,读取InputStream的内容(否则你可以停止进程),你可以用它做任何你想做的事情,我刚刚把它回显到屏幕

Next, we run the command, read the contents of the InputStream (otherwise you can stall the process), you can do what ever you want with this, I've just echoed it to the screen

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 是成功的,但您需要阅读命令的文档以确保

The use int exitValue = process.waitFor(); here is just to ensure that command has completed and get the exit value it generated. Normally, 0 is success, but you'd need to read the documentation of the command to be sure

这篇关于我已经创建了一个流程构建器.如何运行 Process builder 中的所有程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 04:02