简体   繁体   English

如何使用内置命令行参数运行Java应用程序?

[英]How to run java application with built-in command line arguments?

I am making a Java application that is stored in a .jar file and can be launched by opening the jar file either from the command line or from clicking the icon. 我正在制作一个Java应用程序,该应用程序存储在.jar文件中,并且可以通过从命令行或单击图​​标打开jar文件来启动。

For the Mac version of the app, I would like the menu bars to appear at the top of screen in the Mac style instead of in the window (the Windows style). 对于Mac版本的应用程序,我希望菜单栏以Mac样式而不是窗口(Windows样式)出现在屏幕顶部。 I know this can be done with the command line: 我知道这可以通过命令行完成:

java -jar App.jar -Dcom.apple.macos.useScreenMenuBar=true

But this won't work if the user doesn't know how to do this. 但是,如果用户不知道如何执行此操作,则将无法使用。 Is there a way to make this command line argument "built in" to the jar file? 有没有办法将此命令行参数“内置”到jar文件中?

您可以通过在代码的main方法或应用程序开始时调用的其他方法中设置系统属性来实现此目的:

System.setProperty("com.apple.macos.useScreenMenuBar", "true")

I would suggest that you bundle the .jar into a standard mac .app file. 我建议您将.jar捆绑到标准的Mac .app文件中。 Then, in Info.plist, you can specify all sorts of runtime items, including "Arguments" and "Properties". 然后,在Info.plist中,您可以指定各种运行时项目,包括“参数”和“属性”。 Take a look at http://developer.apple.com/library/mac/#documentation/Java/Reference/Java_InfoplistRef/Articles/JavaDictionaryInfo.plistKeys.html 看看http://developer.apple.com/library/mac/#documentation/Java/Reference/Java_InfoplistRef/Articles/JavaDictionaryInfo.plistKeys.html

Is is necessary to do it with the command line? 是否需要使用命令行来完成? You could check it in code with System.getProperty("os.name"). 您可以使用System.getProperty(“ os.name”)在代码中对其进行检查。

在我的主要方法中,我将使用内置的os.name参数,如果没有另外设置,则默认使用命令行参数。

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

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