如何使用 java 将 getopts 参数传递给 shell 脚本

How to pass getopts argurements to a shell script using java

我想 运行 使用 java 使用 Runtime.getRuntime().exec() 的 shell 脚本。但是,我的 shell 脚本正在使用 getopts 所以我需要在终端中使用这个命令来 运行 脚本:

./script.sh -l 01 -n 02 

那么我如何使用 java 执行带有多个参数的脚本?我尝试了下面的代码,但我没有用。

String[] args = {"script.sh", "-l 01", "-n 02"};
Runtime.getRuntime().exec(args);

每个参数都是一个单独的 String。喜欢,

String[] args = { "script.sh", "-l", "01", "-n", "02" };