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?

0

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

0
Avatar
Permanently deleted user

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.

0

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) {
}
System.out.println(intellij);
}
0

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

0

请先登录再写评论。