How to programatically run commands in terminal from plugin
Answered
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
Please sign in to leave a comment.
Looking at the JavaDoc of the GeneralCommandLine class:
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) 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