tags:

views:

104

answers:

3

How to detect that code is running inside eclipse IDE

+7  A: 

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.

romaintaz
+1 for setting an explicit runtime option.
Thilo
actually i am looking for generic way.
DD
+1  A: 

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.

DaveJohnston
A: 

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.

Thorbjørn Ravn Andersen
If 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(<int>)
saugata