Java OS X Lion Set application name doesn't work
Asked Answered
B

6

32

I'm trying to change the application name displayed into the menu bar of OS X but i can't succeed with that. I have tried settings as the first statement in the main method with the following code :

System.setProperty("apple.laf.useScreenMenuBar", "true");
System.setProperty("com.apple.mrj.application.apple.menu.about.name", "Alessio");
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

but it doesn't work, in the menu bar is displayed main :

ScreenShot

I'm on Mac OS X Lion (10.7.2).

Is there a way to change the application name in the menu bar? If so, how?

Bravin answered 18/1, 2012 at 23:4 Comment(1)
AFAIR Java Web Start can configure the application title on OS X.Elgar
K
11

Apparently, you can do it adding the following when you add the following options to the command line:

-Xdock:name="Alessio"

While com.apple.mrj.application.apple.menu.about.name is the right property name, I think you'll be setting it too late. Have you tried it on the command line as:

-Dcom.apple.mrj.application.apple.menu.about.name=Alessio

For more information about writing Java Apps for OS X: http://www.oracle.com/technetwork/articles/javase/javatomac-140486.html

It may also be worth looking at something like this: http://launch4j.sourceforge.net/

Karleen answered 18/1, 2012 at 23:26 Comment(0)
X
24

Using JDK8, you can set the apple.awt.application.name property to affect the Application menu name.

However, Martijn Courteaux’s warning still applies: you must do this before any AWT classes are loaded. And AWT classes will be loaded before your main() method runs if it lives in a subclass of JFrame.

Reference:

http://hg.openjdk.java.net/jdk8u/jdk8u/jdk/file/5c1d06cd7d7b/src/macosx/native/sun/osxapp/NSApplicationAWT.m#l157

Ximenez answered 3/11, 2015 at 0:16 Comment(3)
Works for me, MacOSX 10.10 Yosemite, Oracle JDK 8Eliga
Works for me MacOS 10.13 High SierraCaballero
Note that putting it in the main() will not work if you have SplashScreen-Image in your MANIFEST.MF fileChesney
K
11

Apparently, you can do it adding the following when you add the following options to the command line:

-Xdock:name="Alessio"

While com.apple.mrj.application.apple.menu.about.name is the right property name, I think you'll be setting it too late. Have you tried it on the command line as:

-Dcom.apple.mrj.application.apple.menu.about.name=Alessio

For more information about writing Java Apps for OS X: http://www.oracle.com/technetwork/articles/javase/javatomac-140486.html

It may also be worth looking at something like this: http://launch4j.sourceforge.net/

Karleen answered 18/1, 2012 at 23:26 Comment(0)
I
8

I'm not sure if this also works for OS X Lion, but I'm on Mountain Lion.

After some testing, my conclusion is that you can use the old approach if and only if you don't do anything with

  • java.awt.Toolkit
  • Setting look and feel

before setting the app name.

Some things that use Toolkit are: (Feel free to edit this answer and add items)

  • Loading a java.awt.Font (@see static initializer of Font)
  • Acquiring the Screen resolution.
  • Loading a class that extends JFrame (such as if the class contains your main method)
Interfaith answered 23/7, 2012 at 16:46 Comment(2)
I moved the relevant System.setProperty() calls from my JFrame class into my main class and it started working -- no command line flags involved. Thanks.Wilbertwilborn
At first I just moved the System.setProperty(...) in an own class in the main method, which didn't work because I had a UIManager.setLookAndFeel(...) call before. Now it works perfectly by putting this call AFTER the System.setProperty(...) calls :)Siouan
H
2

I've had the same problem and discovered this: if your main method is a member of one of your GUI classes (for example, one derived from JFrame), when the JVM loads your class it will also need to load some other AWT classes. These can interact with java.awt.Toolkit in static initializers, which, as Martijn observed, causes the property to be checked before your main method has had a chance to set it.

Try moving the main method into a separate class that doesn't extend any Swing or AWT classes and see if it works.

Hoyle answered 22/9, 2012 at 0:54 Comment(2)
OS X 10.1.1 with java 1.8.0_23-b17: Won't work, even if main() is placed alone into a tiny class and called System.setProperty() at the very first.Picul
It does work if you set the apple.awt.application.name property. Just tested on OS X 11.0.1Atp
D
1

try to put System.setProperty("com.apple.mrj.application.apple.menu.about.name", "Alessio"); on your main code before anything else

Deneendenegation answered 3/2, 2015 at 10:32 Comment(0)
D
0

Creating class that only has main method allows you to change application name.

Delija answered 20/2, 2014 at 21:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.