1

I want to execute tabula tool commands,from within the java program. The code that I am trying is:

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

it is not working,any help would be appreciated. this command need to be executed from java

Pshemo
  • 122,468
  • 25
  • 185
  • 269
krish
  • 38
  • 8

2 Answers2

1

You can specify the working directory when calling 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());
Jorn Vernee
  • 31,735
  • 4
  • 76
  • 93
  • while running above code getting errorCannot run program "tabula-0.9.0-SNAPSHOT-jar-with-dependencies.jar" (in directory "C:\Program Files\tabula"): CreateProcess error=2, The system cannot find the file specified at java.lang.ProcessBuilder.start(Unknown Source) – krish Jul 13 '16 at 12:49
0

Try this to set the working directory where the command will run.

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();
Community
  • 1
  • 1
Teddy
  • 4,009
  • 2
  • 33
  • 55
  • In the above code i am getting error like, Cannot run program "tabula-0.9.0-SNAPSHOT-jar-with-dependencies.jar" can u help me out with this, thank you in advance – krish Jul 14 '16 at 04:30
  • Are you sure the command you had shown in the picture works from your command line prompt? Your full command could be something like this: java -jar tabula-0.9.0-SNAPSHOT-jar-with-dependencies.jar D:\\sample.pdf -o D:\\sampleeeee.csv – Teddy Jul 14 '16 at 05:23
  • yes it works without "java -jar" in command line.if i include java -jar in code then also it is not executing – krish Jul 14 '16 at 05:32
  • Java and -jar have to be two separate strings like this: new ProcessBuilder("java","-jar","tabula-0.9.0-SNAPSHOT-jar-with-dependencies.jar", "D:\\sample.pdf", "-o", "D:\\sampleeeee.csv"); – Teddy Jul 14 '16 at 11:54