shadow
shadow

Reputation: 11

How can I change the app name and icon from the default "Java" to something else with JavaFX on MacOS 14?

I want to change the app name displayed on the dock to something else instead of the default "Java".

I tried to change to use stage.setTitle("SomeRandomTitle") but it changes the name displayed on the window only.

I am using Intellij Idea and I am on MacOS 14.2.

App name on the dock:

App name on the dock

Upvotes: 0

Views: 434

Answers (1)

jewelsea
jewelsea

Reputation: 159546

Using jpackage to customize the application name and icon on OS X

The image you show is the default icon and name for a Java application running on OS X. Its display is normal.

To customize the icon displayed in the OS X dock for your application, it is necessary to create an application installer. jpackage can be used to create an installer that defines a custom icon for the application. For certain distributions, such as via the Apple app store, you may need a paid developer certificate for signing OS X applications.

The jpackage documentation describes part of the process for this:

Apple provides information on obtaining developer certificates:

Various types of certificates need to be obtained from Apple. The types of certificates required differ depending on the target for the app (for instance if it will be deployable to the Apple App Store). Information on the required types of certificates is in the linked jpackage documentation.

Example for packaging an application with a custom icon

Software used:

  • OpenJDK 22.0.2
  • JavaFX 22.0.2 SDK
  • JavaFX 22.0.2 JMODS

All versions used were for an x64 Mac (M series processors need aarch64 JDK/SDK/JMODS for Mac and an M series Mac to create the appropriate package).

HelloWorld.java source was used.

package helloworld;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
 
