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.
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.
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: 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).
Location not set
FXMLLoader
throws "Location is not set
" when the input is null
.
In your case it happens because you're providing a wrong absolute path to the XML. The absolute path to your XML should be /projeto/resources/RootLayout.fxml
:
Either fix the absolute path:
new FXMLLoader(MainApp.class.getResource("/projeto/resources/RootLayout.fxml"));
Or use a relative path:
new FXMLLoader(MainApp.class.getResource("resources/RootLayout.fxml"));
(Notice the lack of leading /
)
Related Topics
How to Customize Parameter Names When Binding Spring MVC Command Objects
Rest - Http Post Multipart with JSON
How to Create an Instance of an Interface in Java
Configuring Spring Security 3.X to Have Multiple Entry Points
Use of Class Definitions Inside a Method in Java
Java Generate Random Number Between Two Given Values
How to Find Where Jdk Is Installed on My Windows MAChine
Java Webservice Client (Best Way)
Difference Between Break and Continue Statement
Spring Jdbc Template for Calling Stored Procedures
Java: Class.Isinstance VS Class.Isassignablefrom
Log4J: Log Output of a Specific Class to a Specific Appender
Jprogressbar Isn't Progressing
Singleton with Arguments in Java
How to Create 2 Separate Log Files with One Log4J Config File