Hi,
I am trying to how to execute a system command in java.
Here is the code:
String cmd = "/usr/bin/pkill -9 -f " + "\'"+ getKillCmdLine() +"\'";
Runtime.getRuntime().exec(cmd);
My getKillCmdLine() returns
/home/yinglcs/bin/Main 9091 -DService10000000
(node there is space in the string.
But my code always fail with this:
- /usr/bin/pkill: invalid option -- 'D'
Usage: pkill [-SIGNAL] [-fvx] [-n|-o] [-P PPIDLIST] [-g PGRPLIST] [-s SIDLIST]
[-u EUIDLIST] [-U UIDLIST] [-G GIDLIST] [-t TERMLIST] [PATTERN]
I think the string does not get escaped "\'" and Runtime.getRuntime().exec() thinks '-D as an argument to the pkill command.
Can you please suggest any idea how to get this to work?
I try wrapping my string with ' and " and \"
String cmd = "/usr/bin/pkill -9 -f " + "'"+ getKillCmdLine() +"'";
String cmd = "/usr/bin/pkill -9 -f " + "\""+ getKillCmdLine() +"\"";
All does not work.
Thank you for any pointers.