how can i get access to write /etc/hosts

已回答

I am developing a plugin to update file /etc/hosts on Mac, but this file is read only, how can i get access to write /etc/hosts

0

You can try using

com.intellij.execution.util.ExecUtil.sudo(commandLine: GeneralCommandLine, prompt: @Nls String)

 

1

Thank you for your help, Jakub Chrzanowski, it works.

For simple command, we can use ExecUtil.sudo, but for "echo", we need to use script like this

StringBuilder shellScript = new StringBuilder("#!/bin/sh\n");
for (String writeLine : writeLines) {
shellScript.append(String.format("echo %s >> /etc/hosts\n", writeLine));
}

File script = ExecUtil.createTempExecutableScript("modify_hosts", ".sh", shellScript.toString());
GeneralCommandLine scriptCommandLine = new GeneralCommandLine(script.getPath());
ProcessOutput output = ExecUtil.sudoAndGetOutput(scriptCommandLine, "modify /etc/hosts");
0

请先登录再写评论。