[英]ProcessBuilder throwing java.lang.Exception:
我試圖弄清楚為什么下面的代碼會拋出一個
java.lang.Exception:沒有這樣的文件或目錄
例外
ProcessBuilder send = new ProcessBuilder("/bin/bash","/opt/ftp/scripts/XFER.sh | /opt/ftp/myftp -c /opt/ftp/ftp.conf >> /logging/ftp.log2>&1");
Process sendProcess = send.start();
br = new BufferedReader(new InputStreamReader(sendProcess.getErrorStream()));
builder = new StringBuilder();
line = null;
while ( (line = br.readLine()) != null) {
builder.append(line);
builder.append(System.getProperty("line.separator"));
}
if(!builder.toString().isEmpty()){
throw new Exception( "ERROR with XFER.sh: "+builder.toString() );
}
我試過隔離字符串數組中的參數,但這也不起作用。 關於什么可能導致此堆棧跟蹤的任何想法?
我使用以下代碼成功。 也許您必須使用-c
選項:
private static int execute(String command) {
Runtime runtime = null;
Process process = null;
int exitValue = -1;
BufferedInputStream bis = null;
try {
runtime = Runtime.getRuntime();
process = runtime.exec(new String[] { "/bin/bash", "-c", command });
bis = new BufferedInputStream(process.getInputStream());
byte[] b = new byte[1892];
while (bis.read(b) != -1) {
}
exitValue = process.waitFor();
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
}
}
if (process != null) {
process.destroy();
}
} catch (Exception e) {
//Logging
}
return exitValue;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.