在Java中,表示如果代码是从的IntelliJ / Eclipse的等或命令行运行(In Java

2019-08-17 11:09发布

我想知道是否有如果代码是通过Eclipse /的IntelliJ或任何其他编辑器运行或命令行中运行写程序的选项

我当时就想,应该使用System.getProperty(),但有没有指示任何财产?

提前致谢

尼尔

Answer 1:

没有可靠的方法来做到这一点。 IDE本身会使用安装在您的系统或一个包装含有IDE上的JRE / JDK。 有没有在SDK / JVM是明确自身标识为从IDE内运行。

如果你需要在你的程序,以确定这一点,通过-D标志,当您从IDE运行代码通过系统属性。 此属性的存在(或不存在)可被用来确定在码是从运行。



Answer 2:

下面的代码可以检测你的代码是否是从跑IntelliJ IDEA与否。

public static boolean runningFromIntelliJ()
{
    String classPath = System.getProperty("java.class.path");
    return classPath.contains("idea_rt.jar");
}

它的工作所测试LinuxMac OS XWindows所以它应该是独立的平台。



Answer 3:

仅当您使用的不是从运行/调试配置“缩短命令行”选项中的一个作品。 因为我们需要缩短命令行(类路径是歌厅太长时间)我现在正在使用

public static boolean runningFromIntelliJ()
{
    return System.getProperty("idea.test.cyclic.buffer.size") != null;
}

的IntelliJ时,它的运行测试设置该属性。



文章来源: In Java to indicate if code is running from IntelliJ/Eclipse etc or command line