2

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>. I don't use Swing and I don't have any GUI. Application name in the menubar is just <package>.<mainClass>, the menu just contains e.g. 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:

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. I also tried setting the property apple.laf.useScreenMenuBar but that also didn't make any difference for me.

dinfuehr
  • 587
  • 6
  • 13

2 Answers2

3

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.

Steve C
  • 18,876
  • 5
  • 34
  • 37
2

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." 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. The behavior is triggered when such a heavyweight component is instantiated, even lazily. Using -verbose may help identify the offending container. Bug report STS-3692 suggests that the issue is difficult to resolve with any generality.

For reference, ImageJ has a similar problem that requires the use of a special headless.jar that works in headless mode; a similar approach for gradle may be possible, for example, but I've not tried it.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045