问题描述
我的Jar文件支持单击并从命令行启动。
My Jar file supports both being clicked, and launched from the command line.
如果有可用的图形环境,我只会通过检查<$来显示GUI c $ c> GraphicsEnvironment.isHeadless()
I will only display the GUI if there is a graphics environment available by checking GraphicsEnvironment.isHeadless()
如果用户双击,我希望能够将日志打印到磁盘上的文件jar文件,如果从命令行启动,则将它们打印到控制台。
I would like to be able to print logs to a file on disk if the user double clicks the jar file, and print them to the console if launched from the command line.
我一直无法找到答案,是否存在任何跨平台环境变量,或者我可以看到的任何其他内容,以了解用户是否使用 java -jar app.jar 启动了我的程序,或者他们是否双击了.jar文件?
I have been unable to find an answer to this, are there any cross-platform environment variables, or anything else I can look at to know whether the user launched my program using java -jar app.jar, or whether they double clicked the .jar file?
推荐答案
执行此操作的一种方法是确定控制台何时为空:
One way to do this is to determine when the console is null:
public static void main(String[] args) {
Console console = System.console();
if(console!=null){
System.out.println("Console is not null");
}else{
System.out.println("Console is null");
}
}
尝试使用命令行从命令行运行代码以下命令:
Try to run the code from the command line using the following command:
java -jar [your_runnable_file.jar]
这篇关于我的Jar是双击还是从命令行启动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!