简体   繁体   English

将参数添加到ProcessBuilder-Java

[英]Add parameters to ProcessBuilder - Java

I want to add paramters to the ProcessBuilder. 我想向ProcessBuilder添加参数。 I have a shell script like this "test.sh" 我有一个像这样的shell脚本“ 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. 我想用Process Builder将此脚本称为"test.sh" ,以将用户和pw作为参数。 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. 否则,我总是必须输入用户名和密码。用户名和pw始终相同,这就是我要自动执行此操作的原因。

The full path to the script is here {/c/users/lia/desktop/test.sh"..} 脚本的完整路径在此处{/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.. 因此,当我只在git bash中执行脚本时,它直接在桌面上带有“ sh test.sh”,就可以得到内容,并且可以正常工作。

In my String I have it like this String[] cmd = {"sh test.sh", "user", "pw"}; 在我的String中,它像这样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. 当我将其转换为jar文件(以后需要将其作为jar文件)时,它将无法工作。 Got the message "cannot find file" 得到了消息“找不到文件”

When I do it like this : String [] cmd = {/c/users/lia/desktop/test.sh"..}; it does not work 当我这样做时: String [] cmd = {/c/users/lia/desktop/test.sh"..};它不起作用

Thank you all. 谢谢你们。

You can write the shell script like the example given below. 您可以像下面给出的示例一样编写shell脚本。

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

Now you have to execute as sh test.sh uname1 pwd1. 现在,您必须以sh test.sh uname1 pwd1的身份执行。 Use this command in Java ProcessBuilder to execute. 在Java ProcessBuilder中使用此命令来执行。

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

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