簡體   English   中英

java.io.IOException:無法運行程序“/usr/bin/sh”:java.io.IOException:error=2,沒有那個文件或目錄

[英]java.io.IOException: Cannot run program “/usr/bin/sh”: java.io.IOException: error=2, No such file or directory

我想執行重命名和刪除功能,環境是LINUX。 這是我正在使用的代碼,

String[] command_ary = { "/usr/bin/sh", "-c", command };
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec( command_ary );

但我收到以下錯誤消息,

java.io.IOException: Cannot run program "/usr/bin/sh": java.io.IOException: error=2, No such file or directory

有誰能夠幫我。 謝謝

正如例外所說,您的系統上沒有文件/usr/bin/sh 如果您正在尋找 Bourne shell,那就是/bin/sh

public void run() { //log.info("taskDao:{},concurrentHashSet:{},id:{},shellName:{},shellDate:{}", taskDao, concurrentHashSet, id, shellName, shellDate); log.info("concurrentHashSet:{},param:{}", concurrentHashSet, param); int exeState = 99999; // ssh跳轉到其他機器去執行腳本,you can add "ssh rd@g1-jg-hadoop-01 \"source ~/.bash_profile ; bash -x %s %s\"" instead of command String command = "source ~/.bash_profile ; bash -x %s %s"; String commandF = String.format(command, param.getShellName(), param.getRecallDate()); String[] cmdArr = {"/bin/sh", "-c", commandF}; long taskId = param.getTaskId(); Runtime runtime = Runtime.getRuntime(); Process process = null; try { process = runtime.exec(cmdArr); InputStream stderr = process.getErrorStream(); InputStreamReader isr = new InputStreamReader(stderr); BufferedReader br = new BufferedReader(isr); String line = null; StringBuilder sb = new StringBuilder(); while ((line = br.readLine()) != null) { sb.append(line).append(System.lineSeparator()); } log.info("exe task thread param:{},commandF:{},execute shell info:{}", param, commandF, sb.toString()); exeState = process.waitFor(); } catch (InterruptedException | IOException e) { log.error("execute shell error,exeState:{},command:{}", exeState, commandF,e); } finally { log.info("execute shell state:{}", exeState); // 從set中刪除 更新表狀態 多張表一個腳本,會有問題,其中狀態覆蓋 if (exeState == 0) { // 執行成功 taskDao.updateStateByPrimaryKey(taskId, (short) 30, new Date()); // 郵件參數 param.setState(30); String mailBody = SendMailUtil.beautyMailHtmlLayout(param); String mailToAddress = param.getMailToUser(); String mailTitle = param.getMailContentTitle(); try { SendMailUtil.sendMail(mailToAddress, mailTitle, mailBody); } catch (Exception e) { e.printStackTrace(); } } if (exeState != 0) { // 執行失敗 taskDao.updateStateByPrimaryKey(taskId, (short) 40, new Date()); // 郵件參數 param.setState(40); String mailBody = SendMailUtil.beautyMailHtmlLayout(param); String mailToAddress = param.getMailToUser(); try { SendMailUtil.sendMail(mailToAddress, "回溯任務", mailBody); } catch (Exception e) { e.printStackTrace(); } } ExecuteTaskModel taskModel = new ExecuteTaskModel(); taskModel.setId(taskId); log.info("remove task in thread,id:{}", taskId); boolean remove; if (concurrentHashSet.contains(taskModel)) { remove = concurrentHashSet.remove(taskModel); } log.info("remove from set success set:{}", concurrentHashSet); log.info("execute task thread exit!"); System.out.println("Process exitValue: " + exeState); assert process != null; process.destroy(); } }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM