简体   繁体   English

如何检测代码在eclipse IDE中运行

[英]How to detect that code is running inside eclipse IDE

如何检测代码在eclipse IDE中运行

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. 当您在Tomcat中启动Java程序(或Web服务器)时,只需添加一个参数即可指示此程序是由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 . 您可以通过打开“打开运行对话框”(“运行”菜单),然后选择您的应用程序类型并在“参数”选项卡中添加-DrunInEclipse=true

In your Java code, you can check the value of the property: 在Java代码中,您可以检查属性的值:

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. 这样,如果程序没有在Eclipse中运行(或者不幸的是,如果你忘了设置属性),那么属性将为null ,然后boolean inEclipse将等于false。

1) Create a helper method like: 1)创建一个辅助方法,如:

public boolean isDevelopmentEnvironment() {
    boolean isEclipse = true;
    if (System.getenv("eclipse42") == null) {
        isEclipse = false;
    }
    return isEclipse;
}

2) Add an environment variable to your launch configuration: 2)在启动配置中添加环境变量:

在此输入图像描述

在此输入图像描述

3) Usage example: 3)用法示例:

if (isDevelopmentEnvironment()) {
    // Do bla_yada_bla because the IDE launched this app
}

The following should work. 以下应该有效。

Although I agree that having the code detecting a single IDE as the dev env is not an optimal solution. 虽然我同意将代码检测到单个IDE作为开发环境并不是最佳解决方案。 Using a flag at runtime is better. 在运行时使用标志更好。

public static boolean isEclipse() {
    boolean isEclipse = System.getProperty("java.class.path").toLowerCase().contains("eclipse");
    return isEclipse;
}

If your workspace matches some pattern like "/home/user/workspace/Project" you can use the code below: 如果您的工作区与“/ home / user / workspace / Project”之类的模式匹配,则可以使用以下代码:

Boolean desenv = null;

boolean isDevelopment() {
    if (desenv != null) return desenv;

    try {
        desenv = new File(".").getCanonicalPath().contains("workspace");
    }
    catch (IOException e) {
        e.printStackTrace();
    }

    return desenv;
}

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. 实际上代码不是在Eclipse中运行,而是在由Eclipse启动的单独Java进程中运行,并且默认情况下,Eclipse没有做任何事情使它与程序的任何其他调用有任何不同。

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. 但是,您可以检查用于调用程序的参数,看看是否有任何您不喜欢的内容。

A more generic and precise way, that can be used on any IDE would be loop at: 可以在任何IDE上使用的更通用和精确的方法是循环:

ManagementFactory.getRuntimeMXBean().getInputArguments()

looking for "-Xdebug" || 寻找“-Xdebug”|| (starting with) "-agentlib:jdwp=". (以)开头“-agentlib:jdwp =”。

I came with this from @saugata comment here . 我来到这个从@saugata评论这里

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!) 使用像“ideWait”这样的布尔值,并将它作为ideWait = false添加到Eclipse监视表达式中,所以无论何时停止该抛出,并“逐帧”,您都可以继续快速调试(我的意思是!)

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'. 但我建议只是一个命令行参数,如'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. 这样,您也可以通过指定调试参数来获取额外的代码,但在正常情况下它将永远不会执行。

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
} 

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? 你可以检测你是否在JAR中, 你可以在运行时告诉你是否从jar中运行java?

Eclipse will run your app from the classes/ dir, whereas most of your end users will be running the app from a JAR. Eclipse将从classes/ dir运行您的应用程序,而大多数最终用户将从JAR运行应用程序。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM