简体   繁体   English

Java Jar控制台应用程序打开Mac系统菜单栏

[英]Java Jar Console Application opens Mac System Menu Bar

I have a Java console application, that opens the System Menu Bar on Mac (the menubar on the top of the screen) when I run java -jar jarfile.jar <args> . 我有一个Java控制台应用程序,当我运行java -jar jarfile.jar <args>时,它会在Mac上打开系统菜单栏(屏幕顶部的菜单栏)。 I don't use Swing and I don't have any GUI. 我不使用Swing,也没有任何GUI。 Application name in the menubar is just <package>.<mainClass> , the menu just contains eg About, Quit. 菜单栏中的应用程序名称仅为<package>.<mainClass> ,菜单仅包含例如About,Quit。

Since I call this multiple times when running a script, this is a bit disturbing for me and I want to disable it. 由于我在运行脚本时多次调用此命令,因此这对我来说有点不便,我想禁用它。

I am using gradle for building and I build the jar like this: 我正在使用gradle进行构建,并且像这样构建jar:

jar {
  baseName = 'appname'
  from(configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }) {
    exclude "META-INF/*.SF"
    exclude "META-INF/*.DSA"
    exclude "META-INF/*.RSA"
  }

  def commit = "git rev-parse --short HEAD".execute().text.trim()

  manifest {
    attributes(
      'Implementation-Title': 'appname',
      'Implementation-Version': '1.0',
      'Commit-Id': commit,
      'Built-By': System.getProperty('user.name'),
      'Built-Date': new Date(),
      'Built-JDK': System.getProperty('java.version'),
      'Main-Class': mainClassName
    )
  }
}

dependencies {
  compile 'com.lowagie:itext:2.1.3'
  compile 'com.itextpdf:itext-hyph-xml:5.1.0'
}

Is this possible? 这可能吗? Do you need any other information? 您还需要其他信息吗? Unfortunately I don't know what causes the appearing of the menu bar, so I don't really know what else I should provide. 不幸的是,我不知道是什么原因导致菜单栏出现,所以我真的不知道我还应该提供什么。

I created a sample gradle-project: no menu bar, even if I added my dependencies. 我创建了一个示例gradle-project:即使添加了依赖项,也没有菜单栏。 I also tried setting the property apple.laf.useScreenMenuBar but that also didn't make any difference for me. 我也尝试设置属性apple.laf.useScreenMenuBar但这对我没有任何影响。

If you add the following command line option: 如果添加以下命令行选项:

-Djava.awt.headless=true

to the java command line in your script then AWT will not attempt to initialise itself and GUI elements such as the menubar will not be presented. 到脚本中的java命令行,那么AWT不会尝试初始化自身,并且不会显示GUI元素(如菜单栏)。

Is this possible? 这可能吗?

As discussed in Using Headless Mode in the Java SE Platform , certain "heavyweight components require a peer at the operating-system level." 如在Java SE平台使用无头模式中所述 ,某些“重量级组件需要在操作系统级别使用对等端”。 Mac OS X integrates an application containing such components into the desktop environment by adding a minimal application menu entry that can be modified as suggested here and here . Mac OS X通过添加一个最小的应用程序菜单项将包含此类组件的应用程序集成到桌面环境中,该菜单项可以按此处此处的建议进行修改。 The behavior is triggered when such a heavyweight component is instantiated, even lazily. 当实例化这样一个重量级的组件时,即使是延迟地,也会触发该行为。 Using -verbose may help identify the offending container. 使用-verbose可能有助于识别有问题的容器。 Bug report STS-3692 suggests that the issue is difficult to resolve with any generality. 错误报告STS-3692表明,该问题很难一概而论。

For reference, ImageJ has a similar problem that requires the use of a special headless.jar that works in headless mode ; 作为参考, ImageJ存在类似的问题,需要使用特殊的headless.jar才能在无头模式下工作 a similar approach for gradle may be possible, for example , but I've not tried it. 例如 ,可以使用类似的方法进行gradle,但是我还没有尝试过。

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

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