When I run an application in the IDE, accessing the parent directory (relative to the current directory) is easily done by using ..
(two periods).
Here's a quick example:
Package Structure
src
└───main
├───java
│ │ Main.java
│ │
│ └───controllers
│ Controller.java
│ InnerController.java
│
└───resources
└───fxml
inner-layout.fxml
layout.fxml
From inside the Controller
, I try to say, "Go up one level and then go find fxml/inner-layout.fxml
".
URL relativeResource = Controller.class.getResource("../fxml/inner-layout.fxml");
// Returns: "file:/C:/Users/BradTurek/IdeaProjects/Parent%20Directory%20Jar%20MCVE/build/resources/main/fxml/inner-layout.fxml"
This works on the file system.
However, when I deploy the application as an executable .jar
file, the ..
is no longer interpreted as "go up one directory". I checked: I can still access resources in the .jar
—except if I use a path including ..
.
It seems like ..
just doesn't work when dealing with a path inside of a .jar
.
Here's a full MCVE of the above example that you can download and run yourself.
The answer to this question isn't easily found on the internet—though it may be out there. My hope is that I've posed this question clearly enough to allow it's answer to be easily found by others.
Why does this happen?