Java can';不做系统调用

Java can';不做系统调用,java,java-8,Java,Java 8,我正在使用Java将一首诗格式化为LaTeX格式,然后进行编译。这个命令运行得非常好,但不知怎么的,我无法运行这个命令。这是我的LaTeX调用的问题吗?出于某种原因,当我使用Java编写一个等效的批处理文件,然后运行它时,Java将什么也不做,但当我从shell运行批处理文件时,它就工作了 /** * * @param title the title of the poem * @param poem a <code>List</code&

我正在使用Java将一首诗格式化为LaTeX格式,然后进行编译。这个命令运行得非常好,但不知怎么的,我无法运行这个命令。这是我的LaTeX调用的问题吗?出于某种原因,当我使用Java编写一个等效的批处理文件,然后运行它时,Java将什么也不做,但当我从shell运行批处理文件时,它就工作了

    /**
     *
     * @param title the title of the poem
     * @param poem a <code>List</code> with one string for each line of the
     * poem.
     * @throws FileNotFoundException
     */
    protected static void writePDF(final String title, List<String> poem) throws FileNotFoundException {
    final StringBuilder latex = new StringBuilder(0);
    // I know I shouldn't concatenate like this; I'll fix it later.
    // eeeewww escapes
    latex.append(
        "\\documentclass[letterpaper,12pt,article,oneside]{memoir}\n"
        + "\\usepackage[utf8]{inputenc}\n"
        + "\\usepackage{ebgaramond}\n"
        + "\\usepackage{hyperref}\n"
        + "\\usepackage[protrusion,expansion,kerning,tracking,spacing]{microtype}\n"
        + "\\linespread{1.3}\n"
        + "\\nonfrenchspacing\n"
        + "\\microtypecontext{spacing=nonfrench}\n"
        + "\\begin{document}\n"
        + "\\title{" + title + "}\n"
        + "\\author{}\n"
        + "\\date{\\today}\n"
        + "\\maketitle\n"
        + "\\setlength{\\parindent}{0pt}\n"
        + "\\setlength{\\parskip}{\\baselineskip}\n");
    // Go Java 8!
    poem
        .stream()
        .map((String s)
            // Original poem's in HTML
            -> s.replace("<p>", "\n\n").replace("<br>", "\\\\\n"))
        .forEach(latex::append);
    latex.append("\n\\end{document}");
    final String latexstr = latex.toString().replace("...", "\\ldots");
    final String filename = title + ".tex";
    final File file = new File(filename);
    try (final PrintWriter pw = new PrintWriter(new BufferedOutputStream(new FileOutputStream(file)))) {
        pw.print(latexstr);
    }
    final String path = file.getAbsolutePath()
        .substring(0, file.getAbsolutePath().lastIndexOf("\\")) + "\\";
    System.out.println("Path: " + path);

    final String LaTeXcmd = "pdflatex \""
        + path
        + title + "\"";
    final File script = new File(""
        + rand.nextDouble()
        + "compile"
        + title.replace(" ", "_")
        + ".bat");
//I originally wanted just to write a batch file and run it from Java.
//  try (final PrintWriter pw = new PrintWriter(new BufferedOutputStream(new FileOutputStream(script)))) {
//      pw.print(""
//          //"@echo off\n"
//          + "cd " + path + "\n"
//          + LaTeXcmd + "\n"
//          //          + "del \"" + path + title + ".aux\"\n"
//          //          + "del \"" + path + title + ".log\"\n"
//          //          + "del \"" + path + title + ".out\"\n"
//          //          + "del \"" + path + title + ".tex\"\n"
//          //          + "start /b \"\" cmd /c del \"%~f0\"&exit /b\n"
//
//          + "msg * all\n"
//      );
//  }
    try {
        System.out.println("latexcmd " + LaTeXcmd);
        final File workingdir = new File(path);
        System.out.println("workingdir " + workingdir);
// >>>>>>>>>>>>>>>> IS THIS CORRECT? <<<<<<<<<<<<<<<<
        Runtime.getRuntime().exec(LaTeXcmd, new String[]{}, workingdir);
// This statement works perfectly fine (Windows).
//      Runtime.getRuntime().exec("msg * all");
    } catch (IOException ex) {
        Logger.getLogger(PoetryBackend.class.getName()).log(Level.SEVERE, null, ex);
    }

    }
这是不对的。试一试

String command = new String[] {"cmd", "/c", LaTeXcmd };
Runtime.getRuntime().exec(command, new String[]{}, workingdir);
否则,您不会通过命令解释器执行它,如果您希望它的行为像从cmd提示符运行一样,这就是您想要的


这也适用于运行.bat文件。

请删除所有冗余代码。你问的是如何进行系统调用,那么你为什么要把这个问题和所有其他事情混为一谈呢?您的LaTex代码和大多数其他代码与您的问题完全无关。