intelliJ IDEA为JAR添加资源

时间:2018-06-08 10:34:45

标签: intellij-idea jar artifacts

我有一个使用资源的应用程序(.wav .png- files)。 我想在构建我的项目时将这些资源包含在我的.jar中。

从IDE运行应用程序效果很好。

正如here所述,我在resources-folder

的同一级别创建了src-folder

我的项目结构:

ProjectName --        
              |
              out
              |
              resources --
              |           |
              |           sounds
              |           |
              |           images
              src --
                    |
                    ...

正如here所述,我已将文件夹resources标记为Resources root

正如here所述,我在Project structure中创建了自己的工件。 这是他们的样子: enter image description here

这段代码,适用于音频文件,例如:

new Media(new File("resources/sounds/login_sound.wav").toURI().toString());

当我尝试通过终端运行jar时,我收到以下错误:

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.<init>(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 个答案:

答案 0 :(得分:7)

TL; DR:

从类路径而不是文件系统加载文件:

new Media(
  Test.class.getResource(
    "resources/sounds/login_sound.wav"
   ).toURI().toString())`

说明:这里有几件事情:

  • 您正尝试从路径/your/home/.../project/out/artifacts/.../resources/...加载文件。通过将resources标记为资源文件夹,您可以告诉IntelliJ将所有文件放在类路径的根目录中,如屏幕截图所示。

enter image description here

  • 它不会将该文件放在文件系统中,而是将其包含在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构建。