Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect that code is running inside eclipse IDE

Tags:

java

ide

How to detect that code is running inside eclipse IDE

like image 727
Dipak Chaudhari Avatar asked Mar 18 '10 07:03

Dipak Chaudhari


4 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.

like image 126
Romain Linsolas Avatar answered Nov 07 '22 03:11

Romain Linsolas


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;
}
like image 33
cbaldan Avatar answered Nov 07 '22 05:11

cbaldan


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:

enter image description here

enter image description here

3) Usage example:

if (isDevelopmentEnvironment()) {
    // Do bla_yada_bla because the IDE launched this app
}
like image 6
Java42 Avatar answered Nov 07 '22 05:11

Java42


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.

like image 2
Thorbjørn Ravn Andersen Avatar answered Nov 07 '22 04:11

Thorbjørn Ravn Andersen