Execute command in Intellij terminal

Hi there!

I’m in the early stages of developing an IntelliJ plugin and need to execute a command in the IntelliJ terminal. I’m using JBTerminalWidget, which provides the executeCommand method for running commands, but it’s protected. Since I’m working with IntelliJ Community Edition, is there an alternative way to execute a command in the terminal?

Please use org.jetbrains.plugins.terminal.ShellTerminalWidget.executeCommand(shellCommand). So, you need to cast JBTerminalWidget to ShellTerminalWidget.

Hey,

I noticed that my project uses the package:

import com.intellij.terminal.TerminalShellCommandHandler

which includes the method TerminalShellCommandHandler.executeShellCommandHandler(...). However, I can’t seem to access org.jetbrains.plugins.terminal.ShellTerminalWidget.executeCommand(shellCommand) in my current setup.

Should I switch to a different version of the IntelliJ SDK or update my dependencies? I’m encountering an issue where certain commands aren’t executing as expected—for example, mvn commands run correctly, but npm commands are not being handled.

Any advice on resolving these limitations or which version to use would be greatly appreciated!

Hi,
in order to access org.jetbrains.plugins.terminal.ShellTerminalWidget.executeCommand,
you need to add the terminal plugin dependency in plugin.xml

<depends>org.jetbrains.plugins.terminal</depends>

in gradle.properties

platformPlugins=org.jetbrains.plugins.terminal:242.20224.237

Note add correct version by referring [Terminal Plugin for JetBrains IDEs | JetBrains Marketplace](https://plugins.jetbrains.com/plugin/13123-terminal/versions)
in build.gradle.kts

plugins(providers.gradleProperty("platformPlugins").map { it.split(',') })

Hi Kiruthika,

Thank you for your response. My primary objective is to execute a terminal command directly within the user’s IDE—specifically, opening the Run or Terminal prompt inside the IDE (not in an external window) and executing the command there. Is this kind of action supported? If so, do you have any sample code that demonstrates how to achieve it?

For example, this action executes “echo Hello” command in a new terminal tab in the Terminal tool window:

import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.project.DumbAwareAction
import com.intellij.terminal.ui.TerminalWidget
import org.jetbrains.plugins.terminal.TerminalToolWindowManager

class ExecuteEchoHelloInTerminalAction : DumbAwareAction("Execute 'echo Hello' in a new terminal tab") {

  override fun actionPerformed(e: AnActionEvent) {
    val project = e.project ?: return
    val widget: TerminalWidget = TerminalToolWindowManager.getInstance(project).createShellWidget(null, null, true, true)
    widget.sendCommandToExecute("echo Hello")
  }
}
2 Likes