jar无法找到音频文件

时间:2016-10-05 09:51:33

标签: java audio intellij-idea jar lwjgl

所以我用Java编写一个必须能够播放wav文件的程序。它在Intellij中运行时效果很好,但是当我从jar运行它时,它会在尝试检索声音文件时立即崩溃并返回此错误:

Unable to create from inputstream, Stream closed
Exception in thread "main" java.lang.NullPointerException

以下是我用来检索声音文件的代码:

protected int loadSound(String file) {

    int data = AL10.alGenBuffers(); //Create new buffer object
    buffers.add(data); //Store buffer ID in array list
    ClassLoader loader = Thread.currentThread().getContextClassLoader();
    InputStream filePath = loader.getResourceAsStream("/res/sound/" + file);
    WaveData wavFile = WaveData.create(new BufferedInputStream(filePath)); //Retrieve wav file
    AL10.alBufferData(data, wavFile.format, wavFile.data, wavFile.samplerate); //Store wav file data and properties in buffer object
    wavFile.dispose(); //Discard wav file from stored memory

    return data; //Return buffer ID
}

我无法理解为什么从jar运行时这不起作用,因为当我查看jar文件时,我可以看到res文件夹确实在其中。我仍然感到奇怪的是,我使用或多或少相同的代码来检索程序的另一部分中的文本文件,并且能够在IDE内部运行时确认这是正常的。罐。该代码如下:

ClassLoader loader = Thread.currentThread().getContextClassLoader();
InputStream mapPath = loader.getResourceAsStream("dat/maps/" + mapName);

我知道此前曾多次提出过类似的问题,但我对这些问题所找到的解决方案并没有取得任何成功。我很确定它与未正确打包的文件有关,但我不知道应该怎么做。

这是完整的堆栈跟踪:

C:\Users\Jack>java -jar C:\Users\Jack\IdeaProjects\ProjectBlackSpace\out\artifacts\ProjectBlackSpace_jar\ProjectBlackSpace.jar
OpenALC10: true
OpenALC11: true
caps.ALC_EXT_EFX = true

Num HRTF specifiers = 2: true
Sample rate: default-44100
HRTF enabled: true
HRTF state: 0x3

OpenGL version: 4.5.0 NVIDIA 368.81
[LWJGL] Failed to load a library. Possible solutions:
        a) Set -Djava.library.path or -Dorg.lwjgl.librarypath to the directory that contains the shared libraries.
        b) Add the JAR(s) containing the shared libraries to the classpath.
[LWJGL] Enable debug mode with -Dorg.lwjgl.util.Debug=true for better diagnostics.
Unable to create from inputstream, Stream closed
Exception in thread "main" java.lang.NullPointerException
        at projectBlackSpaceApp.Sounds.loadSound(Sounds.java:22)
        at projectBlackSpaceApp.Player.init(Player.java:94)
        at projectBlackSpaceApp.Game.init(Game.java:72)
        at projectBlackSpaceApp.Main.run(Main.java:159)
        at projectBlackSpaceApp.Main.init(Main.java:82)
        at projectBlackSpaceApp.Main.main(Main.java:195)
AL lib: (EE) alc_cleanup: 1 device not closed

使用-Dorg.lwjgl.util.Debug=true运行程序时从命令行运行时的结果没有什么不同,但是当从IDE中运行时,它提供了更多信息:

[LWJGL] Version: 3.0.0 build 90
[LWJGL]      OS: Windows 10 v10.0
[LWJGL]     JRE: 1.8.0_60 amd64
[LWJGL]     JVM: Java HotSpot(TM) 64-Bit Server VM v25.60-b23 by Oracle Corporation
[LWJGL] Loading library (system): lwjgl
[LWJGL]     Loaded from java.library.path: libs\lwjgl.dll
[LWJGL] MemoryUtil accessor: MemoryAccessorUnsafe
[LWJGL] Loading library: OpenAL
[LWJGL] ThreadLocalUtil state: UnsafeState
[LWJGL]     Loaded from java.library.path: libs\OpenAL.dll
OpenALC10: true
OpenALC11: true
caps.ALC_EXT_EFX = true

Num HRTF specifiers = 2: true
Sample rate: default-44100
HRTF enabled: true
HRTF state: 0x3
[LWJGL] Loading library: glfw
[LWJGL]     Loaded from java.library.path: libs\glfw.dll
[LWJGL] Loading library: opengl32
[LWJGL]     opengl32.dll not found in java.library.path=libs/
[LWJGL]     Loaded from system paths

