How to programatically run commands in terminal from plugin

已回答

Hi team, 

How do i run any commands in command prompt or terminal from a plugin. Like,  creating directory(mkdir folder) or cloning any repo(git clone), or any kind of commands which can run on terminal.

 

I have tried this code. but getting errros such as no such file or directory while i am creating but it is saying error=2, No such file or directory

ArrayList<String> cmds = new ArrayList<>();
cmds.add("mkdir testing");

GeneralCommandLine commandLine = new GeneralCommandLine(cmds);
commandLine.setCharset(Charset.forName("UTF-8"));
commandLine.setWorkDirectory("/Users/sainikhilk/Desktop/");

ProcessHandler processHandler = new OSProcessHandler(commandLine);
processHandler.startNotify();
System.out.println("done");


Is this the correct way or is there any better approach to use ? Thanks

0

Looking at the JavaDoc of the GeneralCommandLine class:

Main idea of the class is to accept parameters "as-is", just as they should look to an external process, and quote/escape them
as required by the underlying platform - so to run some program with a "parameter with space" all that's needed is
{@code new GeneralCommandLine("some program", "parameter with space").createProcess()}.

So "mkdir testing" is not a correct way to handle it.

To execute the command and read the output, you may use

ExecUtil.execAndReadLine(GeneralCommandLine commandLine)
0

ExecUtil.execAndReadLine(GeneralCommandLine commandLine) seem work, 

 

but while execute command like  ‘sudo   cmd xxx ',  it require input something,  how to avoid this input step ? if possible automatic fill something 

0

请先登录再写评论。