作者:卢健波_785 | 来源:互联网 | 2022-12-14 13:07
我有一个使用资源的应用程序(.wav .png- files).我想在构建项目时将这些资源包含在我的.jar中.
从IDE运行应用程序运行良好.
如上所述这里,我创造了resources-folder
在同级别我src-folder
我的项目结构:
ProjectName --
|
out
|
resources --
| |
| sounds
| |
| images
src --
|
...
如上所述这里,我已经标记文件夹resources
为Resources root
.
如上所述这里,我创造了我自己的假象Project structure
.这是他们的样子:
这段代码,适用于音频文件,例如:
new Media(new File("resources/sounds/login_sound.wav").toURI().toString());
当我尝试通过终端运行jar时,Iam收到以下错误:
Caused by: MediaException: MEDIA_UNAVAILABLE : /home/moritz/IdeaProjects/TheFloorIsLavaGUI/out/artifacts/TheFloorIsLavaGUI_jar/resources/sounds/login_sound.wav (File or directory not found)
完整的堆栈跟踪:
moritz@Momix:~/IdeaProjects/TheFloorIsLavaGUI/out/artifacts/TheFloorIsLavaGUI_jar$ java -jar TheFloorIsLavaGUI.jar
Gtk-Message: 12:33:12.649: Failed to load module "canberra-gtk-module"
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: MediaException: MEDIA_UNAVAILABLE : /home/moritz/IdeaProjects/TheFloorIsLavaGUI/out/artifacts/TheFloorIsLavaGUI_jar/resources/sounds/login_sound.wav (Datei oder Verzeichnis nicht gefunden)
at javafx.scene.media.Media.(Media.java:407)
at managers.SoundManager.playSong(SoundManager.java:71)
at main.TheFloorIsLavaGUI.start(TheFloorIsLavaGUI.java:39)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication.lambda$null$48(GtkApplication.java:139)
... 1 more
Exception running application main.TheFloorIsLavaGUI
有什么建议吗?谢谢
1> wwerner..:
TL; DR:
从类路径而不是从文件系统加载文件:
new Media(
Test.class.getResource(
"resources/sounds/login_sound.wav"
).toURI().toString())`
说明:这里有几件事情:
您正在尝试从路径加载文件/your/home/.../project/out/artifacts/.../resources/...
.通过标记resources
为资源文件夹,您可以告诉IntelliJ将所有文件放在类路径的根目录中,如屏幕截图所示.
它不会将该文件放在文件系统中,而是将其包含在jar中
从IDE运行时,new File("resources/sounds/login_sound.wav").toURI()
评估/home/moritz/IdeaProjects/TheFloorIsLavaGUI/resources/sounds/login_sound.wav
项目中文件的位置.从jar运行时,它将计算输出文件夹中的路径,该路径/home/moritz/IdeaProjects/TheFloorIsLavaGUI/out/artifacts/TheFloorIsLavaGUI_jar/resources/sounds/login_sound.wav
不是构建所放置的位置.
现在,处理此问题的正确方法是不是从文件系统加载资源,而是从类路径加载资源.尝试从IDE和jar中运行以下命令以查看差异
System.out.println(new File("foo.wav").toURI().toString());
System.out.println( Test.class.getResource("foo.wav").toURI().toString());
// From IDE
file:/Users/.../untitled/foo.wav
file:/ Users/.../untitled/out/production/untitled/foo.wav
// From Jar
file:/Users/.../untitled/out/artifacts/foo.wav
jar:file: /Users/.../untitled/out/artifacts/untitled_jar/untitled.jar!/foo.wav
请注意,Media
接受jar URI:https: //docs.oracle.com/javafx/2/api/javafx/scene/media/Media.html#Media%28java.lang.String%29
所以你需要做的事实上很简单.更改加载wav new Media(new File("resources/sounds/login_sound.wav").toURI().toString());
到new Media(Test.class.getResource("resources/sounds/login_sound.wav").toURI().toString())
除此之外:您可能希望查看构建工具(如gradle或maven)而不是从IDE构建.