本机 JavaFX OS X app bundle with JLink:无法从 OS X 菜单项中的“org.example.app.Main”更改应用程序名称
Native JavaFX OS X app bundle with JLink: can't change application name from `org.example.app.Main` in OS X menu items
我正在使用 JLink 创建本机 JavaFX 应用程序。创建 OS X app bundle 时,我可以在 Info.plist
中指定 CFBundleName
和 CFBundleDisplayName
,但我没有找到任何设置菜单名称的方法 'Hide'和 'Quit' 项。我得到的是:
如何将 org.example.samplejavafx.Main
重命名为有意义的名称?
我的示例项目:
我将 Maven 与 moditect plugin 一起使用(我必须在生产中使用非模块化第三方库)。这是完整的 pom.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>samplejavafx</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<module.mainClass>org.example.samplejavafx.Main</module.mainClass>
<module.name>samplejavafx</module.name>
</properties>
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>13.0.1</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-graphics</artifactId>
<version>13.0.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<outputDirectory>
${project.build.directory}/modules
</outputDirectory>
</configuration>
</plugin>
<plugin>
<groupId>org.moditect</groupId>
<artifactId>moditect-maven-plugin</artifactId>
<version>1.0.0.Beta2</version>
<executions>
<execution>
<id>add-module-info</id>
<phase>package</phase>
<goals>
<goal>add-module-info</goal>
</goals>
<configuration>
<overwriteExistingFiles>true</overwriteExistingFiles>
<buildDirectory>${project.build.directory}/modules</buildDirectory>
<outputDirectory>${project.build.directory}/modules</outputDirectory>
<jvmVersion>13</jvmVersion>
<modules></modules>
<module>
<mainClass>${module.mainClass}</mainClass>
<moduleInfoSource>
module ${module.name} {
requires javafx.controls;
requires javafx.graphics;
exports org.example.samplejavafx;
}
</moduleInfoSource>
</module>
<jdepsExtraArgs>
<arg>--multi-release</arg>
<arg>13</arg>
</jdepsExtraArgs>
</configuration>
</execution>
<execution>
<id>create-runtime-image</id>
<phase>package</phase>
<goals>
<goal>create-runtime-image</goal>
</goals>
<configuration>
<modulePath>
<path>${project.build.directory}/modules</path>
</modulePath>
<modules>
<module>${module.name}</module>
</modules>
<launcher>
<name>launcher</name>
<module>${module.name}/${module.mainClass}</module>
</launcher>
<compression>2</compression>
<stripDebug>true</stripDebug>
<outputDirectory>${project.build.directory}/jlink-image</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/modules</outputDirectory>
<includeScope>runtime</includeScope>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>${java.home}/bin/java</executable>
<arguments>
<argument>--module-path</argument>
<argument>
${project.build.directory}/modules
</argument>
<argument>--module</argument>
<argument>${module.name}/${module.mainClass}</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>13</source>
<target>13</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
org.example.samplejavafx.Main
的源代码:
package org.example.samplejavafx;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
/**
*
*/
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Label l = new Label("Hello");
Scene scene = new Scene(new StackPane(l), 640, 480);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) throws Exception {
launch(args);
}
}
创建 OS X 应用程序包:
mvn clean install
mkdir -p samplejavafx.app/Contents/MacOS
cp -r target/jlink-image/* samplejavafx.app/Contents/MacOS
Info.plist
:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleName</key>
<string>SampleJavaFX</string>
<key>CFBundleDisplayName</key>
<string>SampleJavaFX App</string>
<key>CFBundleIdentifier</key>
<string>org.example.samplejavafx</string>
<key>CFBundleShortVersionString</key>
<string>1.0.0</string>
<key>CFBundleVersion</key>
<string>1.0.0</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleSignature</key>
<string>smpl</string>
<key>CFBundleExecutable</key>
<string>bin/launcher</string>
<key>NSHumanReadableCopyright</key>
<string>Sample copyright</string>
</dict>
</plist>
您所要求的可以使用新的 jpackage 工具来完成。请参阅 GitHub 上的此示例 https://github.com/dlemmermann/JPackageScriptFX。使用 jpackage 工具,您可以创建一个具有您想要的名称的应用程序包,您还可以为其添加适当的图标,如果您愿意,还可以创建一个特定于平台的安装程序。
我正在使用 JLink 创建本机 JavaFX 应用程序。创建 OS X app bundle 时,我可以在 Info.plist
中指定 CFBundleName
和 CFBundleDisplayName
,但我没有找到任何设置菜单名称的方法 'Hide'和 'Quit' 项。我得到的是:
如何将 org.example.samplejavafx.Main
重命名为有意义的名称?
我的示例项目:
我将 Maven 与 moditect plugin 一起使用(我必须在生产中使用非模块化第三方库)。这是完整的 pom.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>samplejavafx</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<module.mainClass>org.example.samplejavafx.Main</module.mainClass>
<module.name>samplejavafx</module.name>
</properties>
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>13.0.1</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-graphics</artifactId>
<version>13.0.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<outputDirectory>
${project.build.directory}/modules
</outputDirectory>
</configuration>
</plugin>
<plugin>
<groupId>org.moditect</groupId>
<artifactId>moditect-maven-plugin</artifactId>
<version>1.0.0.Beta2</version>
<executions>
<execution>
<id>add-module-info</id>
<phase>package</phase>
<goals>
<goal>add-module-info</goal>
</goals>
<configuration>
<overwriteExistingFiles>true</overwriteExistingFiles>
<buildDirectory>${project.build.directory}/modules</buildDirectory>
<outputDirectory>${project.build.directory}/modules</outputDirectory>
<jvmVersion>13</jvmVersion>
<modules></modules>
<module>
<mainClass>${module.mainClass}</mainClass>
<moduleInfoSource>
module ${module.name} {
requires javafx.controls;
requires javafx.graphics;
exports org.example.samplejavafx;
}
</moduleInfoSource>
</module>
<jdepsExtraArgs>
<arg>--multi-release</arg>
<arg>13</arg>
</jdepsExtraArgs>
</configuration>
</execution>
<execution>
<id>create-runtime-image</id>
<phase>package</phase>
<goals>
<goal>create-runtime-image</goal>
</goals>
<configuration>
<modulePath>
<path>${project.build.directory}/modules</path>
</modulePath>
<modules>
<module>${module.name}</module>
</modules>
<launcher>
<name>launcher</name>
<module>${module.name}/${module.mainClass}</module>
</launcher>
<compression>2</compression>
<stripDebug>true</stripDebug>
<outputDirectory>${project.build.directory}/jlink-image</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/modules</outputDirectory>
<includeScope>runtime</includeScope>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>${java.home}/bin/java</executable>
<arguments>
<argument>--module-path</argument>
<argument>
${project.build.directory}/modules
</argument>
<argument>--module</argument>
<argument>${module.name}/${module.mainClass}</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>13</source>
<target>13</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
org.example.samplejavafx.Main
的源代码:
package org.example.samplejavafx;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
/**
*
*/
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Label l = new Label("Hello");
Scene scene = new Scene(new StackPane(l), 640, 480);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) throws Exception {
launch(args);
}
}
创建 OS X 应用程序包:
mvn clean install
mkdir -p samplejavafx.app/Contents/MacOS
cp -r target/jlink-image/* samplejavafx.app/Contents/MacOS
Info.plist
:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleName</key>
<string>SampleJavaFX</string>
<key>CFBundleDisplayName</key>
<string>SampleJavaFX App</string>
<key>CFBundleIdentifier</key>
<string>org.example.samplejavafx</string>
<key>CFBundleShortVersionString</key>
<string>1.0.0</string>
<key>CFBundleVersion</key>
<string>1.0.0</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleSignature</key>
<string>smpl</string>
<key>CFBundleExecutable</key>
<string>bin/launcher</string>
<key>NSHumanReadableCopyright</key>
<string>Sample copyright</string>
</dict>
</plist>
您所要求的可以使用新的 jpackage 工具来完成。请参阅 GitHub 上的此示例 https://github.com/dlemmermann/JPackageScriptFX。使用 jpackage 工具,您可以创建一个具有您想要的名称的应用程序包,您还可以为其添加适当的图标,如果您愿意,还可以创建一个特定于平台的安装程序。