Woo
Woo

Reputation: 41

Missing SwingInterOpUtils when running JavaFx in an Swing application build with Maven

I have an Swing application and I want to integrate JavaFx to it. The application runs well when starting from Eclipse.

Here is the main class:

package org.openjfx.hellofx;

import java.awt.BorderLayout;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javax.swing.JFrame;

public class AppRun {

   public static void main(String[] args) {
      JFrame f = new JFrame("TEST");
      f.setSize(400, 400);;
      f.getContentPane().setLayout(new BorderLayout());
      JFXPanel panel = new JFXPanel();
      Scene s = new Scene(new javafx.scene.control.Label("JavaFx"));
      panel.setScene(s);
      f.getContentPane().add(panel);
      f.setVisible(true);
   }
}

The necessary dependencies are definied in the Maven pom.xml:

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.openjfx</groupId>
    <artifactId>hellofx</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.release>11</maven.compiler.release>
        <javafx.version>16</javafx.version>
        <javafx.maven.plugin.version>0.0.6</javafx.maven.plugin.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>${javafx.version}</version>
        </dependency>
                <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-graphics</artifactId>
            <version>${javafx.version}</version>
            <classifier>win</classifier>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-swing</artifactId>
            <version>${javafx.version}</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>${javafx.version}</version>
        </dependency>
        <dependency>
      <groupId>org.openjfx</groupId>
      <artifactId>javafx-base</artifactId>
      <version>${javafx.version}</version>
    </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <release>${maven.compiler.release}</release>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>${javafx.maven.plugin.version}</version>
                <configuration>
                    <mainClass>org.openjfx.hellofx.App</mainClass>
                </configuration>
            </plugin>

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

But when I build and start a Fat Jar it doesn't work:

Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: jdk/swing/interop/SwingInterOpUtils
        at com.sun.javafx.embed.swing.newimpl.JFXPanelInteropN.isUngrabEvent(JFXPanelInteropN.java:40)
        at javafx.embed.swing.JFXPanel.lambda$new$6(JFXPanel.java:834)
        at java.desktop/java.awt.Toolkit$SelectiveAWTEventListener.eventDispatched(Unknown Source)
        at java.desktop/java.awt.Toolkit.notifyAWTEventListeners(Unknown Source)
        at java.desktop/java.awt.Component.dispatchEventImpl(Unknown Source)
        at java.desktop/java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.desktop/java.awt.Window.dispatchEventImpl(Unknown Source)

The missing file 'SwingInterOpUtils' is in the modul 'jdk.unsupported.dekstop'. And this is not part of the standard JRE. So is there a possibility to get the application running with an Maven build?

Upvotes: 1

Views: 1298

Answers (1)

Rafael Guillen
Rafael Guillen

Reputation: 1673

I don't know if you are creating a modular application, but I think you should consider using javafx-maven-plugin instead of maven-shade-plugin to generate a custom native image of your application. You already included the plugin, just run mvn clean javafx:jlink. Check the plugin's GitHub README for configuration details: https://github.com/openjfx/javafx-maven-plugin

Upvotes: 1

Related Questions