简体   繁体   English

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

[英]Run a shell script using java program along with parameters

I am trying to run a shell script from my java application and not been able to pass the context params that are required for my script to run.我正在尝试从我的 java 应用程序运行 shell 脚本,但无法传递我的脚本运行所需的上下文参数。

Below is the command that works when I direct call the file on cmd以下是我直接调用 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"

Now I am trying to call the same using java Runtime and i am passing the context param as strArray现在我正在尝试使用 java 运行时调用相同的方法,并且我将上下文参数作为 strArray 传递

Updated:更新:

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

    }

}

Approach 2: Using Process builder方法 2:使用流程构建器

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

}

This is invoking my batch/sh file but can someone please help as to how to pass parameters into this ProcessBuilder?这是在调用我的批处理/sh 文件,但是有人可以帮忙解决如何将参数传递到这个 ProcessBuilder 中吗?

I feel I am not passing the parameters correctly.我觉得我没有正确传递参数。

Please let me know where I need to correct this.请让我知道我需要在哪里更正。

TIA TIA

You should be passing the arguments after the command in one array, and split the arguments to individual parameters at the spaces:您应该在一个数组中的命令之后传递 arguments,并将 arguments 拆分为空格处的各个参数:

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

It does not work in your first example as the Runtime.exec(String,String[]) method, the second argument is environment not arguments to the script.它在您的第一个示例中作为Runtime.exec(String,String[])方法不起作用,第二个参数是脚本的环境而不是arguments。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM