我正在使用Eclipse编写Java应用程序。
是否可以以编程方式检测代码是从Eclipse环境(F11)执行,还是在没有Eclipse的情况下使用"java -jar“执行代码。
我知道在Visual中,可以检测到Visual是否执行,但这里不是编写.Net应用程序,而是在Eclipse下编写Java。
发布于 2016-10-12 10:52:39
在过去,我通过检查是否可以找到.project文件来查看我的软件是否在Eclipse中运行。
public static boolean isRunningInEclipse(int folderDepth) {
File projectRoot = new File(Boot.class.getProtectionDomain().getCodeSource().getLocation().getFile());
for (int i = 0; i < folderDepth; i++) {
projectRoot = projectRoot.getParentFile();
if (projectRoot == null || !projectRoot.isDirectory()) {
return false;
}
}
return new File(projectRoot, ".project").isFile();
}如果位于bin文件夹中的主类使用folderDepth = 1调用方法,如果主类位于target/classes/中,则使用folderDepth = 2调用它。
如果您特别想知道它是否在调试器中运行,我建议您检查this answer。
https://stackoverflow.com/questions/39988896
复制相似问题