1

I have a Java-App, which should execute an sh command.
My command looks like sudo /bin/sh -c "echo 7 > /sys/class/gpio/export" and when I execute this in the command prompt of my computer it works, but not with my Java-Programm.

The Programm-line looks like this:

System.out.println(CmdExecutor.execute("sudo /bin/sh -c \"echo 7 > /sys/class/gpio/export\""));


public class CmdExecutor {

public static String execute(String[] cmd) {
    StringBuffer output = new StringBuffer();

    Process p;
    try {
        p = Runtime.getRuntime().exec(cmd);
        p.waitFor();
        BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));

        String line = "";
        while ((line = reader.readLine()) != null) {
            output.append(line).append("\n");
        }

    } catch (IOException | InterruptedException e) {
    }

    return output.toString();
}

public static String execute(String cmd) {
    StringBuffer output = new StringBuffer();

    Process p;
    try {
        p = Runtime.getRuntime().exec(cmd);
        p.waitFor();
        BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));

        String line = "";
        while ((line = reader.readLine()) != null) {
            output.append(line).append("\n");
        }

    } catch (IOException | InterruptedException e) {
    }

    return output.toString();
}

}

Can someone help me?

7
  • try ....exec(cmd.toString()); Commented Jan 15, 2015 at 19:07
  • 3
    Pretty sure you need to provide a password when you sudo. Commented Jan 15, 2015 at 19:07
  • 1
    Add a reader to the ouputstrem and see if you get an error. Example you can find here Commented Jan 15, 2015 at 19:09
  • 1
    What happens when you run the code? Do you expect to supply a password to sudo, or is it supposed to work without a password? Does sudo or the shell emit any error messages? Do you get an exception? What is the exception and the stacktrace? Commented Jan 15, 2015 at 19:11
  • Maybe this will help? stackoverflow.com/questions/18708087/… Commented Jan 15, 2015 at 19:12

1 Answer 1

0

I see two issues:

  • Multiple arguments need to be split in Java already.
  • Authentication with sudo.

Multiple arguments need to be split.

If you run exec("a b"), the system will look for a command named a b as one single String command name.

If you run exec("a", "b"), the system will look for a command namedaand passb` as argument to that program.

So what you want to do is execute("sudo", "/bin/sh", "-c", "echo 7 > /sys/class/gpio/export").

sudo might require authentication

When you execute commands with sudo, an authentication will be performed. If you execute multiple sudo commands from the same process, the system will cache the authentication for convenience, but basically the authentication is required.

The authentication with sudo usually means that you need to supply a password.

The reason why you sudo this is that /sys/class/gpio/export has permissions -w------- (200) owned by root root, which means nobody can read it and only root can write it.

You have a few options:

  • Change the permissions of that file so that everybody can write it (not recommended): chmod a+w /sys/class/gpio/export.
  • Change the permissions of that file so that the user in question can write it: setfacl -m user:cher:w /sys/class/gpio/export - note that this only works if your sysfs is mounted with acl option, and usually it isn't. I don't know if it's even possible to mount sysfs with acl option, I haven't tried myself.
  • Pipe the password to the sudo command: exec("echo password | sudo /bin/sh -c \"echo 7 > /sys/class/gpio/export\"") WARNING THIS IS DANGEROUS!!!
  • Use a graphical sudo replacement like kdesudo
  • Change your sudoers configuration so that the user in question never needs to enter password for sudo - not recommended.
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for your reply. I didn't need any authentication or to change the owner of the directory. I only changed the String to a String-Array "sudo", "/bin/sh", "-c", "echo 7 > /sys/class/gpio/export"
I updated the answer. I kept the sudo part for reference, in case others have a similar issue.

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.