Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX packaging: NoClassDefFoundError

Tags:

I package a JavaFX application using maven and OpenJDK 1.8 The relevant part from my pom.xml:

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <mainClass>ui.Main</mainClass>
            </manifest>
        </archive>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
    </configuration>
</plugin>

Now importantly this used to work. I know this because I specifically made a git commit where the packaging finally worked. After a day or so and multiple builds like this (I can't figure out what changed), I can no longer launch the jar because of the following error:

$ java -jar target/app.jar
Error: Could not find or load main class ui.Main Caused by:
java.lang.NoClassDefFoundError: javafx/application/Application

When I view the contents of the Jar, I can see that all dependencies are specifically included, except the JavaFX classes.

What I've tried:

  • Build the jar with javapackager
  • Tinker around with the javafx-maven-plugin
  • Explicitly add JavaFX as dependency in my pom.xml in the hopes that the assembly-plugin would package it for me

None of the above was successful. The only way I'm able to launch my app is directly out of the IDE, which seems to mean that the JavaFX libs are still available on my system.

Any ideas on how to get this to run (again)?

like image 921
tannerli Avatar asked Oct 11 '18 13:10

tannerli


1 Answers

Short answer

I ran my jar with openJDK 11 which does not include JavaFX while building it with openJDK 8. Case closed.

Findings, details and reasons

Since my IDE was able to run my code without issues, I presumed the problem to be specific to my config/machine. As it turns out Canonical/Ubuntu decided

[For] Bionic [...] move the default JRE/JDK in main to OpenJDK 11 in September/October 2018 as an SRU. AskUbuntu

And, as pointed out on a different SO Thread:

JavaFX 11 is not part of the JDK anymore StackOverflow

(This also includes instructions how to fix this with JDK 11)

And finally, as Murphy's law had it, I set up my packaging the project on October 4th, and decided to install eclipse via apt on October 8th which includes openjdk-11-jdk:amd64 (this is logged in the useful /var/log/apt/history.log).

This also updated my /etc/alternatives/java to point to v11. Since I ran my jar without explicitly specifying the java binary, it broke.

Running

/usr/lib/jvm/java-8-openjdk-amd64/bin/java -jar target/app.jar

for a quick test works as expected. Now I only need to upgrade my project to openJDK 11 or else always run my app with the specific JDK 8.

like image 135
tannerli Avatar answered Oct 11 '22 17:10

tannerli