krish
krish

Reputation: 38

java code to execute commands

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

Upvotes: 1

Views: 256

Answers (2)

Teddy
Teddy

Reputation: 4243

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

Upvotes: 0

Jorn Vernee
Jorn Vernee

Reputation: 33895

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

Upvotes: 1

Related Questions