Running multiple run configurations programatically in debug mode
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:
- Get active project using application manager
- Get list of run configurations using RunManager
- 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
Please sign in to leave a comment.
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?
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
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.
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 ?