Running console commands from intelliJ plugin
Answered
Hello,
I'm developing an intelliJ plugin that has to run console commands.
I would like to call commands in the current project's root directory.
I'm having trouble with it. So far I only succeeded to run commands on the plugin's directory.
I tried using code I saw on StackOverflow:
ArrayList<String> cmds = new ArrayList<>();
cmds.add("./gradlew");
GeneralCommandLine generalCommandLine = new GeneralCommandLine(cmds);
generalCommandLine.setCharset(Charset.forName("UTF-8"));
generalCommandLine.setWorkDirectory(project.getBasePath());
ProcessHandler processHandler = new OSProcessHandler(generalCommandLine);
processHandler.startNotify();
But I get this error when I run or debug the task:
Cannot run program ".\gradlew" (in directory ..): CreateProcess error=2, The system cannot find the file specified
It tries to find ./gradlew on the project's dir and obviously it is on the plugin's dir.. can anyone help please?
Please sign in to leave a comment.
Do you need to execute arbitrary commands or just call Gradle targets? Is the gradlew file not in the project's directory? What is "plugin's dir"? Please share full sources of your plugin and sample of project structure/required files if possible.
Hey Yann, thanks for the comment.
I need to execute commands such as "cd" or git commands.
By saying "plugin's dir" I meant the local directory of my intellij plugin and not the project that I'm running the plugin within.
(for example if my plugin is called plugin-test and a project is called Excercise so I meant the local directory of plugin-test)
Here's my plugin source: (it throws exception and get into the catch phrase..)
You defined a work directory as project path
You can find paths to installed plugin there https://intellij-support.jetbrains.com/hc/en-us/articles/206544519-Directories-used-by-the-IDE-to-store-settings-caches-plugins-and-logs
hey Chenarviv
i have same question,how to solve this problem ...bad day
Hey Chenarviv,
Were you able to solve this? If yes, could you post the solution here. Stuck with this since 2 days.
I have the same problem. Tried to run command on basePath but getting same error.
+1
I am getting the same error no matter what command i run, i have even tried "ls" and "dir"
However command like "git" is working.
Hey Kasper,
You can use Files.list(Path dir) instead of command "dir"
E.g. (Project was on kotlin):
Hi Yrakovets,
Thanks for the info, the idea is not to do a dir, I just chose that to test a command that I knew 100% would be on the computer. The users of my plugin will have a CLI rogram installed on their computer and I want to run a command with that program as part of my IntelliJ extension.
Nothing here? I'm facing the same problem, some basic OS programs like ls work, others don't like echo $PATH.
Although, what I'm trying to call is other applications executables and system script, just called ls and echo for testing purposes.
To those looking for solution, here I managed to solve my problem (kotlin)
I finally find a way to run command line in user's project. For example, when I call the method below, it will run " java -jar target/benchmarks.jar org.sample.JmhTestApp1.arrayListAdd -bm thrpt -f 1 -i 5 -r 1s -tu ms -w 1s -wi 5 -rf json ".