2016-07-13 6 views
1

자바 프로그램 내에서 tabula tool 명령을 실행하고 싶습니다. 내가 시도한 코드는 다음과 같습니다.명령을 실행하는 자바 코드

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

+0

있다. –

답변

0

명령을 실행할 작업 디렉토리를 설정하십시오. exec를 호출 할 때

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

위의 코드에서 "tabula-0.9.0-SNAPSHOT-jar-with-dependencies.jar"프로그램을 실행할 수 없습니다. 미리 알려 주시면 감사하겠습니다. – krish

+0

명령 하시겠습니까? 그림에 표시된 명령 줄 프롬프트에서 작동 했습니까? 전체 명령은 다음과 같을 수 있습니다. java -jar tabula-0.9.0-SNAPSHOT-jar-with-dependencies.jar D : \\ sample.pdf -o D : \\ sampleeeee.csv – Teddy

+0

예 "java -jar "코드에서 java -jar를 포함하면 실행되지 않습니다. – krish

1

당신은 작업 디렉토리를 지정할 수 있습니다 : 첫 번째 문이 (또는 자식 프로세스 ') 작업 디렉토리를 변경하지 않을

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

위 코드를 실행하는 동안 오류가 발생하지 않습니다. "tabula-0.9.0-SNAPSHOT-jar-with-dependencies.jar"디렉토리 ("C : \ Program Files \ tabula"디렉토리)에서 다음을 실행하십시오. CreateProcess error = 2, 지정된 파일 \t at java.lang.ProcessBuilder.start (알 수없는 출처) – krish