1

When I execute the following command in the terminal, it executes as expected i.e. it displays the current folder contents.

echo <password> | sudo -u root -S ls

But if i execute the same command in my java application, its output is:

<password> | sudo -u root -S ls

How do I overcome this?

3
  • 3
    java does not execute processes using a shell, and you are using shell metacharacters '|'. there are numerous answers already on SO and the web specifying how to do this. Commented Aug 9, 2013 at 12:41
  • 2
    Have you tried to execute sh -c "echo sanovi | sudo -u root -S ls" instead? Commented Aug 9, 2013 at 13:04
  • @FabianHenze I tried ur suggestion. But no success. Any other suggestions? Commented Aug 14, 2013 at 8:02

1 Answer 1

2

When you execute it in the shell, the shell splits the command line according to normal shell rules, i.e. you get "execute echo with parameter <password>, pipe the result to sudo ...".

Java doesn't use the shell, so you're getting "execute echo with parameter <password> | sudo -u root -S ls".

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.