1

So I know similar questions have been asked multiple times, but I've tried about everything I found, yet I just can't seem to figure this out. Im trying to make this simple JavaFX sample work: https://openjfx.io/openjfx-docs/#install-javafx
I'm using IntelliJ and Gradle, I followed the instructions but whenever I try to run the program in IntelliJ it gives me the "Location is required" error on my FXMLLoader.

From what i gathered, it should work by putting the scene.fxml file in the resource directory, but that doesn't seem to work for me. I've also tried putting the scene.fxml in the same package as MainApp, still didnt work.

EDIT I just noticed the Gradle build output seems to work fine. It's only a problem when i try to run my code inside IntelliJ.

So I tried another JavaFX Project without Gradle. In that case, IntelliJ creates an "out" directory which is structured the same way as my project. When using Gradle, a "build" directory is created which does contain the fxml, but not at the location I'm expecting?

This is my Main Class:

package org.openjfx;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class MainApp extends Application {

   @Override
   public void start(Stage primaryStage) throws Exception {

       Parent root = FXMLLoader.load(getClass().getResource("/fxml/scene.fxml"));

       Scene scene = new Scene(root);

       primaryStage.setTitle("JavaFX Test");
       primaryStage.setScene(scene);
       primaryStage.show();
   }

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

The build.gradle:

plugins {
    id 'application'
    id 'org.openjfx.javafxplugin' version '0.0.8'
    id 'org.beryx.jlink' version '2.15.1'
}

version '1.0-SNAPSHOT'

sourceCompatibility = 1.12

repositories {
    mavenCentral()
}

javafx {
    version = "12.0.2"
    modules = ['javafx.controls', 'javafx.fxml']
}


mainClassName = "$moduleName/org.openjfx.MainApp"

jlink{
    options = ['--strip-debug', '--compress', '2', '--no-header-files', '--no-man-pages']
    launcher {
        name = 'hellofx'
    }
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

And this is what my project structure looks like:

project structure

Netii
  • 11
  • 2
  • Please try `Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("fxml/scene.fxml"));` – Meiswjn Sep 06 '19 at 13:02
  • think _package path_ (I doubt that you have a _package_ fxml, do you? yes you seem to have, sry haven't seen the project structure before) - and then look into your build hierarchy whether the resource actually is where you expect it to be – kleopatra Sep 06 '19 at 13:15
  • 2
    @Meiswjn no random suggestions, please: your snippet would work only if the _package path_ to the resource would be org.openjfx.fxml. Instead read up on how resource paths are resolved – kleopatra Sep 06 '19 at 13:20
  • @kleopatra Could you explain that? My build directory contains a folder named "resources" which contains the .fxml. But it seems IntelliJ won't find it there? – Netii Sep 06 '19 at 14:28
  • just to clarify: with "build" I meant the output location that contains the .class files, that hierarchy must contain a package fxml which contains the scene.fxml – kleopatra Sep 06 '19 at 14:44
  • What is the JDK version ? – Ahamed Safnaj Sep 08 '19 at 13:44
  • @AhamedSafnaj JDK is 12.0.2 – Netii Sep 11 '19 at 10:43
  • Running from a terminal `./gradlew run` should work. It also works if you run from the Gradle window in IntelliJ (Tasks -> application -> run), or if you create a Gradle Run configuration with the task `run`. What you try to do, however, fails, as you are running a gradle project as a **Java application**, and IntelliJ copies the resources to the `out/production/resources` folder (as expected in a Gradle project), and not to the `out/production/classes` folder. As quick check, manually copy the `scene.fxml` file to `classes/fxml/`, and run again as Java application, now it will work. – José Pereda Sep 22 '19 at 21:43

0 Answers0