简体   繁体   中英

Add parameters to ProcessBuilder - Java

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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