How to detect that code is running inside eclipse IDE
-
You have to elaborate more. What is the context? What are you trying to achieve? – Itay Maman Mar 18 '10 at 07:09
-
I want to execute some additional code in case its running inside Eclipse IDE – Dipak Chaudhari Mar 18 '10 at 07:14
-
2warning ! bad practice detected ! – chburd Mar 18 '10 at 10:57
11 Answers
I am not aware of a generic way to get this kind of information.
One suggestion:
When you start a Java program (or a web server) inside Tomcat, simply add an argument that will indicate that this program is launched by Eclipse.
You can do that by opening the "Open Run Dialog" ("Run" menu), then select your type of application and add in the "Arguments" tab a -DrunInEclipse=true
.
In your Java code, you can check the value of the property:
String inEclipseStr = System.getProperty("runInEclipse");
boolean inEclipse = "true".equalsIgnoreCase(inEclipseStr);
This way, if the program is not running inside Eclipse (or unfortunately if you forgot to set the property) the property will be null
and then the boolean inEclipse
will be equal to false.

- 79,475
- 49
- 202
- 273
Although I agree that having the code detecting a single IDE as the dev env is not an optimal solution, the following code works.
Like others proposed, using a flag at runtime is better.
public static boolean isEclipse() {
boolean isEclipse = System.getProperty("java.class.path").toLowerCase().contains("eclipse");
return isEclipse;
}

- 522
- 8
- 16
-
2Note that this produces a false positive if your project depends on other eclipse projects, such as Jetty. – John Coker Dec 15 '21 at 19:00
1) Create a helper method like:
public boolean isDevelopmentEnvironment() {
boolean isEclipse = true;
if (System.getenv("eclipse42") == null) {
isEclipse = false;
}
return isEclipse;
}
2) Add an environment variable to your launch configuration:
3) Usage example:
if (isDevelopmentEnvironment()) {
// Do bla_yada_bla because the IDE launched this app
}

- 7,628
- 1
- 32
- 50
If your workspace matches some pattern like "/home/user/workspace/Project" you can use the code below:
Boolean desenv = null;
boolean isDevelopment() {
if (desenv != null) return desenv;
try {
desenv = new File(".").getCanonicalPath().contains("workspace");
}
catch (IOException e) {
e.printStackTrace();
}
return desenv;
}

- 15,044
- 7
- 62
- 88
Actually the code is not being run inside Eclipse, but in a separate Java process started by Eclipse, and there is per default nothing being done by Eclipse to make it any different than any other invocation of your program.
Is the thing you want to know, if your program is being run under a debugger? If so, you cannot say for certain. You CAN, however, inspect the arguments used to invoke your program and see if there is anything in there you do not like.

- 73,784
- 33
- 194
- 347
-
3If run with debugger there will be some argument similar to -agentlib:jdwp=transport=dt_socket,suspend=y,address=localhost:2124, which can be obtained from ManagementFactory.getRuntimeMXBean().getInputArguments().get(
) – saugata Mar 18 '10 at 08:57 -
-
@ThorbjørnRavnAndersen ...such as not being able to quote properly arguments that we don't want interpolated, e.g. `'*.txt.gz'`? – Pierre D Jun 09 '14 at 22:01
-
-
@ThorbjørnRavnAndersen I was referring to the last sentence of your answer: *"You CAN, however, inspect the arguments used to invoke your program and see if there is anything in there you do not like"*. That's exactly the problem that brought me to this question: if I want to pass verbatim the String `"*"` or `"*.ext"` (without the double quotes) as command line argument, from a shell I can write `'*'`. But I cannot do that from the Eclipse debugger as it leaves the quotation marks. So in that case, and in that case only, we need to do some args cleaning. – Pierre D Jun 10 '14 at 19:05
-
This is a facility of the shell executing your Java program. As Eclipse do not parse your arguments like your shell, you can just leave out the quotes. – Thorbjørn Ravn Andersen Jun 10 '14 at 20:48
A more generic and precise way, that can be used on any IDE would be loop at:
ManagementFactory.getRuntimeMXBean().getInputArguments()
looking for "-Xdebug" || (starting with) "-agentlib:jdwp=".
I came with this from @saugata comment here.
This is excellent if you want to throw a conditional exception preventing the application from simply exiting. Use a boolean like "ideWait" and add it to Eclipse watch expressions as ideWait=false, so whenever you stop at that throw, and "drop to frame" you can continue debugging happily (I mean it!)

- 1
- 1

- 3,729
- 5
- 32
- 67
I don't think there is any way to do this. But what I would suggest is just a command line argument such as 'debug'. Then in your main method just do
if (args.length > 0 && args[0].equalsIgnoreCase("debug")) {
// do whatever extra work you require here or set a flag for the rest of the code
}
This way you can also get your extra code to run whenever you want just by specifiying the debug parameter but under normal conditions it will never execute.

- 10,031
- 10
- 54
- 83
System.out.println("Is my parent eclipse[.exe]? " +
ProcessHandle.current()
.parent()
.flatMap(parent -> parent.info().command())
.orElse("")
.matches("^.*eclipse(\\.exe)?$"));

- 76
- 2
- 2
- 5
This might work if your alternative execution work flow provides a different set of dependencies:
boolean isRunningInEclipe = false;
try {
Workbench.getInstance();
isRunningInEclipe = true;
} catch (NoClassDefFoundError error) {
//not running in Eclipse that would provide the Workbench class
}

- 10,010
- 7
- 61
- 117
You could detect if you're inside a JAR or not, as per Can you tell on runtime if you're running java from within a jar?
Eclipse will run your app from the classes/
dir, whereas most of your end users will be running the app from a JAR.

- 15,048
- 2
- 66
- 119
You may try something like this:
if (ClassLoader.getSystemResource("org/eclipse/jdt/core/BindingKey.class")!=null){
System.out.println("Running within Eclipse!!!");
} else {
System.out.println("Running outside Eclipse!!!");
}

- 1
-
This didn't work for me. It says I'm "outside Eclipse" when I'm in-line debugging from Eclipse. – MasterHD Feb 17 '23 at 13:15