简体   繁体   中英

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.

Question : I want to check if a class has been run from within the NetBeans IDE or not. 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. The System.console() will return null if Application running on Netbeans, but it will return Console Object, if App running on Console.

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 启动的,否则这是不可能的。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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