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?

 

9 comments
Comment actions Permalink

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.

0
Comment actions Permalink

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..)

 

public class Popup extends AnAction {
@NotNull
private EditorTextField myTextField;

@Override
public void actionPerformed(@NotNull AnActionEvent e) {
Project project = e.getProject();

if (project == null) {
return;
}
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 = null;
try {
processHandler = new OSProcessHandler(generalCommandLine);
} catch (ExecutionException e1) {
e1.printStackTrace();
}
processHandler.startNotify();

myTextField = "test";
JPanel panel = new Panel(new BorderLayout());
panel.add(myTextField, BorderLayout.CENTER);

ComponentPopupBuilder builder = JBPopupFactory.getInstance().createComponentPopupBuilder(panel, myTextField);


final JBPopup popup = builder.createPopup();
popup.setMinimumSize(new Dimension(400, 20));
popup.showCenteredInCurrentWindow(project);

}

@Override
public boolean isDumbAware() {
return false;
}

}
0
Comment actions Permalink

You defined a work directory as project path

generalCommandLine.setWorkDirectory(project.getBasePath());

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

0
Comment actions Permalink

hey Chenarviv

i have same question,how to solve this problem ...bad day

0
Comment actions Permalink

Hey Chenarviv,

Were you able to solve this? If yes, could you post the solution here. Stuck with this since 2 days.

 

0
Comment actions Permalink

I have the same problem. Tried to run command on basePath but getting same error. 

0
Comment actions Permalink

+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.

Caused by: java.io.IOException: Cannot run program "dir" (in directory "C:\Users\accou\workspace\private\TestWorkspaces\Test1"): CreateProcess error=2, The system cannot find the file specified

0
Comment actions Permalink

Hey Kasper,

You can use Files.list(Path dir) instead of command "dir"

E.g. (Project was on kotlin):

val dirName = System.getenv("ProgramFiles") + jetBrainsDirectory
Files.list(File(dirName).toPath())
.filter { x: Path -> x.toFile().isDirectory }
.forEach { path: Path -> check(pluginPath, verifierVersion, path.toString(), logFile) }
0
Comment actions Permalink

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.

0

Please sign in to leave a comment.