Detecting Intellij from within main methods


Is there a reliable way to detect that a test or main method is being run within Intellij.  My use case is I have a command line app, but while developing it in Intellij I may want to turn more debug features on, or change the way error reporting is done.

In my case end users might also be using Intellij to edit (typechecked) kotlin scripts, and so I'd like to make this as smooth as possible. Is anyone doing anything similar and are there any nice features to use like further integration with Intellij debuggers, log views, open up the failing script etc?


Pass some environment variable or VM option from IntelliJ IDEA Run/Debug configuration.


I'd like to do this automatically for various runs e.g. right click to run a test class, or launch some main method.  I'm mainly curious if there are good patterns to do this correctly and safely in code without requiring extra config.


Change your default run/debug config.

Or you could do something crazy like this:


public static void main(String[] args)  {
boolean intellij = false;
try {
intellij = Main.class.getClassLoader().loadClass("com.intellij.rt.execution.application.AppMainV2") != null;
} catch (ClassNotFoundException e) {

idea_rt.jar may be not present in the classpath if is set in Help | Edit Custom Properties, so it's not a 100% reliable way.


Please sign in to leave a comment.