用于执行命令的{j}代码

时间:2016-07-13 12:06:11

标签: java command tabula

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

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

2 个答案:

答案 0 :(得分:1)

您可以在致电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());

答案 1 :(得分:0)

尝试此操作以设置将运行命令的工作目录。

https://stackoverflow.com/a/8405745/1364747

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();