We're writing an Java application using JavaFX. At this time we have 3 different forms:
- Login
- Game window
- Registration
For our next iteration, we want to implement the Registration form, but we get the IOException error Unknown Path
It's about this piece of code:
FXMLLoader registrationLoader = new FXMLLoader(); try{ mainroot = (Parent)registrationLoader.load(this.getClass().getResource("FXMLRegistration.fxml").openStream()); Stage registrationStage = new Stage(); Scene scene = new Scene(mainroot); registrationStage.setScene(scene); registrationStage.setTitle("Register your account"); registrationStage.show(); } catch(IOException ex) { System.out.print(ex.getMessage()); } The above code is working when I change FXMLRegistration.fxml to FXMLDocument.fxml or FXMLLoader.fxml.
When I change
mainroot = (Parent)registrationLoader.load(this.getClass().getResource("FXMLRegistration.fxml").openStream()); to
mainroot = (Parent)registrationLoader.load(Paths.get("src/hackattackfx/FXMLRegistration.fxml").toUri().toURL()); I get the absolute path in the debugger output, which is correct when I use it with the file command in terminal.
I hope someone could help us with this error.
Thanks in advance!
EDIT
I Changed some code to the following:
FXMLLoader registrationLoader = new FXMLLoader(getClass().getResource("/FXMLRegistration.fxml")); mainroot = (Parent)registrationLoader.load(); but this will return an IllegalStateException: Location is not set. When I remove / before /FXMLRegistration.fxml, I get to my catch block printing the full path of the file:
file:/Users/juleskreutzer/Documents/github/PTS3/HackAttackFX/dist/run1793658053/HackAttackFX.jar!/hackattackfx/FXMLRegistration.fxml
Also changing the path to src/hackattackfx/FXMLRegistration.fxml will give the IllegalStateException: Location not set.
Project Structure
We use different packages in our application. all these packages are within the default package: hackattackfx
The packages in the default package are:
- Default Package
- Exceptions
- Interfaces
- enums
- Resources
- Templates
- JSON Package
My FXML documents are located in the default package (hackattackfx). If it's not 100% clear how I arranged my files, please take a look at my Github repo
Paths.get("src/hackattackfx/FXMLRegistration.fxml").toUri().toURL()does it work? Is you aim to usePaths.get()? If yes, why?getClass().getResource()if you file is present on the classpath. Paths.get() doesn't make any sense under such circumstances.