java 如何检测代码在 Eclipse IDE 中运行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2468059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to detect that code is running inside eclipse IDE
提问by Dipak Chaudhari
How to detect that code is running inside eclipse IDE
如何检测代码在 Eclipse IDE 中运行
回答by Romain Linsolas
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.
您可以通过打开“打开运行对话框”(“运行”菜单),然后选择您的应用程序类型并在“参数”选项卡中添加 a 来实现-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 nulland then the boolean inEclipsewill be equal to false.
这样,如果程序没有在 Eclipse 中运行(或者不幸的是,如果您忘记设置属性),则该属性将是null,然后布尔值inEclipse将等于 false。
回答by Java42
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
}
回答by cbaldan
The following should work.
以下应该工作。
Although I agree that having the code detecting a single IDE as the dev env is not an optimal solution. Using a flag at runtime is better.
虽然我同意让代码检测单个 IDE 作为开发环境不是最佳解决方案。在运行时使用标志更好。
public static boolean isEclipse() {
boolean isEclipse = System.getProperty("java.class.path").toLowerCase().contains("eclipse");
return isEclipse;
}
回答by Italo Borssatto
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;
}
回答by Aquarius Power
A more generic and precise way, that can be used on any IDE would be loop at:
可以在任何 IDE 上使用的更通用和更精确的方法是在以下位置循环:
ManagementFactory.getRuntimeMXBean().getInputArguments()
ManagementFactory.getRuntimeMXBean().getInputArguments()
looking for "-Xdebug" || (starting with) "-agentlib:jdwp=".
寻找“-Xdebug” || (以)“-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”这样的布尔值并将其添加到 Eclipse 监视表达式中作为 ideWait=false,因此无论何时您在该抛出处停止并“降到帧”,您都可以愉快地继续调试(我是认真的!)
回答by Thorbj?rn Ravn Andersen
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.
如果您的程序在调试器下运行,您想知道的是什么?如果是这样,你不能肯定地说。但是,您可以检查用于调用程序的参数,看看其中是否有您不喜欢的内容。
回答by DaveJohnston
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.
通过这种方式,您还可以通过指定调试参数让您的额外代码随时运行,但在正常情况下它永远不会执行。
回答by Stefan
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
}
回答by Rich
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/,而您的大多数最终用户将从 JAR 运行应用程序。
回答by Dirk
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!!!");
}

