Dipak Chaudhari
Dipak Chaudhari

Reputation: 475

How to detect that code is running inside eclipse IDE

How to detect that code is running inside eclipse IDE

Upvotes: 31

Views: 15839

Answers (11)

Display Name
Display Name

Reputation: 76

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

Upvotes: 1

cbaldan
cbaldan

Reputation: 553

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;
}

Upvotes: 7

Rich
Rich

Reputation: 15465

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.

Upvotes: 0

Stefan
Stefan

Reputation: 12280

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
} 

Upvotes: 0

Aquarius Power
Aquarius Power

Reputation: 3985

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!)

Upvotes: 2

Italo Borssatto
Italo Borssatto

Reputation: 15689

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;
}

Upvotes: 2

Java42
Java42

Reputation: 7706

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
}

Upvotes: 7

Dirk
Dirk

Reputation: 1

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!!!");
}

Upvotes: -2

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.

Upvotes: 2

DaveJohnston
DaveJohnston

Reputation: 10151

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.

Upvotes: 1

Romain Linsolas
Romain Linsolas

Reputation: 81627

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.

Upvotes: 22

Related Questions