Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
199 views
in Technique[技术] by (71.8m points)

java - Include icon in Self-Contained JavaFX application

I've been fighting with this for over a day and have read many posts on SO and other places, but I'm still having problems.

I need to include my application icon in a self-contained JavaFX application package. I'm using JDK 1.8.0_45 and its included JavaFX package. I'm using Maven to build the .exe and it all works great except I can't get my icon included.

Here is my 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>

    <prerequisites>
        <maven>2.2.1</maven>
    </prerequisites>

    <groupId>com.mycompany.drm</groupId>
    <artifactId>DRMDashboard</artifactId>
    <version>2.0</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <javafx.version>8.0.45</javafx.version>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <!-- copy all dependencies of your app to target folder-->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.10</version>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <configuration>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>false</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                        </configuration>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <archive>
                        <manifestEntries>
                            <JavaFX-Version>${javafx.version}+</JavaFX-Version>
                            <Main-Class>com.mycompany.client.HelloWorld</Main-Class>
                            <implementation-version>2.0</implementation-version>
                            <JavaFX-Application-Class>com.mycompany.client.HelloWorld</JavaFX-Application-Class>
                            <JavaFX-Class-Path>
                                <!-- list all your dependencies here-->

                            </JavaFX-Class-Path>
                            <!-- The artifactId (name) of the jfxrt.jar ... see dependency system scope-->
                            <Class-Path>
                                javafx-${javafx.version}.jar
                            </Class-Path>
                        </manifestEntries>
                        <manifest>
                            <addClasspath>true</addClasspath>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.6</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <configuration>
                            <target>

                                <path id="mypath">
                                    <pathelement path="${maven.plugin.classpath}"/>
                                    <fileset dir="${project.basedir}">
                                        <include name="package/windows/DRMDashboard.ico"/>
                                    </fileset>
                                </path>

                                <!-- define the deploy ANT task-->
                                <taskdef name="jfxdeploy" classname="com.sun.javafx.tools.ant.DeployFXTask"
                                         classpathref="mypath" />
                                <!-- define the JarSign ANT task-->
                                <taskdef name="jfxsignjar" classname="com.sun.javafx.tools.ant.FXSignJarTask"
                                         classpathref="maven.plugin.classpath" />
                                <jfxdeploy outdir="${project.build.directory}/deploy"
                                           outfile="DRMDashboard"
                                           nativeBundles="exe"
                                           verbose="true">
                                    <info title="DRM Dashboard" vendor="My Company, Inc."/>
                                    <application name="DRMDashboard" mainClass="com.mycompany.client.HelloWorld" version="2.0" />
                                    <resources>
                                        <fileset dir="${project.build.directory}" includes="*.jar" />
                                                 <!--includes="*.jar" />-->
                                    </resources>
                                    <!-- set your jvm args-->
                                    <platform javafx="${javafx.version}+">
                                        <jvmarg value="-Xms512m" />
                                        <jvmarg value="-Xmx1024m" />
                                    </platform>
                                    <preferences install="false" menu="true" shortcut="true"/>
                                </jfxdeploy>
                                <!-- you need to generate a key yourself -->
                                <jfxsignjar destdir="${project.build.directory}/deploy"
                                            keyStore="c:/Users/me/DRMDashboard.ks" storePass="****" alias="DRMDashboard"
                                            keyPass="****">
                                    <fileset dir="${project.build.directory}/deploy"
                                             includes="*.jar" />
                                </jfxsignjar>
                            </target>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>com.oracle</groupId>
                        <artifactId>ant-javafx</artifactId>
                        <version>${javafx.version}</version>
                        <systemPath>${java.home}/../lib/ant-javafx.jar</systemPath>
                        <scope>system</scope>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
        <finalName>DRMDashboard</finalName>
    </build>
</project>

EDIT 1: Here's a screenshot of my file explorer: enter image description here

EDIT 2: Here's a screenshot of the expanded target folder: enter image description here

When I execute the build with the verbose flag, I get these messages:

