Java: Check if your code is running in Eclipse

Veröffentlicht von

Sometimes, for example during debugging, it is useful to check if your code is running from the Eclipse IDE to exclude certain code from execution.

How can this be done? Here is an example:

public class mainClass {

    public static void main(String[] args) {
        String inEclipse = System.getProperty("ineclipse");

        if ((inEclipse == null) || (!inEclipse.equals("true"))) {
            System.out.println("Not running from Eclipse");
            System.out.println(inEclipse);          
        } else {
            System.out.println("Running from Eclipse");
            System.out.println(inEclipse);
        }           
    }
}

The other thing we need to do here is to set the VM argument “ineclipse” in the “Run Configurations”:

image

Here is the output from the example from within Eclipse and from the command line:

compare_output

Kommentar hinterlassen

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert