In Java to indicate if code is running from Intell

2019-03-29 22:25发布

问题:

I would like to know if there is an option to write programmatic if code is running via Eclipse/IntelliJ or any other editor or running from command line

I was thinking to use System.getProperty() but is there any property that indicate it?

Thanks in advance

Nir

回答1:

There is no reliable way to do that. The IDE itself would use a JRE / JDK that is installed on your system or one that comes packaged with the IDE. There is nothing in the SDK / JVM that specifically identifies itself as running from within an IDE.

If you need to identify this in your program, pass a system property through the -D flag when you run the code from the IDE. The presence (or absence) of this property can be used to determine where the code is being run from.



回答2:

The following code can detect whether your code is ran from IntelliJ IDEA or not.

public static boolean runningFromIntelliJ()
{
    String classPath = System.getProperty("java.class.path");
    return classPath.contains("idea_rt.jar");
}

It's tested working on Linux, Mac OS X and Windows so it should be platform independent.