简体   繁体   English

如何检查 Java 类是否在 NetBeans IDE 中运行?

[英]How-to check if java class was run from within the NetBeans IDE?

Background: I use a command line based user-interface to use ( = mostly test ) utility classes.背景:我使用基于命令行的用户界面来使用(= 主要是测试)实用程序类。 When I use the NetBeans IDE I make use of 'Eric's console' which enables command line i/o from within NetBeans.当我使用 NetBeans IDE 时,我使用了“Eric 的控制台”,它可以从 NetBeans 中启用命令行输入/输出。

Question : I want to check if a class has been run from within the NetBeans IDE or not.问题:我想检查一个类是否在 NetBeans IDE 中运行过。 Is this possible and if so how to implement this?这是可能的,如果可以,如何实现?

检查System.getProperty("user.dir")是运行应用程序的地方。

You can use System.console() to check if it running on Netbeans or not.您可以使用System.console()检查它是否在 Netbeans 上运行。 The System.console() will return null if Application running on Netbeans, but it will return Console Object, if App running on Console.如果应用程序在 Netbeans 上运行, System.console()将返回null ,但如果应用程序在控制台上运行,它将返回控制台对象。

So we can make check like this:所以我们可以这样检查:

if(System.console() == null){
    System.out.println("This Application is running on Netbeans");
} else {
    System.out.println("This Application is running on Console");
}

The only reliable way to do this is to explicitly tell your program either by passing a special argument or a system property which your code then acts accordingly upon.唯一可靠的方法是通过传递一个特殊参数或系统属性来明确告诉您的程序,然后您的代码会根据这些属性进行相应操作。

Otherwise you have to rely on heuristics which eventually will break.否则,您必须依赖启发式方法,这最终会失败。

除非您添加一个特定的命令行参数来指示程序是从 NetBeans 启动的,否则这是不可能的。

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

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