Search code examples
javaintellij-ideaconsoleintellij-plugin

IntelliJ Plugin - Run Console Command


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.


Solution

  • 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/