如何检测是否支持图形界面? [英] How to detect if a graphical interface is supported?
问题描述
我需要我的Java程序有两种显示模式:GUI界面和命令行界面。如果我在Windows,OS X或其他图形环境中运行它,我应该得到GUI界面,但是如果我通过SSH运行它,我应该得到命令行界面。
你实际上有两个问题:
1)检查您是否在无头环境中运行(无图形)。检查此方法:
if(GraphicsEnvironment.isHeadless()){
//非gui模式
} else {
// gui mode
}
您正在运行:
System.getProperty(os.name)
但是,即使您在无头环境中操作,第二个问题也会返回相同的名称。
I need my Java program to have two display modes: a GUI interface and a command line interface. If I run it in Windows, OS X, or another graphical environment I should get the GUI interface, but if I run it via SSH I should get the command line interface.
How can I detect whether a GUI can be displayed or if I should use a command line interface?
解决方案You actually have two questions:
1) Check if you run in a headless environment (no graphics). Check this method:
if (GraphicsEnvironment.isHeadless()) { // non gui mode } else { // gui mode }
2) Check which OS you are running under:
System.getProperty("os.name")
However, the second (2) question will return the same name even though you operate in a headless environment.
这篇关于如何检测是否支持图形界面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!