#FXML Loader not working when Eclipse project is compiled

1 messages · Page 1 of 1 (latest)

wise garden
#

I am making a project in Eclipse using Maven and JavaFX and I am trying to load an FXML file as a scene using:

FXMLLoader menuLoader = new FXMLLoader(getClass().getResource(fxmlPath));

My fxmlPath is a valid path to a fxml file in my resources folder which is under the main folder under the src folder. When running this in eclipse it works however when I compile the project to an executable .jar file and try running it I get an error:

WARNING: Unsupported JavaFX configuration: classes were loaded from 'unnamed module @14462937'
/fxml/scenes/menu.fxml
null
java.lang.IllegalStateException: Location is not set.
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2561)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2536)
at application.Main.loadScene(Main.java:148)
at application.Main.start(Main.java:189)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:839)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:483)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:456)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:400)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:455)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
at java.base/java.lang.Thread.run(Thread.java:1570)

The location is not set error happens when you try and load an fxml file which doesn't exist and through using System.out.println() I found that getClass().getResource(fxmlPath) is null when the executable jar is run, even though the fxml file exists within the project.

Let me know if anyone has any ideas.

tepid badgerBOT
#

<@&987246527741304832> please have a look, thanks.

wise garden
#

/fxml/scenes/menu.fxml

hoary sail
wise garden
#

I tried extracting it

#

and I got lots of jar files and folders all over my desktop

#

but a resource folder was there

hoary sail
#

you don't need to extract it, just look inside with an app that works with zip files

hoary sail
wise garden
#

and the items in it had the same structure as in the project

hoary sail
#

so you must have seen it wrong

hoary sail
hoary sail
#

if you rename it to .zip, windows explorer itself can also do that

wise garden
#

I'm in

hoary sail
#

show a screenshot please

wise garden
hoary sail
#

that's not normal

wise garden
#

It is compiled as an executable jar

#

as opposed to a normal one

hoary sail
#

no

#

it shouldn't have a resources folder

#

show your pom

wise garden
hoary sail
wise garden
#

nah

#

there's dependencies

#

and plugins

#

I'll get screenshot of plugins

tepid badgerBOT
hoary sail
#

How do you create a jar ?

wise garden
#

I use eclipses export feature

hoary sail
wise garden
#

you mean from the command lin

hoary sail
#

yes

#

well

#

or from run configuration

#

from eclipse

wise garden
#

how do I do it from run configuration

hoary sail
#

create run configuation

#

maven configuration

#

then select a maven command

wise garden
#

where is create run configuration

hoary sail
wise garden
#

do I click run configurations

#

or run as then maven build

hoary sail
wise garden
#

I did

#

I can't see maven configurations though

hoary sail
wise garden
hoary sail
wise garden
#

;eft you mean

hoary sail
#

ah yes

#

left

wise garden
#

So I clicked the configurations for my project but I don't know where you enter maven commands

wise garden
hoary sail
#

for example clean package

#

or javafx:run

wise garden
#

I did javafx:run

#

how do I compile it now

#

do you want me to not use export

hoary sail
hoary sail
wise garden
#

It now says unable to access the file when I try to run it frm the terminal

hoary sail
wise garden
#

I want it to be compild though

hoary sail
#

yes ?

wise garden
#

I want a compiled jar which can be used without eclipse

hoary sail
#

and ?

#

yes ?

wise garden
#

how do I get the jar form run configurations

#

I can run the program

hoary sail
wise garden
#

I'll try entering clean package instead of javafx:run

#

it compiled

#

and I ran the compiled executable jar

#

but it had an error about manifest files

#

no main manifest attribute,

hoary sail
#

then you didn't configure the jar plugin correctly

wise garden
#

how do I configure it

hoary sail
#

try do add <mainClass>application.Main</mainClass>

wise garden
#

in the pom.xml

hoary sail
#

in the jar plugin

#

well

#

I rather advise you to use another plugin

#

like

tepid badgerBOT
#

To run a maven project, you first need to add this following plugin:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>exec-maven-plugin</artifactId>
  <version>3.1.0</version>
  <goals>
    <goal>java</goal>
  </goals>
  <configuration>
    <mainClass>your.MainClass</mainClass>
  </configuration>
</plugin>

Don't forget to replace the class name by the correct one.
Then run mvn exec:java

Maven automatically creates a shallow jar (without any lib) and without a manifest by default, in target.
To create a runnable fat jar, you can use this following plugin:

<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <version>3.5.0</version>
  <configuration>
    <descriptorRefs>
      <descriptorRef>jar-with-dependencies</descriptorRef>
    </descriptorRefs>
    <archive>
      <manifest>
        <mainClass>your.MainClass</mainClass>
      </manifest>
    </archive>
  </configuration>
  <executions>
    <execution>
      <id>make-assembly</id>
      <phase>package</phase>
      <goals>
        <goal>single</goal>
      </goals>
    </execution>
  </executions>
</plugin>

Don't forget to replace the class name by the correct one.
Then run mvn package to build it, then go in target, and run java -jar MyJar-jar-with-dependencies.jar and don't forget to replace MyJar by the jar name. Don't mix with the other jar which is the shallow jar without manifest.

wise garden
#

yeah I'm not really familiar with compiling things in java

#

I'll try adding <mainClass>application.Main</mainClass>

#

I got the same error

#

I might go now and try to solve this later

#

but thanks for your help so far