In Java to indicate if code is running from IntelliJ/Eclipse etc or command line

扶醉桌前 提交于 2019-12-05 00:10:01

问题


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.




回答3:


This only works if you're not using one of the "Shorten command line" options from the Run/Debug configuration. As we need to shorten the command line (the classpath was geting too long) I'm now using

public static boolean runningFromIntelliJ()
{
    return System.getProperty("idea.test.cyclic.buffer.size") != null;
}

IntelliJ sets that property when it's running tests.



来源:https://stackoverflow.com/questions/15339148/in-java-to-indicate-if-code-is-running-from-intellij-eclipse-etc-or-command-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!