6

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

Nir
  • 1,384
  • 5
  • 21
  • 37
  • 3
    IntelliJ: check if System property `java.library.path` contains `C:\Program Files (x86)\JetBrains\IntelliJ IDEA 12.0.1\bin`, or something like it – vikingsteve Mar 11 '13 at 13:14
  • 1
    @vikingsteve: Caution, this only works on `Windows`. See my answer below for a platform-independent solution – BullyWiiPlaza Apr 21 '17 at 22:46

3 Answers3

23

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.

BullyWiiPlaza
  • 14,779
  • 7
  • 95
  • 158
4

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.

Deepak Bala
  • 10,895
  • 2
  • 36
  • 48
  • 1
    That's great if you are running the IDE yourself :) If not, he needs another solution. – vikingsteve Mar 11 '13 at 13:36
  • True. I don't see any reliable solution for the problem though. At least none that would not require user intervention to detect the environment. – Deepak Bala Mar 11 '13 at 13:53
  • Well did u see my comment on the question. It appears that IntelliJ leaves a hint in `java.library.path`... at least on v12. – vikingsteve Mar 11 '13 at 14:52
  • How is that a reliable solution that works across any IDE ? You cannot assume that the presence of the word 'eclipse' or 'intellij' on a library path means that the application was launched from the IDE. RCP applications still need eclipse equinox libraries. The same applies to applications that extend the Netbeans framework. Equinox can also serve as an OSGi container outside of eclipse. – Deepak Bala Mar 11 '13 at 16:22
  • ...of course it won't work across all IDE's (how many IDE's are there in the universe?), however if he only wants to identify a couple of them (for example Eclipse or IntelliJ) then a little bit of detective work might indeed show that from the runtime environment. – vikingsteve Mar 12 '13 at 08:48
1

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.

gdt
  • 1,774
  • 16
  • 19