This is giving just the output of ls:
String[] cmd={"bash","-c","ls","-l"}:
ProcessBuilder pb=new ProcessBuilder(cmd);
Whereas this is giving long listing output properly:
String[] cmd={"bash","-c","ls -l"};
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In the first code snippet, the
-loption is being passed as an argument to bash, and not to ls. Bash interprets the-loption as specifying that it should behave as a ‘login’ shell.The argument after
-cshould include the whole bash script (spaces included) that you want to be executed, so the second code snippet is correct.