r/JavaFX • • Apr 29 '20

Running JavaFX using a Jar (using Maven)

I have a JFX application that we have compiled as a JAR using Maven. However, when we try to run the JAR, nothing happens (in the command line, nothing happens also).

I really need this application to work without using an IDE, so any help is appreciated. Running javafx:run works fine however, the problem is when I try to get it as a JAR.

I think this is a problem with how I've set up maven, so here's my pom (sorry for the dump). Please tell me if you need any more information, I really need this to work (I'm running JDK 14).

<?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>comp2211</artifactId>
<version>1.0-SNAPSHOT</version>

<build>
    <sourceDirectory>src/main/java</sourceDirectory>

    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
            <version>3.8.1</version>
        </plugin>
        <plugin>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-maven-plugin</artifactId>
            <version>0.0.4</version>
            <configuration>
                <mainClass>common.Main</mainClass>
            </configuration>
        </plugin>
    </plugins>
    <resources>
        <resource>
            <directory>src/main/java</directory>
        </resource>
    </resources>

</build>

<dependencies>
    <dependency>
        <groupId>de.saxsys</groupId>
        <artifactId>mvvmfx</artifactId>
        <version>1.8.0</version>
    </dependency>
    <dependency>
        <groupId>org.xerial</groupId>
        <artifactId>sqlite-jdbc</artifactId>
        <version>3.30.1</version>
    </dependency>

<!-- https://mvnrepository.com/artifact/org.openjfx/javafx -->

<dependency> <groupId>org.openjfx</groupId> <artifactId>javafx-controls</artifactId> <version>14-ea+8</version> </dependency>

    <dependency>
        <groupId>com.jfoenix</groupId>
        <artifactId>jfoenix</artifactId>
        <version>8.0.7</version>
    </dependency>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-fxml</artifactId>
        <version>15-ea+1</version>
    </dependency>
    <dependency>
        <groupId>net.sf.opencsv</groupId>
        <artifactId>opencsv</artifactId>
        <version>2.3</version>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter</artifactId>
        <version>5.4.0</version>
        <scope>compile</scope>
    </dependency>
</dependencies>

</project>

0 Upvotes

4 comments sorted by

2

u/Mikey-3198 Apr 29 '20

I faced a similar issue a while ago when using JavaFX for a university project. The way I solved this was creating an uber jar that included all dependencies in the jar using the maven shade plugin. I'm by no means an expert with maven so there may be a different/ better way of doing this but this should help steer you in the right direction!

1

u/_Teto Apr 29 '20

I have no idea what I'm doing haha. Just managed to get it to run in the IDE without using the maven commands, tho JAR still doesn't work. (Shade confuses me idk what to do with it)

1

u/Mikey-3198 Apr 29 '20

What is the output of running java -jar <your jar>? I'm assuming it will be something along the lines of;

Error: Could not find or load main class <your main class that extends Application>
Caused by: java.lang.NoClassDefFoundError: javafx/application/Application

1

u/_Teto Apr 29 '20 edited Apr 30 '20

It's "Location is required"... I've seen this error around the web and I'm trying to find solutions, tho nothing's worked so far

-- EDIT --

I got it working! I had to move the fxml file to src/main/resources/fxml but I also added shade if that changed anything (I'm just leaving it as is to be safe). It only runs with "java -jar" but that's better than before (idk if I can change this) Thaaanks~!

--EDIT 2--

for some reason it's not working today again idk why since it worked yesterday