-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);
}}
5
  • Look here. I think this would be usefull: Pass variable to shell Commented Jun 22, 2016 at 13:37
  • If you show us the code you have, we can guide you in the right direction. Commented Jun 22, 2016 at 13:37
  • I do not want to run external script but would like to directly call multiple commands from with in java code Commented Jun 22, 2016 at 14:10
  • "touch " + var1 + ";" Commented Jun 22, 2016 at 14:29
  • Modified the lines as given above, it works now. Thanks. Commented Jun 22, 2016 at 14:30

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.