10

I would like to know if there is a way (programmatically) to check if a code is running within Eclipse, IntelliJ, any other editor or running from the command line (console).

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

Ola Ström
  • 4,136
  • 5
  • 22
  • 41
Nir
  • 1,524
  • 6
  • 23
  • 41
  • 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

6 Answers6

26

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
  • 17,329
  • 10
  • 113
  • 185
5

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
  • 11,095
  • 2
  • 38
  • 49
  • 2
    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
  • 1
    ...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
2

There is no reliable way of doing this without a custom run argument, BUT!

This works more reliably than checking for idea_rt.jar:

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);
}

Source: https://intellij-support.jetbrains.com/hc/en-us/community/posts/360000015340/comments/360000017399

Mark Woon
  • 2,046
  • 1
  • 20
  • 26
0

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,822
  • 17
  • 19
  • 1
    Seems to not be working anymore, at least if its a gradle project. Manually setting a System Property or EnvVariable when launching through IDE seems to be the way to go. – icyerasor Sep 15 '22 at 15:58
  • Yeah. that's probably for the best! – gdt Sep 16 '22 at 16:14
0

Tested on a Mac with Eclipse and IntelliJ:

String xpcsce = System.getenv("XPC_SERVICE_NAME") );

will return "0" with Terminal, "application.org.eclipse.platform.ide...." with Eclipse and "application.com.jetbrains.intellij.ie-EAP...." with IntelliJ...

But XPC Service is a MacOS specific feature so this won't work on any other platform.

piero B
  • 37
  • 6
0

Here is some ways:

    private static boolean checkIntelliJIDEA() {
        try {
            URL ideDetectorClassLocation = IdeDetector.class.getProtectionDomain().getCodeSource().getLocation();
            File ideDetectorClassFile = new File(ideDetectorClassLocation.toURI());
            return ideDetectorClassFile.isDirectory();
        } catch (URISyntaxException e) {
            System.err.println("Error determining if the application is running in an IDE: " + e.getMessage());
        }

        String ideaLaunchProperty = System.getProperty("idea.launcher.port");
        String ideaRunConfigProperty = System.getProperty("idea.run.configuration");
        String classpath = System.getProperty("java.class.path");
         if(classpath.contains("idea_rt.jar")) return true;
         if(ideaLaunchProperty != null) return true;
         if(ideaRunConfigProperty != null) return true;
        if(ideaRunConfigProperty != null) return true;
        try {
            Class.forName("com.intellij.rt.execution.application.AppMainV2");
            Class.forName("com.intellij.rt.execution.application.AppMain");
            return true;
        } catch (ClassNotFoundException e) {
            return false;
        }

    }
pvpb0t
  • 33
  • 6