18

I'm writing a Java Swing application for the Mac using Java 1.6. I've read a number of tutorials that step you through how to better integrate your Java application with OS X, but there's one thing I haven't been able to get working. I can't get the application name (the first, bolded menu item in the Mac menu bar) to display. By default, the fully-qualified class name of the main class is shown and I can't get it to change.

This site says that you have to set the following property:

System.setProperty("com.apple.mrj.application.apple.menu.about.name", "AppName");

But that doesn't work (I'm running 10.6, so maybe the property name changed?).

When I create a new Java project in XCode (I normally use Eclipse), the name somehow magically gets set! (it starts you out with a runnable, boiler-plate application) I've looked all around the XCode project for how this is done, but I can't figure it out!

My guess is that it only sets the application name if you wrap your Java application up in a Mac *.app package, but was wondering if anyone knew the answer. Thanks.

EDIT: Interestingly, it sets the application name if I package my application in a runnable JAR file, but not if I run it from Eclipse.

Michael
  • 34,873
  • 17
  • 75
  • 109

2 Answers2

31

You should do the following during app initialization, before GUI is built:

// take the menu bar off the jframe
System.setProperty("apple.laf.useScreenMenuBar", "true");

// set the name of the application menu item
System.setProperty("com.apple.mrj.application.apple.menu.about.name", "AppName");

// set the look and feel
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

UPDATE. Above code works in Java 1.5, this code may not work in 1.6

For new java see documentation:

  1. Either use -Xdock:name command-line property: -Xdock:name=YourAppName
  2. Or set CFBundleName in information property list file (plist)
uthark
  • 5,333
  • 2
  • 43
  • 59
  • 1
    Yes, I am setting the property before calling `UIManager.setLookAndFeel()` and before I display my GUI. I'm running Java 1.6, so I guess the property doesn't work with that version? *Adding a `-Xdock:name=AppName` VM argument worked*, thanks. – Michael Jul 01 '10 at 13:49
  • 1
    @uthark Please improve the answer (bullet 1) with a more complete example, such as -Xdock:name="My App Name" -- it is not obvious without reading the developer.apple.com page. Thanks. – djb Feb 20 '16 at 13:20
  • The link to documentation gives a 404. I could not find the documentation in the site. – rrrocky Oct 15 '17 at 13:37
  • @rockydgeekgod 7 years ago, when I answered, it was there, updated link to a new location. – uthark Oct 16 '17 at 13:52
5

On Mac 10.7.5, programatically setting the property will work with with Java 1.6 but not with Java 1.7.

Ardent Coder
  • 3,777
  • 9
  • 27
  • 53
PeterVermont
  • 1,922
  • 23
  • 18
  • I know this is more of a comment than an answer but do not know how (or am not privileged enough) to add comments to the original question. – PeterVermont Jan 05 '13 at 18:36