java 执行命令的代码

java code to execute commands

我想从 java 程序中执行表格工具命令。我正在尝试的代码是:

System.setProperty("user.dir", "C:\Program Files");
String command ="\tabula\tabula-0.9.0-SNAPSHOT-jar-with-dependencies.jar "+"D:\sample.pdf"+" -o "+"D:\sampleeeee.csv";
Process p = Runtime.getRuntime().exec(command);

它不工作,任何帮助将不胜感激。 this command need to be executed from java

试试这个来设置命令 运行.

的工作目录

Process p = null;
ProcessBuilder pb = new ProcessBuilder("java","-jar","tabula-0.9.0-SNAPSHOT-jar-with-dependencies.jar", "D:\sample.pdf", "-o", "D:\sampleeeee.csv");
pb.directory("C:\Program Files\tabula");
p = pb.start();

调用时可以指定工作目录exec:

Path workingDir = Paths.get("C:\Program Files\tabula");

String[] command = {
    "tabula-0.9.0-SNAPSHOT-jar-with-dependencies.jar",
    "sample.pdf",
    "-o samk.csv"
};

Process p = Runtime.getRuntime().exec(command, null, workingDir.toFile());