使用ProcessBuilder运行Shell脚本 [英] Running a shell script using ProcessBuilder

查看:840
本文介绍了使用ProcessBuilder运行Shell脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Java和ProcessBuilder运行脚本。当我尝试运行时,收到以下消息:error = 2,没有这样的文件或目录。

I am trying to run a script using Java and ProcessBuilder. When I try to run, I receive the following message: error=2, No such file or directory.

我不知道我做错了什么,但这是我的代码(ps:我尝试仅执行不带参数的脚本,并且错误相同:

I dont know what I am doing wrong but here is my code (ps: I tried to execute just the script without arguments and the error is the same:

String[] command = {"/teste/teste_back/script.sh, "+argument1+", "+argument+""};
ProcessBuilder p = new ProcessBuilder(command);

    try {  

        // create a process builder to send a command and a argument
        Process p2 = p.start(); 
        BufferedReader br = new BufferedReader(new InputStreamReader(p2.getInputStream()));
        String line;

        log.info("Output of running " + command + " is: ");
        System.out.println("Output of running " + command + " is: ");
        while ((line = br.readLine()) != null) {
            log.info(line);
        }

    }  


推荐答案

尝试替换

String[] command = {"/teste/teste_back/script.sh, "+argument1+", "+argument+""};

String[] command = {"/teste/teste_back/script.sh", argument1, argument};

引用 ProcessBuilder 了解更多信息。


ProcessBuilder (字符串...命令)

ProcessBuilder(String... command)

使用指定的操作系统
程序和参数构造流程生成器。

Constructs a process builder with the specified operating system program and arguments.

这篇关于使用ProcessBuilder运行Shell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