자바 어플리케이션에서 쉘 명령어를 실행하는 것에 관한 문제가 있습니다. 여기에 코드입니다 :자바에서 쉘 명령어를 실행하십시오.
public String execRuntime(String cmd) {
Process proc = null;
int inBuffer, errBuffer;
int result = 0;
StringBuffer outputReport = new StringBuffer();
StringBuffer errorBuffer = new StringBuffer();
try {
proc = Runtime.getRuntime().exec(cmd);
} catch (IOException e) {
return "";
}
try {
response.status = 1;
result = proc.waitFor();
} catch (InterruptedException e) {
return "";
}
if (proc != null && null != proc.getInputStream()) {
InputStream is = proc.getInputStream();
InputStream es = proc.getErrorStream();
OutputStream os = proc.getOutputStream();
try {
while ((inBuffer = is.read()) != -1) {
outputReport.append((char) inBuffer);
}
while ((errBuffer = es.read()) != -1) {
errorBuffer.append((char) errBuffer);
}
} catch (IOException e) {
return "";
}
try {
is.close();
is = null;
es.close();
es = null;
os.close();
os = null;
} catch (IOException e) {
return "";
}
proc.destroy();
proc = null;
}
if (errorBuffer.length() > 0) {
logger
.error("could not finish execution because of error(s).");
logger.error("*** Error : " + errorBuffer.toString());
return "";
}
return outputReport.toString();
}
하지만 난 명령처럼 간부 인하려고하면
/export/home/test/myapp -T "some argument"
myapp와 내가 "some argument"
으로 만 인수를 읽을 수 arguments.but 구분 "some argument"
두 가지로 읽습니다. 터미널에서이 명령을 직접 실행하면 성공적으로 실행됩니다.'"some argument"'
, ""some argument""
, "some\ argument"
을 시도했지만 저에게 적합하지 않았습니다. 어떻게이 논쟁을 하나의 논쟁으로 읽을 수 있을까요?
thx @midhat. 이것은 나를 위해 작동합니다. – Aykut
[Runtime.exec()'가 다음과 같은 경우 :'Runtime.exec()'메소드와 관련된 함정 주위를 둘러보십시오.] (http://www.javaworld.com/article/2071275/core-java/when -runtime-exec --- won-t.html) 또는 미래를 보장하는 [검색 엔진 쿼리] (https://duckduckgo.com/?q=When+Runtime.exec+wont+Navigate+yourself+around+ 함정 + 관련 + + + Runtime.exec + 메소드). –