0

Does anyone have any up-to-date steps on how to build a complete JAR file which includes JavaFX (hopefully using IntelliJ IDEA and Maven)? I feel like I'm going around in circles trying to piece together bits of information from here, there and everywhere, but I'm yet to create a working JavaFX JAR file.

From my pom.xml I've tried this (com.whatever.App is where my main method is, of course):

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.4</version>
    <configuration>
        <archive>
            <!-- It makes no difference which of these two manifest configs I try -->
            <manifestFile>src/main/resources/META-INF/MANIFEST.MF</manifestFile>
            <manifest>
                <addClasspath>true</addClasspath>
                <mainClass>com.whatever.App</mainClass>
            </manifest>
        </archive>
    </configuration>
</plugin>

I can run the "javafx:run" configuration from the IntelliJ IDEA Maven tool window and the program runs just fine, but not "jar:jar". It results in a 345KB JAR file (which looks suspiciously small!) that ends up with this error when I try "java -jar whatever.jar":

Error: Could not find or load main class com.whatever.App
Caused by: java.lang.NoClassDefFoundError: javafx/application/Application

My src/main/resources/META-INF/MANIFEST.MF file has the following:

Manifest-Version: 1.0
Main-Class: com.whatever.App

I also tried the shade plugin but it complains thusly:

Failed to execute goal org.apache.maven.plugins:maven-shade-plugin:3.2.4:shade (default-cli) on project whatever: Failed to create shaded artifact, project main artifact does not exist.

Here's the relevant shade config from my pom.xml:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.2.4</version>
    <configuration>
        <minimizeJar>true</minimizeJar>
    </configuration>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
        </execution>
    </executions>
</plugin>

If I run "mvn package" myself (after adding the shade config above) from the command-line using the latest Maven downloaded myself (because running the goals myself from the Maven tool window in IDEA seems to be not doing everything I would hope), it seems to go through a lot of motions successfully and produces an 11,996KB JAR file (which is bigger, at least), but running it results in this error:

Error: JavaFX runtime components are missing, and are required to run this application
Marc Fearby
  • 1,305
  • 1
  • 13
  • 28
  • try this plugin configuration https://stackoverflow.com/a/60028374/9458852 – amarildo.xyz Jan 09 '21 at 07:17
  • Thanks, but I get the same "JavaFX runtime components are missing..." error. The transformer config looks like it's only doing the same as "Main-Class: com.whatever.App" that's already in my src/main/resources/META-INF/MANIFEST.MF file. The problem (with my shade plugin attempt) seems to be that the JavaFX stuff isn't being bundled into the JAR. If I try "jar:jar", I get a very tiny jar that complains about not finding the main class, but at this stage I seem to be getting further with the shade method. I'd take anything that ultimately works, though. – Marc Fearby Jan 09 '21 at 10:25
  • A little more progress. I found this link which says I have to have a main class which does not extend Application and it will build the JAR file properly: https://github.com/javafxports/openjdk-jfx/issues/236#issuecomment-426606561 But, I'm now trying to solve problems with loading resources (which is a good thing because it seems that javafx is now working... or trying to, at least). Hopefully I'll get this working before I die :-) – Marc Fearby Jan 09 '21 at 11:33
  • It seems I have to do this to load resources from in some cases: App.class.getResourceAsStream() but in other cases this works: getClass().getResourceAsStream(). I haven't figured out why this one particular class has to use App.class instead of getClass() but changing it meant the resource loaded when running from the uber jar. Weird. – Marc Fearby Jan 09 '21 at 11:55
  • 1
    This may help with the resource problem: [How do I determine the correct path for FXML files, CSS files, Images, and other resources needed by my JavaFX Application?](https://stackoverflow.com/questions/61531317/how-do-i-determine-the-correct-path-for-fxml-files-css-files-images-and-other). – Slaw Jan 09 '21 at 11:55
  • @slaw: Yes, Thanks. That was going to be my next post. Although I found that same answer here: https://github.com/javafxports/openjdk-jfx/issues/236 – Marc Fearby Jan 09 '21 at 11:56

0 Answers0