How to run sudo command for OS X in Java

I'm using the app in JavaFX. I'm trying to open an application using the command in the terminal. I'm using my java code to run the command. My command has some variables. It has the path of my installer file, because the file name will not always be the same and can be different when updating the build

Process process = Runtime.getRuntime().exec("echo password | sudo -S open -a safari");
    String line;
    BufferedReader input = new BufferedReader(new InputStreamReader(pb.getInputStream()));
    while ((line = input.readLine()) != null) {
        System.out.println(line);
    }
    input.close();

The process gives no output. It stops there without any response I tried the same command from the terminal and it worked normally

I've tried the content mentioned in this link

How to execute bash command with sudo privileges in Java?

But it didn't work either

I also run commands like "CHMOD X" from my java code. These commands work normally My original order was as follows: –

runCommand = "echo" + " " + password + "| sudo -S " + "\"" + a.getAbsolutePath() + "\"" + " --deploymentFile="
                            + "\"" + b.getAbsolutePath() + "\"";

Where a. getabsolutepath () is the path of the installer file and B. getabsolutepath () is the path of the deployment file we use to install the application

pb.getInputStream()

Print command, when I copy and paste it is the terminal, it works normally

pb.getErrorStream()

Don't give anything

I tried running

String[] cmd = {"/bin/bash","-c","echo tester| sudo -S ","\"",a.getAbsolutePath()," --deploymentFile=",b.getAbsolutePath()};

and

String[] cmd = {"/bin/bash","echo tester| sudo -S","--deploymentFile=",b.getAbsolutePath()};

Also here I got the following mistakes

getErrorStreamusage: sudo -h | -K | -k | -L | -V
getErrorStreamusage: sudo -v [-AknS] [-g groupname|#gid] [-p prompt] [-u user name|#uid]
getErrorStreamusage: sudo -l[l] [-AknS] [-g groupname|#gid] [-p prompt] [-U user name] [-u
getErrorStream            user name|#uid] [-g groupname|#gid] [command]
getErrorStreamusage: sudo [-AbEHknPS] [-C fd] [-g groupname|#gid] [-p prompt] [-u user
getErrorStream            name|#uid] [-g groupname|#gid] [VAR=value] [-i|-s] [<command>]
getErrorStreamusage: sudo -e [-AknS] [-C fd] [-g groupname|#gid] [-p prompt] [-u user
getErrorStream            name|#uid] file ...

Solution

Xuteng

I strongly recommend editing the sudoers file and allowing users running the application to use specific commands through sudo without prompting for a password instead of using echo passwd sudo

This avoids storing passwords in plaintext (or preferably a little confusing) in applications or configuration files, and you don't need to call the shell using a shell script that calls sudo, etc

Sudoers can be edited by the command visudo Here's how to do it on unbuntu, but it's the same on any UNIX https://askubuntu.com/questions/159007/how-do-i-run-specific-sudo-commands-without-a-password

Supplementary reference number: https://www.sudo.ws/man/1.8.16/sudoers.man.html

I think you asked the wrong question

Authorization on MAC

On MAC applications that need to perform operations that require additional permissions, sudo should not be used to start

The application should use the authorization service

reference:

> Introduction to Authorization Services Programming Guide (apple) > Authorization Services Tasks (apple) > I need to give a java application super user access to view protected files on a mac > Is there any graphical “sudo” for Mac OS X?

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>