1

I'm trying to execute a linux command in my java code. It needs to change permissions for some directory.

Here is my attempt:

String  Cmd = "echo myPassword | sudo -S chmod 777 -R /home/somePath";
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(Cmd);   

The command held in String Cmd is working perfectly when I used it in terminal. But when I use it in my code nothing happens. There is no error or warning feedback that helps me to understand my mistake. What might be the problem?

1 Answer 1

5

Java will not magically select bash as your executable. You probably want to do something like

"bash -c <your command>"

See this question:

(Also the | is a bash-thing. Java won't magically create pipes between processes.)

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

2 Comments

Maybe you should mention that the | thing is part of the bash language and therefore it needs to be interpreted by bash (or similar shell).
Process proc = new ProcessBuilder("/bin/bash", "-c", "echo myPassword | sudo -S chmod 777 -R /home/somePath").start();

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.