Run a command-line command in plugin
Answered
Hello,
I need to run a command-line command in my plugin and get results. Could somebody direct me on how to implement this logic? Which classes to use?
Thank you!
Alex.
Please sign in to leave a comment.
I used ProcessBuilder from java.lang
https://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html
If you don't set directory, the command will be run at [user]\.Idea[version]\
Thank you, Yurii! I will try.
Also, I found additional solution using IntelliJ API (example on Scala):
import java.util
import com.intellij.execution.configurations.GeneralCommandLine
import com.intellij.execution.process.OSProcessHandler
import com.intellij.execution.process.ProcessHandler
import java.nio.charset.Charset
val commands: util.ArrayList[String] = new util.ArrayList[String]
commands.add("some_command")
commands.add("parameter1")
val generalCommandLine = new GeneralCommandLine(commands)
generalCommandLine.setCharset(Charset.forName("UTF-8"))
generalCommandLine.setWorkDirectory(project.getBasePath)
val commandLineOutputStr = ScriptRunnerUtil.getProcessOutput(generalCommandLine)
Best regards,
Alex.