1

I am using Eclipse to write my Java application.

Is it possible to detect programmatically if code executes from the Eclipse debug environment (F11) or code executes without Eclipse using "java -jar ".

I know that in Visual Studio, it is possible to detect if Visual Studio executes, but I am not writing a .Net application here, but Java under Eclipse.

Sarah Weinberger
  • 15,041
  • 25
  • 83
  • 130
  • 3
    what are you trying to acheive? – Scary Wombat Oct 12 '16 at 00:53
  • 1
    Maybe this will help: http://stackoverflow.com/questions/3776204/how-to-find-out-if-debug-mode-is-enabled – xiaofeng.li Oct 12 '16 at 00:56
  • One option is to just use a Java property, or simply environment variable, and then just set that in Eclipse when running it. This is probably much better alternative than trying to detect a specific development environment, anyway, and enables running the app normally even under Eclipse without modifying code. – hyde Oct 12 '16 at 11:20
  • @ScaryWombat The overall goal was to finally implement revision increment, as Eclipse does not natively provide this feature, but I have other uses for this code, such as debug functionality that the non-Eclipse would not have. This code is quite handy for multiple reasons. – Sarah Weinberger Oct 12 '16 at 21:11

1 Answers1

2

In the past I've used a bit of a hack to see if my software was running in Eclipse by checking if I could find a .project file.

public static boolean isRunningInEclipse(int folderDepth) {
    File projectRoot = new File(Boot.class.getProtectionDomain().getCodeSource().getLocation().getFile());       
    for (int i = 0; i < folderDepth; i++) {
        projectRoot = projectRoot.getParentFile();
        if (projectRoot == null || !projectRoot.isDirectory()) {
            return false;
        }
    }
    return new File(projectRoot, ".project").isFile();
}

If your main class in located in the bin folder you call the method with folderDepth = 1, if your main class is located in target/classes/ you call it with folderDepth = 2.

If you specifically want to know if it is running in the debugger I suggest you check this answer

Community
  • 1
  • 1
THelper
  • 15,333
  • 6
  • 64
  • 104
  • Nice hack. Note you may need to do a few more checks to cover class files being in `bin` vs `target/classes` vs `WEB-INF/classes` but the idea is the same. – AngerClown Oct 12 '16 at 11:07
  • @AngerClown Thx for the compliment! You're right, my code example assumes that classes are in the target/classes/ folder. I'll add this to my answer. – THelper Oct 12 '16 at 11:10
  • 1
    `false == ` should be `!`. But the `exists` test is obsolete, the final `isFile` test implies that its parent exists. Further, the `.toString()` is obsolete. So the whole test can be fused to `return oFile!=null && new File(oFile, ".project").isFile();` – Holger Oct 12 '16 at 21:15
  • @Holger you are right. I've removed Sarah's edit and made my own code example a bit more generic. – THelper Oct 13 '16 at 07:39