2011-03-29 1 views
5

안녕 얘들 아. 나는 다른 Java 클래스를 실행하는 새로운 Java 프로세스를 작성하는 Linux 환경에서 사용하도록 설계된 Java 프로그램을 작성하고 있지만 문제가 있습니다. 마침내 모든 문제를 해결했습니다. 내 자바 프로그램에 Linux에서 Java Runtime.exec 비애

Runtime.getRuntime().exec(new String[] { "/bin/bash", "-c", "'java -classpath /home/kevin/workspace/Misc/bin HelloWorld'" }) 

를 호출하면 중 하나를 표준 출력/표준 에러에

/bin/bash: /usr/lib/jvm/java-6-openjdk/jre/bin/java -classpath /home/kevin/workspace/Misc/bin HelloWorld: No such file or directory 

를 반환합니다. 내가

Runtime.getRuntime().exec(new String[] { "/bin/bash -c 'java -classpath /home/kevin/workspace/Misc/bin HelloWorld'" }) 

을하려고하면 나는 간단한

Runtime.getRuntime().exec("/bin/bash -c 'java -classpath /home/kevin/workspace/Misc/bin HelloWorld'") 

나에게

-classpath: -c: line 0: unexpected EOF while looking for matching `'' 
-classpath: -c: line 1: syntax error: unexpected end of file 

표준 출력에서 ​​/ 표준 에러를 제공하여,

Cannot run program "/bin/bash -c 'java -classpath /home/kevin/workspace/Misc/bin HelloWorld'": java.io.IOException: error=2, No such file or directory 
    ... 
Caused by: java.io.IOException: java.io.IOException: error=2, No such file or directory 

그리고 마지막으로 Java 예외를 얻을.

한편, (파일의 맨!/빈/떠들썩한 파티 NO #) 새로운 라인에 .sh 파일 생성 (적절한 권한 부여) 만이 가진 것은

/bin/bash -c 'java -classpath /home/kevin/workspace/Misc/bin HelloWorld' 

함께 정확한 출력을 제공한다 오류 없음.

나는 Runtime.exec의 사용법이 완벽하기 때문에 매우 복잡하다는 것을 알고 있으며 이전에 큰 문제를 이미 해결했지만이 문제는 Runtime.exec의 StringTokenizer 사용으로 인해 명령에 공백이있어서 문자열 배열을 허용하는 오버로드를 호출 한 이유입니다. 그러나, 나는 아직도 그것에 문제가 있고 나는 이유를 이해하지 못한다.

답변

11

첫 번째 시도가 거의 정확했습니다.

Runtime.getRuntime().exec(new String[] { "/bin/bash", "-c", "java -classpath /home/kevin/workspace/Misc/bin HelloWorld" }) 

는 개별 String 인수를 전달하는 것이 효과적 자동으로 인용하고 있기 때문에 인용 추가가 필요하지 않습니다.

+2

선생님, 당신은 최고입니다. 나는 대답이 그렇게 간단하다고 믿을 수 없다! 고마워, 그런 식으로 생각한 적도 없어. –