JAVAFX: Location is not set error
When you use getClass().getResource(...)
you are loading a resource, not specifying a path to a file. In the case where the class loader loads classes from the file system, these essentially equate to the same thing, and it does actually work (though even then there's no technical reason it has to). When the class loader is loading classes by other mechanisms (and probably in all cases anyway), then it's important to pay attention to the Java specifications for a resource.
In particular, note:
Resources, names, and contexts
A resource is identified by a string consisting of a sequence of
substrings, delimited by slashes (/), followed by a resource name.
Each substring must be a valid Java identifier. The resource name is of the form shortName or shortName.extension. Both shortName
and extension must be Java identifiers.
(My emphasis.) Since ..
is not a valid Java identifier, there's no guarantee of this resource being resolvable. It happens that the file system class loader resolves this in the way you expect, which is why it works in your IDE, but the implementation of getResource(...)
in the jar class loader does not implement this in the way you are hoping.
Try
FXMLLoader loader = new FXMLLoader(getClass().getResource("/sm/customer/CustomerHome.fxml"));
Using controller locations to load FXML:
Since you have organized your code so that each FXML is in the same package as its corresponding controller file (which I think is a sensible way to do things), you could also leverage this in loading the FXML: just load the FXML "relative to its controller":
FXMLLoader loader = new FXMLLoader(CustomerHomeCtrl.class.getResource("CustomerHome.fxml"));
This seems fairly natural in this setup, and the compiler will check that you have the package name for CustomerHomeCtrl
correct at the point where you import the class. It also makes it easy to refactor: for example suppose you wanted to split sm.admin
into multiple subpackages. In Eclipse you would create the subpackages, drag and drop the FXML and controllers to the appropriate subpackages, and the import statements would automatically be updated: there would be no further changes needed. In the case where the path is specified in the getResource(...)
, all those would have to be changed by hand.
java.lang.IllegalStateException: Location is not set in JavaFX application
In your Main class, your package and class name is:
tech.faraaz.zoforo.Main
In SplashScreenController it is:
tech.faraaz.zoforo.gui.SplashScreenController
So these classes are in different packages.
Yet you try to get the resource using the same relative location.
In Main:
getClass().getResource("gui/SplashScreen.fxml")
In SplashScreenController:
getClass().getResource("gui/OpenAccountScreen.fxml")
So relative to the location of the SplashController, then OpenAccountScreen.fxml would need to be in the following location for it to be found:
tech/faraaz/zoforo/gui/gui/OpenAccountScreen.fxml
I bet it's not there...
Probably, rather than accessing the FXML relative to the current class, you should access it relative to a given class (e.g. always Main) or via absolute references. That might help prevent your confusion.
For example, write:
Main.class.getResource("gui/SplashScreen.fxml");
Main.class.getResource("gui/OpenAccountScreen.fxml");
OR:
getClass().getResource("/tech/faraaz/zoforo/gui/SplashScreen.fxml")
getClass().getResource("/tech/faraaz/zoforo/gui/OpenAccountScreen.fxml")
Note, to debug stuff like this, you can always just run:
System.out.println(getClass().getResource("gui/OpenAccountScreen.fxml"));
If it prints null
, then you know the resource is not in the location you expect, then you can troubleshoot from there.
JAVAFX - Exception caused by Location is not set when loading fxml file
I see you are using maven and jetbrains. This exception does indeed come when the fxml file's location is wrong in the code. You are using maven. Maven is searching for resources (like fxml files) in it's resources folder. The maven project's root folder is where pom.xml is. I will reference it as {mavenRoot}.
So let's assume, your fxml file is in this path:
{mavenRoot}/src/main/resources/fxml/sample.fxml
Then you can use theese two lines to set up your fxml loader:
FXMLLoader loader1 = new FXMLLoader();
loader1.setLocation(getClass().getResource("/fxml/sample.fxml"));
In a maven project, the getResource() will search the {mavenRoot}/src/main/resources
folder.
JavaFX: Location is not set error in cmd
Simple change
Main.class.getResource("/application/MainWIndowView.fxml")
to
Main.class.getResource("MainWindowView.fxml")
And be careful with the case sensitivity, because you have a capital I
in MainWIndowView.fxml
but the file is named MainWindowView.fxml
.
Thanks to @James_D for pointing out:
When you run during development in Eclipse, it is loading the classes
from the file system. So if you use an operating system that treats
files the same, irrespective of the case of the file name, then it
will find the right resource. Obviously, the same is not true in a jar
file (or in any sensible operating system).
Related Topics
Add Jar Files to a Spark Job - Spark-Submit
Jackson - Deserialize Using Generic Class
What Are Classes, References, and Objects
Java Random Numbers Using a Seed
How to Count Occurrences with Groupby
Java Generating Non-Repeating Random Numbers
What Data Type to Use for Money in Java
Garbage Collection of String Literals
How to Bundle a Native Library and a Jni Library Inside a Jar
Why We Use If, Else If Instead of Multiple If Block If the Body Is a Return Statement
Java Nio Filechannel Versus Fileoutputstream Performance/Usefulness
How to Convert a String with Unicode Encoding to a String of Letters
Java Byte Array to String to Byte Array
How Does Auto Boxing/Unboxing Work in Java
Build Eclipse Java Project from Command Line
In Java, How to Write a String Literal Without Having to Escape Quotes