main:
No base JDK. Package will use system JRE.
  Using default package resource [application icon]  (add package/windows/DRMDashboard.ico to the class path to customize)
Icon File Name: C:UsersjernstAppDataLocalTempfxbundler8622978628378929412windowsDRMDashboard.ico
Executable File Name: C:UsersjernstAppDataLocalTempfxbundler8622978628378929412imageswin-exe.imageDRMDashboardDRMDashboard.exe
  Config files are saved to C:UsersjernstAppDataLocalTempfxbundler8622978628378929412windows. Use them to customize package.
  Using default package resource [Inno Setup project file]  (add package/windows/DRMDashboard.iss to the class path to customize)
  Using default package resource [setup dialog icon]  (add package/windows/DRMDashboard-setup-icon.bmp to the class path to customize)
Using default package resource [script to run after application image is populated]  (add package/windows/DRMDashboard-post-image.wsf to the class path to customize)

I've tried using the "Drop In Resources" as described in the Oracle Documentation, but no matter what I try to get the classpath right, it won't recognize my custom icon

EDIT 3: I used NwDx's suggested approach of using the javapackager instead of the ant task and I've gotten very close to what I need. Now my only problem is that the dialog box icon still uses the generic java coffee cup: enter image description here

Here's my current POM:

<?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>

    <prerequisites>
        <maven>2.2.1</maven>
    </prerequisites>

    <groupId>com.autoap.drm</groupId>
    <artifactId>native_drm</artifactId>
    <version>2.0</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <javafx.version>8.0.45</javafx.version>
        <mainClass>com.autoap.client.DRMDashboard</mainClass>
        <application.title>DRMDashboard</application.title>
        <organization.name>AutoAp, Inc.</organization.name>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</art

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Prerequirements / Assumptions

  • You're on Windows (7, 8, 8.1)
  • You have a JDK installed at least in version 1.8.0 (javafx included)
  • You've set the JAVA_HOME environment variable pointing to the top directory of your JDK (ex. C:Program FilesJavajdk1.8.0_45)
  • You have Inno Setup at least in version 5.5.5 installed (prefered the unicode version)
  • You already have a icon file (256 x 256px), prefered a multisize one. I recommend to visit this site: http://icoconvert.com/
  • You already have a bmp file (48 x 48 px) for the setup installer as setup icon

Solution

Project structure

First you need to setup the Project in a valid structure, like this: Your package folder have to be in the project root folder and not in any subfolder like src or resources.

ProjectStructur

pom.xml

There are some more properties needed for doing the correct deploy. As you can see in the antrun plugin section, you need to reassign the properties for your ant environment before you can call the build file. The properties are automatically set to the called build file. Normaly Intellij Idea will create the pom.xml for you in the project root dir.

<?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>com.autoap</groupId>
    <artifactId>HelloWorld</artifactId>
    <version>2.0</version>
    <packaging>jar</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <mainClass>com.autoap.client.HelloWorld</mainClass>
        <application.title>${project.artifactId}</application.title>
        <copyright>Han Solo</copyright>
    </properties>

    <organization>
        <name>Star Wars</name>
    </organization>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.6</version>
                <executions>
                    <execution>
                        <id>unpack-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>unpack-dependencies</goal>
                        </goals>
                        <configuration>
                            <excludeScope>system</excludeScope>
                            <excludeGroupIds>junit,org.mockito,org.hamcrest</excludeGroupIds>
                            <outputDirectory>${project.build.directory}/classes</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <executions>
                    <execution>
                        <id>default-cli</id>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                        <configuration>
                            <executable>${java.home}/bin/java</executable>
                            <commandlineArgs>-jar '${project.build.directory}/dist/${project.build.finalName}-${project.version}.jar'
                            </commandlineArgs>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

            <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.8</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <configuration>
                            <target>
                                <property name="compile_classpath" refid="maven.compile.classpath"/>
                                <property name="outputDir" value="${project.build.outputDirectory}"/>
                                <property name="sourceDir" value="${project.build.sourceDirectory}"/>
                                <property name="distDir" value="${project.build.outputDirectory}/../dist"/>
                                <property name="javaHome" value="${java.home}"/>
                                <property name="versionNo" value="${project.version}"/>
                                <property name="mainClass" value="${mainClass}" />
                                <property name="appName" value="${application.title}"/>
                                <property name="appTitle" value="${application.title}"/>
                                <property name="appVendor" value="${project.organization.name}"/>
                                <property name="appCopyright" value="${copyright}"/>
                                <property name="appMenuGroup" value="${project.organization.name}"/>
                                <ant antfile="${basedir}/build.xml" target="default"/>
                            </target>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

