-3

I am using process builder to execute multiple shell commands (not external shell script) from Java. I can pass system environment variables to the shell command; However, if I want to pass on a variable defined in java (for example a string) as a argument to the shell command, how can I do that? My code looks something like this. I want two files to be created (touched) by name 123 & 234.

public class ExecShellCmds {

public static void beginWrite() {
    String var1 = "123";
    String var2 = "234";

    String s = null;
    try {
        String[] cmd = {"/bin/bash", "-c",
            "touch var1;"
            + "touch var2;"
        };

        Process p = Runtime.getRuntime().exec(cmd);
}}
Karthik
  • 13
  • 5

1 Answers1

0

The arguments (and the program) are stored in a List<String>, you can get a reference to this list with ProcessBuilder.command(), you can then just add your parameters to this list.

tkausl
  • 13,686
  • 2
  • 33
  • 50