Running multiple run configurations programatically in debug mode

Answered

I am working on creating a java class which runs multiple run configurations based on the inputs provided [external data or existing configurations].Based on the documentation and other posts on this forum. This is what my approach is: 

  1. Get active project using application manager
  2. Get list of run configurations using RunManager
  3. Execute the run configurations using Executors

The problem is that I am getting null pointer exception for even getting the instance of Application object. It looks like I am doing something fundamentally wrong here. 

Here is the code 

private static void runGivenConfiguration(String configName) throws ExecutionException {
ProjectManager projectManager = ProjectManager.getInstance();
Project openProject = projectManager.getOpenProjects()[0];

RunManager runManager = RunManager.getInstance(openProject);
List<RunConfiguration> runConfigurations = runManager.getAllConfigurationsList();

com.intellij.execution.configurations.RunConfiguration selectedRunConfig =null;
for (com.intellij.execution.configurations.RunConfiguration runConfiguration : runConfigurations) {
if(configName.equalsIgnoreCase(runConfiguration.getName())){
selectedRunConfig = runConfiguration;
break;
}

}
if(selectedRunConfig == null){
System.out.println("No config found");
return;
}else{
Executor executor = DefaultDebugExecutor.getDebugExecutorInstance();

ExecutionEnvironmentBuilder
.create(openProject, executor, selectedRunConfig).buildAndExecute();
}
}

The Output is 

java.lang.NullPointerException
at com.intellij.openapi.project.ProjectManager.getInstance(ProjectManager.java:40)
at com.Executor.runGivenConfiguration(AnotherExecutor.java:35)
at com.Executor.main(AnotherExecutor.java:25)

 

I ran ApplicationManager.getApplication() which is called inside ProjectManager.getInstance() separatey an it returns null and hence resulting in null pointer exception

Please suggest on what I can do to make this work or share any resources which can be helpful. My theory is that I am not creating a plugin and maybe that's why I am getting a null pointer exception

References

https://intellij-support.jetbrains.com/hc/en-us/community/posts/360000600150-Programmatically-start-debugger

https://intellij-support.jetbrains.com/hc/en-us/community/posts/207137575-Is-there-any-way-to-trigger-start-Run-Debug-configuration-programmatically-?input_string=Programmatically%20start

 

0
4 comments

Takkar,

Such a method should not provide null/NPE. Maybe it's a matter of the IDE lifecycle, and you're running such code too early?

0
Avatar
Permanently deleted user

Jakub Chrzanowski

 

I am calling this from the main method in Java. I am not sure about running the code too early part here. Here is the full code for your review

import com.intellij.execution.ExecutionException;
import com.intellij.execution.Executor;
import com.intellij.execution.RunManager;
import com.intellij.execution.configurations.RunConfiguration;
import com.intellij.execution.executors.DefaultDebugExecutor;
import com.intellij.execution.runners.ExecutionEnvironmentBuilder;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectManager;

import java.util.List;

public class RunConfigExecutor {

public static void main(String [] args){
try{
runGivenConfiguration("myRunConfiguration");
}catch (Exception e){
e.printStackTrace(System.out);
}

}
private static void runGivenConfiguration(String configName) throws ExecutionException {
ProjectManager projectManager = ProjectManager.getInstance();
Project openProject = projectManager.getOpenProjects()[0];

RunManager runManager = RunManager.getInstance(openProject);
List<RunConfiguration> runConfigurations = runManager.getAllConfigurationsList();

com.intellij.execution.configurations.RunConfiguration selectedRunConfig =null;
for (com.intellij.execution.configurations.RunConfiguration runConfiguration : runConfigurations) {
if(configName.equalsIgnoreCase(runConfiguration.getName())){
selectedRunConfig = runConfiguration;
break;
}

}
if(selectedRunConfig == null){
System.out.println("No config found");
return;
}else{
Executor executor = DefaultDebugExecutor.getDebugExecutorInstance();

ExecutionEnvironmentBuilder
.create(openProject, executor, selectedRunConfig).buildAndExecute();
}
}
}






0

You can't use the static main method in your plugin. It'll merely not work. You get NPE because you lose the context of the whole IDE thanks to such an approach.

0
Avatar
Permanently deleted user

I am not creating a plugin. I want to create a java program through which I can run multiple run configurations based on some logic.

If this is not possible out-of-box, is there another workaround ?

0

Please sign in to leave a comment.