public class HelloWorld extends Application {     
    @Override
    public void start(Stage primaryStage) {
        Button btn = new Button();
        btn.setText("Say 'Hello World'");
        btn.setOnAction(e ->
                System.out.println("Hello World!")                
        );
        
        StackPane root = new StackPane();
        root.getChildren().add(btn);

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    public static void main(String[] args) {
        launch(args);
    }
}

And this module-info.java:

module hello {
    requires javafx.controls;
    exports helloworld;
}

JAVA_HOME was:

export JAVA_HOME=`/usr/libexec/java_home -v 22.0.2`

The jar file was created using:

javac -p javafx-sdk-22.0.2/lib -d out module-info.java helloworld/HelloWorld.java
jar --verbose --create -p javafx-jmods-22.0.2 --file hello.jar --module-version 1.0 -C out module-info.class -C out helloworld/HelloWorld.class

The jpackage command used was:

jpackage --icon Tatice-Cristal-Intense-Apple-multicolor.icns --name HelloApp -p javafx-jmods-22.0.2:hello.jar --module hello/helloworld --type dmg

Where the .icns file was obtained from icon archive (annoying advertising in the link unfortunately).

You can see the result here, a custom icon image (the multi-colored apple icon) for the application and a custom name "HelloApp" as defined by the jpackage command, as opposed to the default folder icon and "java" name for the running application:

icon image

tree:

$ tree
.
├── HelloApp-1.0.dmg
├── HelloApp-1.0.pkg
├── Tatice-Cristal-Intense-Apple-multicolor.icns
├── hello.jar
├── helloworld
│   └── HelloWorld.java
├── javafx-jmods-22.0.2
│   ├── javafx.base.jmod
│   ├── javafx.controls.jmod
│   ├── javafx.fxml.jmod
│   ├── javafx.graphics.jmod
│   ├── javafx.media.jmod
│   ├── javafx.swing.jmod
│   └── javafx.web.jmod
├── javafx-sdk-22.0.2
│   ├── legal
│   │   ├── javafx.base
│   │   │   ├── ADDITIONAL_LICENSE_INFO
│   │   │   ├── ASSEMBLY_EXCEPTION
│   │   │   └── LICENSE
│   │   ├── javafx.controls
│   │   │   ├── ADDITIONAL_LICENSE_INFO
│   │   │   ├── ASSEMBLY_EXCEPTION
│   │   │   └── LICENSE
│   │   ├── javafx.fxml
│   │   │   ├── ADDITIONAL_LICENSE_INFO
│   │   │   ├── ASSEMBLY_EXCEPTION
│   │   │   └── LICENSE
│   │   ├── javafx.graphics
│   │   │   ├── ADDITIONAL_LICENSE_INFO
│   │   │   ├── ASSEMBLY_EXCEPTION
│   │   │   ├── LICENSE
│   │   │   ├── jpeg_fx.md
│   │   │   └── mesa3d.md
│   │   ├── javafx.media
│   │   │   ├── ADDITIONAL_LICENSE_INFO
│   │   │   ├── ASSEMBLY_EXCEPTION
│   │   │   ├── LICENSE
│   │   │   ├── directshow.md
│   │   │   ├── glib.md
│   │   │   ├── gstreamer.md
│   │   │   └── libffi.md
│   │   ├── javafx.swing
│   │   │   ├── ADDITIONAL_LICENSE_INFO
│   │   │   ├── ASSEMBLY_EXCEPTION
│   │   │   └── LICENSE
│   │   └── javafx.web
│   │       ├── ADDITIONAL_LICENSE_INFO
│   │       ├── ASSEMBLY_EXCEPTION
│   │       ├── LICENSE
│   │       ├── icu_web.md
│   │       ├── libxml2.md
│   │       ├── libxslt.md
│   │       └── webkit.md
│   ├── lib
│   │   ├── javafx-swt.jar
│   │   ├── javafx.base.jar
│   │   ├── javafx.controls.jar
│   │   ├── javafx.fxml.jar
│   │   ├── javafx.graphics.jar
│   │   ├── javafx.media.jar
│   │   ├── javafx.properties
│   │   ├── javafx.swing.jar
│   │   ├── javafx.web.jar
│   │   ├── libdecora_sse.dylib
│   │   ├── libfxplugins.dylib
│   │   ├── libglass.dylib
│   │   ├── libglib-lite.dylib
│   │   ├── libgstreamer-lite.dylib
│   │   ├── libjavafx_font.dylib
│   │   ├── libjavafx_iio.dylib
│   │   ├── libjfxmedia.dylib
│   │   ├── libjfxmedia_avf.dylib
│   │   ├── libjfxwebkit.dylib
│   │   ├── libprism_common.dylib
│   │   ├── libprism_es2.dylib
│   │   └── libprism_sw.dylib
│   └── src.zip
├── module-info.java
└── out
    ├── helloworld
    │   └── HelloWorld.class
    └── module-info.class

Setting the app name in the menu bar

There is a mac specific option in jpackage for customizing the menu bar name:

--mac-package-name name

Name of the application as it appears in the Menu Bar. This can be different from the application name. This name must be less than 16 characters long and be suitable for displaying in the menu bar and the application Info window. Defaults to the application name.

If you set a --name value (like this example) it will also customize the name displayed in the menu bar, so you don't need to set the --mac-package-name value unless you need it to be something different (likely for length restriction reasons).

App menu drop-down after selecting the app name (HelloApp) in the Mac menu bar after running the application.

app menu

Further info for customizing the Mac system menu is provided in:

Info regarding Apple signing certificates for recent JDK/JavaFX/OS X versions

I created am OS X package using jpackage with JavaFX 22.0.2 and OpenJDK 22.0.2 on OS X Sonoma 14.5.

Unlike my experience with earlier attempts at creating packages on OS X using jpackage, the resultant package (either as type pkg or dmg) was installable and usable without signing the application with a developer certificate.

I am not sure if this worked because of:

  • Different security settings on my Mac OS OR
  • Changes in recent OS X versions OR
  • Changes in the JDK packaging tool for JDK 21 or some other recent JDK version OR
  • some slight differences in how I created the package using jpackage.

Though this works, you are probably best off, for a professional installation, obtaining and using a paid certificate from Apple. But at least, for recent software builds, it is possible to create a working installed application on OS X without using a paid certificate.

Notes

In related comments regarding why the unsigned application can be executed, Trashgod noted:

You may have exempted the unsigned application from gatekeeper scrutiny when first launching it, as suggested here.

And trashgod also provided additional information on how the application name is assigned:

The desired bundle name should be specified in your Info.plist, like this.

I think jpackage sets up the bundle name in the Info.plist so that the application is assigned the name configured when jpackage is used to build the application installer so that manual configuration of the value is not required.

Upvotes: 2

Related Questions