Standard resources-Verzeichnis ändern

PierreDole

Mitglied
Moin,
wie kann man das Standardverzeichnis für resources ändern?

Wenn ich das Verzeichnis ausgeben lasse...
Java:
System.out.println(getClass().getResource("").getPath());

... bekomme ich folgendes Verzeichnis angezeigt:

"/G:/workspace/_java/NbaStats/target/classes/main/java/"

Sollte das resources-Verzeichnis nicht src/main/resources sein?
 
Nein, das Laden von Ressourcen läuft über den ClassLoader. Wenn Du z. B. Dein Projekt am Ende in ein Jar packst, dann ändert sich natürlich auch der Pfad, jedoch musst Du im Programm nichts ändern.
 
getResource gibt keinen Pfad zurück, sondern eine Resource-URL, die in manchen Fällen auf ein Verzeichnis zeigt und deshalb auch nur in bestimmten Fällen als Pfad benutzt werden kann.
Insbesondere gibt es auch nur dann etwas sinnvolles zurück, wenn eine gültige Resource benutzt wird – in deinem Fall ist das nicht der Fall.

In deinem Fall ist es eine Kombination aus beidem: eine ungültige Resource, die dann versucht wird als Pfad zu benutzen, das geht also gleich doppelt schief 🙂


Was genau willst du denn damit erreichen? Resourcen aus src/main/resources werden wenn das Build-Tool entsprechend konfiguriert ist gefunden.
 
Das eigentlich Ziel war:
Java:
Parent root = FXMLLoader.load(getClass().getResource("main.fxml"));

Das verursacht aber eine Exception. Unter den vielen Zeilen ist auch die Zeile mit diesem Aufruf. Und etwas drüber steht: Caused by: java.lang.NullPointerException: Location is required.

Exception in Application start method
Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.NullPointerException: Location is required.
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3230)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3194)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3163)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3136)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3113)
at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3106)
at nbaStats/main.java.Program.start(Program.java:143)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
... 1 more


Ich denke mal, er findet die Datei nicht. Habe daraufhin versucht mir das Resources-Verzeichnis anzeigen zu lassen, um zu schauen, ob ich die Datei ins richtige Verzeichnis gelegt habe.
 
Hat geklappt. Bekomme jetzt eine Exception, weil er den RootController nicht findet. Damit findet er aber die main.fxml.

Caused by: java.lang.ClassNotFoundException: NbaStats.src.main.java.gui.RootContoller

Ab welchen Verzeichnis geht es hier los?
Java:
fx:controller="NbaStats.src.main.java.gui.RootContoller"
 
Oh, ich habe mich beim RootContoller vertippt. Da fehlte das r. 🙂

Aber das Verzeichnis-Kuddelmuddel geht weiter. 😀

Java:
    <stylesheets>
        <URL value="Themes/_default/gui.css" />
      </stylesheets>

Das Verzeichnis "Themes" liegt im Projektordner. Mit der File-Klasse kann ich drauf so, wie es oben steht, zugreifen. In der FXML-Datei bekomme ich hingegen eine Exception:

Caused by: javafx.fxml.LoadException: URL is not a valid type.
 

Zurück
Oben