1

I have a simple Java FX(11.x LTS) application developed under Linux. I want to create a jar file to be executed under win 10. For this I have the following 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>groupId</groupId>
    <artifactId>udp_dumper</artifactId>
    <version>1.0-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.3</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>Launcher</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <release>11</release>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>0.0.4</version>
                <configuration>
                    <mainClass>Launcher</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>11.0.2</version>
            <classifier>linux</classifier>
        </dependency>

        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>11.0.2</version>
            <classifier>win</classifier>
        </dependency>

        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>11.0.2</version>
            <classifier>linux</classifier>
        </dependency>

        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>11.0.2</version>
            <classifier>win</classifier>
        </dependency>

    </dependencies>
</project>

On Windows I've downloaded JavaFX SDk and put its path to PATH variable( both bin and lib folders ). I'm trying to execute it via java -jar *.jar But I get the following error :

java.lang.ClassNotFoundException: com.sun.glass.ui.win.WinPlatformFactory

What is the reason for the failure? Do I use a valid approach to deploy JavaFX app?

VGR
  • 40,506
  • 4
  • 48
  • 63
Dmitry
  • 1,912
  • 2
  • 18
  • 29
  • 1
    Check out [this answer](https://stackoverflow.com/a/52654791/6395627); it shows creating a fat jar with Maven. I believe the modules which have platform-specific code are `javafx.graphics`, `javafx.media`, and `javafx.web`. Of those that you use, you have to manually declare a dependency for each platform you want to support. So in your case I think you need to add `linux` and `win` dependencies for `javafx.graphics`. – Slaw Jun 10 '20 at 13:44
  • YEEEES, it works !!!! You'are a rock star !!!! Many thanks for your help – Dmitry Jun 10 '20 at 13:50

1 Answers1

2

As explained by this answer by @JoséPereda, you have to include each platform-specific dependency for certain JavaFX modules—more specifically, the ones which contain platform-specific code—in order to create a cross-platform fat jar. I believe those modules are javafx.graphics, javafx.media, and javafx.web. Of those that you use, you have to manually declare a dependency for each platform you want to support. In your case, I believe the following will be sufficient:

<dependencies>
  <dependency>
    <groupId>org.openjfx</groupId>
    <artifactId>javafx-graphics</artifactId>
    <version>${javafx.version}</version>
    <classifier>win</classifier>
  </dependency>

  <dependency>
    <groupId>org.openjfx</groupId>
    <artifactId></artifactId>
    <version>${javafx.version}</version>
    <classifier>linux</classifier>
  </dependency>

  <dependency>
    <groupId>org.openjfx</groupId>
    <artifactId>javafx-controls</artifactId>
    <version>${javafx.version}</version>
  </dependency>

  <dependency>
    <groupId>org.openjfx</groupId>
    <artifactId>javafx-fxml</artifactId>
    <version>${javafx.version}</version>
  </dependency>
</dependencies>
Slaw
  • 37,820
  • 8
  • 53
  • 80