build.xml

I've tried to make it loosley coupled, so there is normaly no need to change anything in that file. Only if you want to have signing or special behaviour etc.The build.xml file should be saved in the project root dir.

<?xml version="1.0" encoding="UTF-8" ?>

<project name="App" default="default" basedir="."
         xmlns:fx="javafx:com.sun.javafx.tools.ant">


    <target name="default" depends="clean,compile">

        <!-- defines the classpath -->
        <path id="cp">
            <filelist>
                <file name="${javaHome}/../lib/ant-javafx.jar"/>
                <file name="${basedir}" />
            </filelist>
        </path>

        <!-- defines the task with a reference to classpath -->
        <taskdef resource="com/sun/javafx/tools/ant/antlib.xml"
                 uri="javafx:com.sun.javafx.tools.ant"
                 classpathref="cp"/>


        <fx:application id="appId"
                        name="${appName}"
                        mainClass="${mainClass}"
                        version="${versionNo}"/>


        <!-- Defines the resources needed by the application -->
        <fx:resources id="appRes">
            <fx:fileset dir="${distDir}" includes="${appName}-${versionNo}.jar"/>
        </fx:resources>

        <!-- Create a jar file -->
        <fx:jar destfile="${distDir}/${appName}-${versionNo}.jar">
            <fx:application refid="appId"/>
            <fx:resources refid="appRes"/>
            <fileset dir="${outputDir}"/>
        </fx:jar>

        <fx:deploy width="300" height="250"
                   outdir="${distDir}" embedJNLP="true"
                   outfile="${appName}-${versionNo}"
                   nativebundles="exe" verbose="true">

            <!-- define for ex. min javafx version -->
            <!-- <fx:platform /> -->

            <!-- defines the application and setup preferences -->
            <fx:preferences shortcut="true" install="true" menu="true"/>

            <!-- defines the application parts -->
            <fx:application refId="appId"/>

            <!-- defines the needed resources -->
            <fx:resources refid="appRes"/>

            <!-- defines the application info details -->
            <fx:info title="${appTitle}"
                     vendor="${appVendor}"
                     copyright="${appCopyright}"/>

            <!-- Some bundle arguments only for special platforms -->
            <fx:bundleArgument arg="win.menuGroup" value="${appMenuGroup}"/>
        </fx:deploy>

    </target>

    <!-- Removes the folders of previous runs -->
    <target name="clean">
        <mkdir dir="${outputDir}"/>
        <mkdir dir="${distDir}"/>

        <delete>
            <fileset dir="${outputDir}" includes="**/*"/>
            <fileset dir="${distDir}" includes="**/*"/>
        </delete>
    </target>

    <!-- Compiles the sources -->
    <target name="compile" depends="clean">
        <javac includeantruntime="false"
               srcdir="${sourceDir}"
               destdir="${outputDir}"
               fork="yes"
               executable="${javaHome}/../bin/javac"
               source="1.8"
               debug="on">
        </javac>
    </target>

</project>

Images in package folder

The images in your package folder need to be renamed. The icon file need to be exactly (case-sensitive) named as the property application.title in your maven pom. The second file is the setup icon, it need the exact application title as first part and -setup-icon.bmp the last part. It needs to be a bmp. Sizes mentioned above.

IconNaming

My images looks like that:

IconFiles

Run configuration

The only thing you now need is to run the scripts to deploy it. For this you need a special run configuration like showing in the next screen:

RunConfig

App

After you have configured the run, run


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...