OpenGL version: 4.5.0 NVIDIA 368.81
[LWJGL] Loading library: jemalloc
[LWJGL]     jemalloc.dll not found in java.library.path=libs/
[LWJGL]     jemalloc.dll not found in system paths
[LWJGL]     Using SharedLibraryLoader...
java.lang.RuntimeException:     Failed to extract jemalloc library
    at org.lwjgl.system.SharedLibraryLoader.load(SharedLibraryLoader.java:65)
    at org.lwjgl.system.Library.loadNative(Library.java:139)
    at org.lwjgl.system.jemalloc.JEmalloc.<clinit>(JEmalloc.java:37)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at org.lwjgl.system.MemoryManage.getInstance(MemoryManage.java:36)
    at org.lwjgl.system.MemoryUtil$LazyInit.<clinit>(MemoryUtil.java:76)
    at org.lwjgl.system.MemoryUtil.nmemAlloc(MemoryUtil.java:162)
    at org.lwjgl.system.MemoryUtil.memAlloc(MemoryUtil.java:180)
    at org.lwjgl.system.MemoryUtil.memUTF8(MemoryUtil.java:1530)
    at org.lwjgl.system.APIUtil.apiArrayi(APIUtil.java:430)
    at org.lwjgl.opengl.GL20.glShaderSource(GL20.java:425)
    at projectBlackSpaceApp.Shaders.init(Shaders.java:29)
    at projectBlackSpaceApp.Main.run(Main.java:123)
    at projectBlackSpaceApp.Main.init(Main.java:82)
    at projectBlackSpaceApp.Main.main(Main.java:195)
    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:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: java.lang.RuntimeException: Failed to locate resource: jemalloc.dll
    at org.lwjgl.system.SharedLibraryLoader.extractFile(SharedLibraryLoader.java:80)
    at org.lwjgl.system.SharedLibraryLoader.load(SharedLibraryLoader.java:63)
    ... 20 more
[LWJGL] Failed to load a library. Possible solutions:
    a) Set -Djava.library.path or -Dorg.lwjgl.librarypath to the directory that contains the shared libraries.
    b) Add the JAR(s) containing the shared libraries to the classpath.
java.lang.UnsatisfiedLinkError: Failed to locate library: jemalloc.dll
    at org.lwjgl.system.Library.loadNativeRelative(Library.java:177)
    at org.lwjgl.system.Library.loadNative(Library.java:134)
    at org.lwjgl.system.jemalloc.JEmalloc.<clinit>(JEmalloc.java:37)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at org.lwjgl.system.MemoryManage.getInstance(MemoryManage.java:36)
    at org.lwjgl.system.MemoryUtil$LazyInit.<clinit>(MemoryUtil.java:76)
    at org.lwjgl.system.MemoryUtil.nmemAlloc(MemoryUtil.java:162)
    at org.lwjgl.system.MemoryUtil.memAlloc(MemoryUtil.java:180)
    at org.lwjgl.system.MemoryUtil.memUTF8(MemoryUtil.java:1530)
    at org.lwjgl.system.APIUtil.apiArrayi(APIUtil.java:430)
    at org.lwjgl.opengl.GL20.glShaderSource(GL20.java:425)
    at projectBlackSpaceApp.Shaders.init(Shaders.java:29)
    at projectBlackSpaceApp.Main.run(Main.java:123)
    at projectBlackSpaceApp.Main.init(Main.java:82)
    at projectBlackSpaceApp.Main.main(Main.java:195)
    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:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
[LWJGL] [MemoryAllocator] Failed to load the jemalloc library.
[LWJGL] MemoryUtil allocator: StdlibAllocator

jemalloc.dll添加到库目录会阻止运行时异常的发生,但它似乎没有任何其他效果,无论好坏。

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

检查:"/res/sound/""dat/maps/"之间的区别。那是/

<强>更新

从堆栈跟踪看起来,预计会出现共享库。请按照建议做什么:

  

可能的解决方案:           a)将-Djava.library.path或-Dorg.lwjgl.librarypath设置为包含共享库的目录。           b)将包含共享库的JAR添加到类路径中。

答案 1 :(得分:0)

原来问题是双重的。首先,我没有在我的libs目录中包含一个必要的.dll文件,其次,当我应该使用“.WAV”时,我正在用扩展名“.wav”结束文件名。 (捂脸)