确定 Java 应用程序是否在 Eclipse 中处于调试模式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1109019/
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
Determine if a java application is in debug mode in Eclipse
提问by Serxipc
I want to change the logging level depending if I'm debbugging or not, but I can't find a code snippet to check if the application is running in debug mode.
我想根据我是否正在调试来更改日志记录级别,但是我找不到代码片段来检查应用程序是否在调试模式下运行。
I'm using eclipse to debug the application, so if the solution only works within Eclipse it will be fine.
我正在使用 eclipse 来调试应用程序,所以如果该解决方案仅适用于 Eclipse,那就没问题了。
采纳答案by kgiannakakis
You could modify the Debug Configuration. For example add a special VM argument only in the Debug Configuration. You can use System.getProperties()
to read the supplied arguments.
您可以修改调试配置。例如,仅在调试配置中添加特殊的 VM 参数。您可以使用System.getProperties()
读取提供的参数。
Even better, modify the configurations (Run and Debug) to load a different logging configuration file. It isn't good if you need to write code to determine the logging level. This should only be a matter of configuration.
更好的是,修改配置(运行和调试)以加载不同的日志配置文件。如果您需要编写代码来确定日志记录级别,那就不好了。这应该只是配置问题。
回答by VonC
If you are setting the debug level from your own program, may be a line like:
如果您从自己的程序设置调试级别,可能是这样的一行:
public static final boolean DEBUG_MODE = System.getProperty("java.vm.info", "").contains("sharing");
would do the trick.
会做的伎俩。
Just tested it in eclipse3.5:
刚刚在eclipse3.5中测试过:
package test;
public class Test
{
/**
* @param args
*/
public static void main(String[] args)
{
System.out.println(System.getProperty("java.vm.info", ""));
}
}
will display:
将显示:
mixed mode, sharing
if launched without debug
如果在没有调试的情况下启动
mixed mode
if executed with debug launcher
如果使用调试启动器执行
Joachim Sauercomments:
约阿希姆·绍尔评论:
This is highly system depending.
I assume the "sharing" indicates that cross-VM class-sharing is active.
This is a very new feature and is only available on some platforms.
Furthermore there can be many possible reasons to en- or disable it, so I wouldn't use this for debug-mode detection.
这是高度依赖于系统的。
我假设“共享”表示跨虚拟机类共享处于活动状态。
这是一项非常新的功能,仅在某些平台上可用。
此外,启用或禁用它可能有很多可能的原因,因此我不会将其用于调试模式检测。
(Note: I tested it with the latest jdk1.6b14. I leave this as a CW answer.)
(注意:我用最新的 jdk1.6b14 对其进行了测试。我将此留作 CW 答案。)
回答by Manuel Selva
Have a look here:
看看这里:
http://wiki.eclipse.org/FAQ_How_do_I_use_the_platform_debug_tracing_facility%3F
http://wiki.eclipse.org/FAQ_How_do_I_use_the_platform_debug_tracing_facility%3F
Moreover, I think you can't know if your app is run in debug mode. The only thing you can do is to pass an argument to your JVM when you debug.
此外,我认为您无法知道您的应用程序是否在调试模式下运行。您唯一能做的就是在调试时将参数传递给 JVM。
Manu
摩奴
回答by J-16 SDiZ
Have you tried add a vm argument in the eclipse run config?
您是否尝试过在 eclipse 运行配置中添加一个 vm 参数?
Pass this as a VM Argument
将此作为 VM 参数传递
-Ddebug=true
then you can do Boolean.getBoolean("debug")
to check this.
然后你可以Boolean.getBoolean("debug")
检查这个。
回答by Thorbj?rn Ravn Andersen
There is not an officially sanctioned way to reliably determine if any given JVM is in debug mode from inside the JVM itself, and relying on artifacts will just break your code some time in the future.
没有官方认可的方法可以从 JVM 内部可靠地确定任何给定的 JVM 是否处于调试模式,并且依赖工件只会在将来的某个时间破坏您的代码。
You will therefore need to introduce a methology yourself. Suggestions:
因此,您需要自己介绍一种方法。建议:
- A system property.
- An environment variable (shell variable like $HOME or %HOME%)
- Ask the JVM about the physical location of a given resource - http://www.exampledepot.com/egs/java.lang/ClassOrigin.html- and based on it, make your decision (does the path contain the word "debug"? is it inside a jar or an unpacked class file? etc).
- JNDI
- The existance or content of a particular resource.
- 系统属性。
- 环境变量(shell 变量,如 $HOME 或 %HOME%)
- 向 JVM 询问给定资源的物理位置 - http://www.exampledepot.com/egs/java.lang/ClassOrigin.html- 并根据它做出决定(路径是否包含“调试”一词? 是在 jar 中还是在解压的类文件中?等)。
- JNDI
- 特定资源的存在或内容。
回答by topgun_ivard
Found the answer on how-to-find-out-if-debug-mode-is-enabled
找到了关于how-to-find-out-if-debug-mode-is-enabled的答案
boolean isDebug = java.lang.management.ManagementFactory.getRuntimeMXBean().
getInputArguments().toString().indexOf("-agentlib:jdwp") > 0;
This will check if the Java Debug Wire Protocolagent is used.
这将检查是否使用了Java Debug Wire Protocol代理。