IntelliJ Plugin - Run Console Command

浪子不回头ぞ 提交于 2020-01-02 11:04:14

问题


I am new to plugin development for IntelliJ and would like to know, how I can execute a command in the command line from within my plugin.

I would like to call, for instance, the command "gulp" in the current projects root directory.

I already tried using

Runtime.getRuntime().exec(commands);

with commands like "cd C:\Users\User\MyProject" and "gulp", but it does not seem to work that way and I wonder, if the plugin API provides an easier method.

Thank you very much. :-)


回答1:


I know its a bit late (1 year later), but recently I was working on an IntelliJ plugin and I had the same issue and this is what I used and it works pretty well.

First, we need to create a list of commands that we need to execute:

  ArrayList<String> cmds = new ArrayList<>();
  cmds.add("./gradlew");

Then

  GeneralCommandLine generalCommandLine = new GeneralCommandLine(cmds);
  generalCommandLine.setCharset(Charset.forName("UTF-8"));
  generalCommandLine.setWorkDirectory(project.getBasePath());

  ProcessHandler processHandler = new OSProcessHandler(generalCommandLine);
  processHandler.startNotify();

hence the generalCommandLine.setWorkDirectory is set to the project directory which could be equivalent to the terminal command cd path/to/dir/




回答2:


The Runtime class provides exec(String[], String[], File) method where the last argument is working directory of the subprocess being launched.

The plugin API provides OSProcessHandler class (as well as other classes like ProcessAdapter) which can help to manage the subprocess, handle its output etc.



来源:https://stackoverflow.com/questions/36853427/intellij-plugin-run-console-command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!