使用java程序和参数运行shell脚本

使用java程序和参数运行shell脚本,java,runtime,Java,Runtime,我试图从java应用程序运行shell脚本,但无法传递脚本运行所需的上下文参数 下面是我在cmd上直接调用文件时使用的命令 MytestFile.sh --context_param db_host="localhost" --context_param db_name="test_db" --context_param data_api="https://test-api-dev-view.cloud.my.com/meta-info"

我试图从java应用程序运行shell脚本,但无法传递脚本运行所需的上下文参数

下面是我在cmd上直接调用文件时使用的命令

MytestFile.sh --context_param db_host="localhost" --context_param db_name="test_db" --context_param data_api="https://test-api-dev-view.cloud.my.com/meta-info" --context_param api_body_path="C:\\MyTest\\4009\\New\\postRequestSmart.txt" --context_param service_customer="me" --context_param service_url="https://test-view.cloud.my.com/" --context_param file_path="C:\\Test\\4009\New\\fileloc" --context_param processedDirectory="C:\\Test\\4009\\New\\PD"
现在,我尝试使用java运行时调用相同的函数,并将上下文参数作为strArray传递

更新:

public class App2 {

    public static void main(String[] args) throws IOException, InterruptedException {
        String[] strArray = new String[]{ "--context_param db_host=localhost"
                , "--context_param db_name=test_db"
                , "--context_param data_api=https://test-api-dev-view.cloud.my.com/meta-info"
                , "--context_param api_body_path=C:\\MyTest\\4009\\New\\postRequestSmart.txt"
                , "--context_param service_customer=me"
                , "--context_param service_url=https://https://test-view.cloud.my.com/"
                , "--context_param file_path=C:\\Test\\4009\New\\fileloc"
                , "--context_param processedDirectory=C:\\Test\\4009\\New\\PD"}

              try { 
                 Runtime.getRuntime().exec("cmd /c start MytestFile.sh", strArray); 
              } catch (IOException e) { 
                 e.printStackTrace(); 
              }

    }

}
方法2:使用Process builder

package com.test.shell;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map;


public class App2 {

    public static void main(String[] args) throws IOException, InterruptedException {
        

        String[] strArray = new String[]{ "--context_param db_host=localhost"
                , "--context_param db_name=test_db"
                , "--context_param data_api=https://test-api-dev-view.cloud.my.com/meta-info"
                , "--context_param api_body_path=C:\\MyTest\\4009\\New\\postRequestSmart.txt"
                , "--context_param service_customer=me"
                , "--context_param service_url=https://https://test-view.cloud.my.com/"
                , "--context_param file_path=C:\\Test\\4009\New\\fileloc"
                , "--context_param processedDirectory=C:\\Test\\4009\\New\\PD"}
        

        ProcessBuilder pb = new ProcessBuilder(
                "MytestFile.bat");
        Process p = pb.start();

        BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String output;
        while ((output = stdInput.readLine()) != null) {
            System.out.println(output);
        }
    }

}
这正在调用我的批处理/sh文件,但是有人可以帮助我如何将参数传递到此ProcessBuilder中吗

我觉得我没有正确地传递参数

请让我知道我需要在哪里更正此问题


TIA

您应该在一个数组中传递命令后的参数,并在空格处将参数拆分为单个参数:

    String[] cmd = new String[]{ 
        "MytestFile.bat"
            , "--context_param"
            , "db_host=localhost"
            ...
            , "--context_param"
            , "file_path=C:\\Test\\4009\New\\fileloc"
            , "--context_param"
            ,"processedDirectory=C:\\Test\\4009\\New\\PD"}
    

    ProcessBuilder pb = new ProcessBuilder(cmd);

在第一个示例中,它作为
Runtime.exec(String,String[])
方法不起作用,第二个参数是environment而不是脚本的参数。

您没有在try块中关闭括号,也没有将strArray作为方法参数传递,而是发送原始字符串;如果您想使用strArray对“cmd/c start MytestFile.sh”进行参数化,请尝试以下操作:
String.format(“cmd%s start MytestFile.sh”,strArray)
还要检查您的输入
“…--context\u param db\u name=test\u db--context\u param data\u api=…”
至少缺少一个空格(更不用说在
service_customer=me
和更多地方之后的结束语)@user15793316抱歉,这是一个输入错误,我已经更正了它。我将它修改为一个正确的字符串数组,但它仍然无法识别。我在帖子中更新了代码。