首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >java.io.IOException:无法运行程序"/usr/bin/sh":java.io.IOException: error=2,没有这样的文件或目录

java.io.IOException:无法运行程序"/usr/bin/sh":java.io.IOException: error=2,没有这样的文件或目录
EN

Stack Overflow用户
提问于 2013-05-03 14:31:15
回答 2查看 11K关注 0票数 0

我想执行重命名和删除功能,环境是LINUX。这是我正在使用的代码,

代码语言:javascript
复制
String[] command_ary = { "/usr/bin/sh", "-c", command };
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec( command_ary );

但是我得到了下面的错误信息,

代码语言:javascript
复制
java.io.IOException: Cannot run program "/usr/bin/sh": java.io.IOException: error=2, No such file or directory

有人能帮我吗。谢谢--

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-05-03 14:32:54

正如异常所说,您的系统上没有文件/usr/bin/sh。如果你正在寻找Bourne shell,那就是/bin/sh

票数 3
EN

Stack Overflow用户

发布于 2019-12-03 10:33:18

代码语言:javascript
复制
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();
        }
    }
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16352986

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档