1

I want to add paramters to the ProcessBuilder. I have a shell script like this "test.sh"

echo "hello"
read user
read pw

I want to call this script "test.sh" with the Process Builder to give the user and the pw as arguments. That the script is automatically doing this. Otherwise I always have to type in the user and the password.. The username and the pw is always the same, that's the reason why I want to do this automatically.

The full path to the script is here {/c/users/lia/desktop/test.sh"..}

So when I execute just the script this in git bash and it is directly here at the desktop with "sh test.sh" I get the content and it works fine..

In my String I have it like this String[] cmd = {"sh test.sh", "user", "pw"};

When I convert it into an jar file (I need it later as a jar file) it does not work. Got the message "cannot find file"

When I do it like this : String [] cmd = {/c/users/lia/desktop/test.sh"..}; it does not work

Thank you all.

Lia
  • 241
  • 4
  • 13
  • How will you execute .sh file which is shell script in windows ? – Sambit May 17 '19 at 11:57
  • I execute the sh test.sh in git bash its working fine :D – Lia May 17 '19 at 12:03
  • 1
    Possible duplicate of [pass multiple parameters to ProcessBuilder with a space](https://stackoverflow.com/questions/17062219/pass-multiple-parameters-to-processbuilder-with-a-space) – rghome May 17 '19 at 12:03
  • @rghome, might be.. but I still dont get it :( – Lia May 17 '19 at 12:04
  • The shell script reads from the input. You need to change it to use parameters (e.g., $1, $2, etc). – rghome May 17 '19 at 12:06
  • "When I convert it into an jar file (I need it later as a jar file) it does not work. Got the message "cannot find file"" You can't run scripts from inside a jar file. You'll need code to extract the script to a temporary directory and run it from there. Also, your users' machines will need to have `sh` available. – Alexey Romanov May 17 '19 at 12:07
  • thank you both for your answer. ```echo "hello" echo "user" $1" read "pw: $2" ``` and how do I change the java String cmd ? – Lia May 17 '19 at 12:09

1 Answers1

-1

You can write the shell script like the example given below.

echo "hello"
echo "user $1"
echo "pw: $2"

Now you have to execute as sh test.sh uname1 pwd1. Use this command in Java ProcessBuilder to execute.

Sambit
  • 7,625
  • 7
  • 34
  • 65
  • Hi thank you . I want to add the username and pw as arguments because it is always the same .. dont want to type it always :D – Lia May 17 '19 at 12:03
  • 1
    @Sambit except your answer does not answer the question, has syntax errors and generally doesn't seem to make any sense. – rghome May 17 '19 at 13:01
  • @rghome maybe it's ```sh /c/users/Lia/desktop/test.sh ``` maybe it's like this and my code at the top? – Lia May 17 '19 at 19:13