从Java调用shell脚本

时间:2015-12-10 14:10:47

标签: java shell

我需要从java程序中调用shell脚本。我尝试使用下面的代码,但它不起作用 -

        ProcessBuilder pb = new ProcessBuilder("/bin/bash","/usr/local/bin/ticketer/ticketer_robot");
        pb.environment().put("$1", "test");
        pb.environment().put("$2", "testoce");
        pb.environment().put("$3", "2 - Medium");
        pb.environment().put("$4", "2 - Medium");
        pb.environment().put("$5", "Error while reading file");
        pb.environment().put("$6", "Error While reading file in Job . Please check log NotfnLOG for more details");
        pb.environment().put("$7", "testtestets");
        pb.environment().put("$8","testtesttest");
        pb.environment().put("$9", "/data/mars/logs/tesst.log");
        pb.environment().put("$10", "test@test.com");
        final Process process = pb.start();

下面是我们用来从unix shell调用脚本的命令 -

sh /usr/local/bin/ticketer/ticketer_robot "test" "testoce" "2 - Medium" "2 - Medium" "Error while reading file" "Error While reading file in Job. Please check log for details"
 "testtestets" "testtesttest" "/data/mars/logs/tesst.log" "test@test.com"

1 个答案:

答案 0 :(得分:3)

你不应该把bash脚本的参数放在环境中(那就是environemntal变量),而是试试这个:

String[] command = {"/bin/bash",
                    "/usr/local/bin/ticketer/ticketer_robot", 
                    "test",
                    "testoce",
                    "2 - Medium",
                    "2 - Medium",
                    "Error while reading file",
                    "Error While reading file in Job . Please check log NotfnLOG for more details",
                    "testtestets",
                    "testtesttest",
                    "/data/mars/logs/tesst.log",
                    "test@test.com"
};

ProcessBuilder pb = new ProcessBuilder(command);
pb.start();