我需要从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"

最佳答案

您不应将bash脚本的参数放在环境中(即用于环境变量),而应该尝试以下操作:

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();

关于java - 从Java调用Shell脚本,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34204044/

10-12 12:52
查看更多