当 运行 来自 jar 时,Class 和 Classloader 的 getResource 为空
getResource of Class and Classloader null when running from jar
阅读了一堆关于资源系统的帖子后,我仍然运行对这个感到困惑。
场景:
我目前正在开发一个 javafx 应用程序,它应该 运行 作为 jar 和 Applet。
我正在使用简单的技巧 JavaFX into Swing Applications 将我的应用程序包装到 fxpanel 中。
两个版本在 Eclipse 中都可以正常工作。
但是现在,一旦调用第一个 fx 控制器,jar 和 Applet 运行 都会遇到同样的问题。
我的包结构:
/*
1.
2.
3.
controller.
EntryPoint.class <-- Main
MenuController.class
TabsController.class
ui.
pane.
MainMenu.fxml
Startuppane.fxml
Tabs.fxml
note.
NoteSearch.fxml
NoteView.fxml
*/
//编辑:我解压的 jar 中的树命令:Pastebin //编辑结束
我的应用程序执行以下操作:
首先,它从 EntryPoint.class
加载 RootLayout
MainBorder = ROOTLOADER.load(EntryPoint.class.getResourceAsStream("/1/2/3/ui/pane/StartupPane.fxml"));
然后是两个子项、一个菜单和一些选项卡。
菜单工作正常,因为 MenuController 本身不加载任何其他子项。
现在真正的问题出现在调用 TabsController 并尝试加载它自己的子项目时,例如 NoteView。
TP = TABSLOADER.load(EntryPoint.class.getResourceAsStream("/1/2/3/ui/pane/Tabs.fxml")); //Loading The Tabs.
所以 TabPane 被加载,当 TabsController 现在尝试加载他自己的 Childitems 时。我总是在导出的 jar 或 webapplet 中得到一个 NullPointer。
我尝试使用以下方法加载 ChildItems:
NOTE_SEARCH_LOADER.load(TabsController.class.getResourceAsStream("/1/2/3/ui/note/NoteSearch.fxml"));
在 Eclipse 中工作正常,但一旦导出就会导致未知路径/空指针
NOTE_SEARCH_LOADER.load(TabsController.class.getClassLoader().getResourceAsStream("1/2/3/ui/note/NoteSearch.fxml"));
改为使用 ClassLoader,删除第一个斜杠。在 Eclipse 中工作,一旦导出导致未知路径/NullPointer
NOTE_SEARCH_LOADER.load(EntryPoint.class.getResourceAsStream("/1/2/3/ui/note/NoteSearch.fxml"));
使用带有绝对路径的 EntryPoint.class(主要)。在 Eclipse 中工作,一旦导出。导出时未知路径/空指针。
NOTE_SEARCH_LOADER.load(EntryPoint.class.getClassLoader().getResourceAsStream("1/2/3/ui/note/NoteSearch.fxml"));
使用我的 Main Class 中的 ClassLoader,删除了第一个 Slash。在 Eclipse 中工作,导出时未知路径/空指针。
//edit3:我做了另一个测试,将代码从 TabController 复制到 EntryPoint。代码的 EntryPoint 版本即使在导出到 Jar 或 Applet 后也能正常工作。
估计有关系,Controller是怎么调用的?
//编辑结束
我挠头,想知道我还能尝试什么。
//edit2:当我解压我的 fxml 时,可以在给定的位置找到导致 FXML 的 NullPointer。
//编辑结束
也许我又漏掉了一些非常明显的东西。
辛克莱
Fabian95qw
已访问线程列表:
Get a resource using getResource()
Preferred way of loading resources in Java
URL to load resources from the classpath in Java
How to reference javafx fxml files in resource folder?
How do I use Java getResource() to get a resource from a parent directory?
经过几个小时的尝试,我只是选择了简单的方法,只需将 FXML 放入与需要特定 fxml 的 class 相同的文件夹中。
所以我的结构现在看起来像这样:Pastebin
我现在再次访问资源文件。
NOTE_SEARCH_LOADER.load(TabsController.class.getResourceAsStream("NoteSearch.fxml"));
现在导出的 Jar 和 Web Applet 版本都可以使用了。
此致Fabian95qw
阅读了一堆关于资源系统的帖子后,我仍然运行对这个感到困惑。
场景:
我目前正在开发一个 javafx 应用程序,它应该 运行 作为 jar 和 Applet。 我正在使用简单的技巧 JavaFX into Swing Applications 将我的应用程序包装到 fxpanel 中。
两个版本在 Eclipse 中都可以正常工作。
但是现在,一旦调用第一个 fx 控制器,jar 和 Applet 运行 都会遇到同样的问题。
我的包结构:
/*
1.
2.
3.
controller.
EntryPoint.class <-- Main
MenuController.class
TabsController.class
ui.
pane.
MainMenu.fxml
Startuppane.fxml
Tabs.fxml
note.
NoteSearch.fxml
NoteView.fxml
*/
//编辑:我解压的 jar 中的树命令:Pastebin //编辑结束
我的应用程序执行以下操作:
首先,它从 EntryPoint.class
加载 RootLayoutMainBorder = ROOTLOADER.load(EntryPoint.class.getResourceAsStream("/1/2/3/ui/pane/StartupPane.fxml"));
然后是两个子项、一个菜单和一些选项卡。
菜单工作正常,因为 MenuController 本身不加载任何其他子项。
现在真正的问题出现在调用 TabsController 并尝试加载它自己的子项目时,例如 NoteView。
TP = TABSLOADER.load(EntryPoint.class.getResourceAsStream("/1/2/3/ui/pane/Tabs.fxml")); //Loading The Tabs.
所以 TabPane 被加载,当 TabsController 现在尝试加载他自己的 Childitems 时。我总是在导出的 jar 或 webapplet 中得到一个 NullPointer。
我尝试使用以下方法加载 ChildItems:
NOTE_SEARCH_LOADER.load(TabsController.class.getResourceAsStream("/1/2/3/ui/note/NoteSearch.fxml"));
在 Eclipse 中工作正常,但一旦导出就会导致未知路径/空指针
NOTE_SEARCH_LOADER.load(TabsController.class.getClassLoader().getResourceAsStream("1/2/3/ui/note/NoteSearch.fxml"));
改为使用 ClassLoader,删除第一个斜杠。在 Eclipse 中工作,一旦导出导致未知路径/NullPointer
NOTE_SEARCH_LOADER.load(EntryPoint.class.getResourceAsStream("/1/2/3/ui/note/NoteSearch.fxml"));
使用带有绝对路径的 EntryPoint.class(主要)。在 Eclipse 中工作,一旦导出。导出时未知路径/空指针。
NOTE_SEARCH_LOADER.load(EntryPoint.class.getClassLoader().getResourceAsStream("1/2/3/ui/note/NoteSearch.fxml"));
使用我的 Main Class 中的 ClassLoader,删除了第一个 Slash。在 Eclipse 中工作,导出时未知路径/空指针。
//edit3:我做了另一个测试,将代码从 TabController 复制到 EntryPoint。代码的 EntryPoint 版本即使在导出到 Jar 或 Applet 后也能正常工作。
估计有关系,Controller是怎么调用的?
//编辑结束
我挠头,想知道我还能尝试什么。
//edit2:当我解压我的 fxml 时,可以在给定的位置找到导致 FXML 的 NullPointer。
//编辑结束
也许我又漏掉了一些非常明显的东西。
辛克莱
Fabian95qw
已访问线程列表:
Get a resource using getResource()
Preferred way of loading resources in Java
URL to load resources from the classpath in Java
How to reference javafx fxml files in resource folder?
How do I use Java getResource() to get a resource from a parent directory?
经过几个小时的尝试,我只是选择了简单的方法,只需将 FXML 放入与需要特定 fxml 的 class 相同的文件夹中。
所以我的结构现在看起来像这样:Pastebin
我现在再次访问资源文件。
NOTE_SEARCH_LOADER.load(TabsController.class.getResourceAsStream("NoteSearch.fxml"));
现在导出的 Jar 和 Web Applet 版本都可以使用了。
此致Fabian95qw