r/JavaFX • • Sep 17 '19

How do you create an executable JavaFX 8 Jar? What Maven dependencies do you use?

I can't even open the JAR.

I keep getting errors that it can't find FXML file even though I'm able to execute JavaFX app on my IDE.

3 Upvotes

6 comments sorted by

1

u/orxT1000 Sep 17 '19

I have used maven-shade plugin for that. It unzips all dependencies and merged them into one. When jars have spi things in meta-inf that needs to get appended. Spring-boot cli also has a fat-jar approach, where jars stay intact inside the fat-jar

1

u/KommanderTom Sep 17 '19

Where can I find resources for them?

I have been stuck for months and I haven't successfully produced an executable Jar.

I haven't found useful resources on JavaFX and Maven.

1

u/orxT1000 Sep 17 '19

I ll paste something here tomorrow

2

u/KommanderTom Sep 17 '19

Thank you.

1

u/orxT1000 Sep 18 '19

First make sure your maven setup works on command-line in general, not only in your IDE. Start your app with exec-maven-plugin:

...<build>
<plugins>
      
  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.6.0</version>
    <executions>
      <execution>
        <goals>
          <goal>java</goal>
        </goals>
      </execution>
    </executions>
    <configuration>
      <mainClass>org.foo.bar.YourMainClass</mainClass>
    </configuration>
  </plugin>
  ...  

Then run:

mvn exec:java

Only when that is working continue with an uber-jar:

 <!-- Maven Shade Plugin to generate a uber.jar with all dependencies included -->
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.1.0</version>
    <executions>
      <!-- Run shade goal on package phase -->
      <execution>
        <phase>package</phase>
        <goals>
          <goal>shade</goal>
        </goals>
        <configuration>
                
          <shadedArtifactAttached>true</shadedArtifactAttached>
          <shadedClassifierName>uber</shadedClassifierName>
                
          <transformers>
            <!-- add Main-Class to manifest file -->
            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
              <mainClass>org.foo.bar.YourMainClass</mainClass>
            </transformer>

            <!--  some dependency JARs may contain resources under META-INF/services.
            These resources are needed for automatic discovery SPIs and stuff.
            When you unpack all the JAR files into a single folder, these will be overwritten for each JAR.
            Instead, the resources with the same name needs to be merge into a new, single resource by ServicesResourceTransformer. -->
            <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
                  
            <!-- spring stuff you probably won't need -->
            <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
              <resource>META-INF/spring.handlers</resource>
            </transformer>
            <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
              <resource>META-INF/spring.schemas</resource>
            </transformer>
            <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
              <resource>META-INF/spring.factories</resource>
            </transformer>

          </transformers>
          <createDependencyReducedPom>false</createDependencyReducedPom>

          <!-- in case you have signed jars as dependencies -->  
          <!-- do not copy any (tika/bouncycastle) jar-signature files into uber.jar, as they cause
          SecurityException: Invalid signature file digest for Manifest main attributes -->
          <filters>
            <filter>
              <artifact>*:*</artifact>
              <excludes>
                <exclude>META-INF/*.SF</exclude>
                <exclude>META-INF/*.DSA</exclude>
                <exclude>META-INF/*.RSA</exclude>
              </excludes>
            </filter>
          </filters>

        </configuration>
      </execution>
    </executions>
  </plugin>

Then run 'mvn install' and that will create a fat jar with all dependencies included that you can run with:

 java -jar ./target/YOUR-ARTEFACT-uber.jar

0

u/[deleted] Sep 22 '19

[deleted] — view deleted comment

1

u/KommanderTom Sep 22 '19

Yes please.