0

I have a problem with my JavaFX files. They work perfectly well inside my IDE, but when I build myself a .jar file and try to run it outside of the environment, that's where problems begin...

  1. Double clicking on the file itself does nothing. No communication, no signal, not even command line popping for a slit second. Nothing.

  2. When I try to run it from command line, I get this message:

Error: Could not find or load main class com.javafx.main.Main

Caused by: java.lang.NoClassDefFoundError: javafx/application/Application

Here is the mentioned "main.Main" class:

package com.javafx.main;

import com.javafx.controllers.OptionsController;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class Main extends Application {

    private double x, y;

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception{
        FXMLLoader loader = new FXMLLoader(getClass().getResource("/com/javafx/screens/rootScreen.fxml"));
        StackPane rootStackPane = loader.load();
        Scene scene = new Scene(rootStackPane);
        primaryStage.setScene(scene);

        primaryStage.initStyle(StageStyle.UNDECORATED);
        primaryStage.getIcons().add(new Image(getClass().getResourceAsStream("/com/javafx/images/guessingGameIcon.png")));

        rootStackPane.setOnMousePressed(mouseEvent -> {
            x = mouseEvent.getSceneX();
            y = mouseEvent.getSceneY();
        });
        rootStackPane.setOnMouseDragged(mouseEvent -> {
            primaryStage.setX(mouseEvent.getScreenX() - x);
            primaryStage.setY(mouseEvent.getScreenY() - y);
        });

        primaryStage.show();
        OptionsController.backgroundMusic();
    }
}
  1. I decided to try and follow this website https://openjfx.io/openjfx-docs/#install-javafx and run the Main.java itself. This is what I received from the command line:
Main.java:3: error: package com.javafx.controllers does not exist
import com.javafx.controllers.OptionsController;
                             ^
Main.java:40: error: cannot find symbol
        OptionsController.backgroundMusic();
        ^
  symbol:   variable OptionsController
  location: class Main
2 errors

I used this command to get the aboved mentioned error:

javac --module-path C:\ ...\javafx-sdk-11.0.2\lib --add-modules javafx.controls,javafx.fxml,javafx.graphics,javafx.media Main.java

I don't know why I have such problems running FX application from outside my environment. And why the same app is working completely fine inside it! If there is someone who could explain the situation, I would be greatfull.

Community
  • 1
  • 1
  • Try to create your jar file as mentioned here: https://stackoverflow.com/a/4901370/12609572 – ardenit Jan 14 '20 at 19:50
  • Thanks, I guess there is progress.....? Now when I try to run it with command line I get different error message. This: Error: JavaFX runtime components are missing, and are required to run this application And yes I have my VM set up properly. I don't get this error from the environment. – Stanley Lapinski Jan 14 '20 at 20:25

1 Answers1

0

You have to define your main class in the classpath such as when you try to run the jar the system knows which is your starting point (the class contains the main function).

Next you will have to bind your dependencies to the your jar. Dependencies are the other jars (the libraries you could say) that you application depends on.

The most common way of achieving both is by the usage of Maven .

Maven handles the dependencies and their version as well as the build of you jar.

Maven needs a pom.xml file in order to manage everything for you. The template I use to create jar files with dependencies with maven is :

<?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>your-groupId</groupId>
    <artifactId>Your-artifact</artifactId>
    <version>0.1</version>
    <packaging>jar</packaging>

    <!--This is how you pass dependencies -->
    <dependencies>

        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.39</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.5</version>
        </dependency>
    </dependencies>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>

                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.1.0</version>

                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>your-path-of-the-main-class</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>3.0.0</version>
                <executions>
                    <execution>
                        <id>attach-javadoc</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>3.0.1</version>
                <executions>
                    <execution>
                        <id>attach-source</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>

    </build>
</project>