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?

 

0
12 comments

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

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

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

hey Chenarviv

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

0

Hey Chenarviv,

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

 

1

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

1

+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

1

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) }
-1

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.

1

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.

1

To those looking for solution, here I managed to solve my problem (kotlin)

var cmd = GeneralCommandLine("php");
cmd.addParameter("-r");
cmd.addParameter("echo 123;");
1

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

public static void runJmhTest(JMHParameters jmhParameters) throws RunnerException, ExecutionException, InterruptedException {

GeneralCommandLine generalCommandLine = new GeneralCommandLine("java");
String cmd = "-jar target/benchmarks.jar org.sample.JmhTestApp1.linkedListAdd -bm thrpt -f 1 -i 5 -r 1s -tu ms" +
" -w 1s -wi 5 -rf json";
String[] list = cmd.split(" ");
generalCommandLine.addParameters(list);
generalCommandLine.setCharset(StandardCharsets.UTF_8);
generalCommandLine.setWorkDirectory(project.getBasePath());

ExecUtil.execAndReadLine(generalCommandLine);
}

 

 
0

Please sign in to leave a comment.