Search code examples
javaaudiointellij-ideajarlwjgl

jar unable to locate audio files


So I'm writing a program in Java that must be able to play wav files. It works perfectly when run from within Intellij, but when I run it from the jar it crashes immediately when it tries to retrieve the sound files and returns this error:

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

Here is the code that I'm using to retrieve the sound files:

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
}

I cannot understand why this does not work when run from the jar, since when I look inside the jar file I can see that the res folder is indeed inside of it. What's stranger still is that I'm using more or less identical code to retrieve a text file in another part of the program and have been able to confirm that this is working correctly when run inside the IDE and from the jar. That code is as follows:

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

I know similar questions have been asked here many times before, but I have not had any success with the solutions I found to those questions. I am pretty sure it's something to do with the files not being packaged correctly, but I cannot say how it should be done.

Here is the complete stack trace:

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

When running the program with -Dorg.lwjgl.util.Debug=true The results when running from the command line are no different, but when run from within the IDE it provides some more information:

[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

Adding the jemalloc.dll to the library directory does stop the runtime exception from happening, but it doesn't seem to have any other effect for better or worse.

Any help would be greatly appreciated.


Solution

  • check the difference between: "/res/sound/" and "dat/maps/". That is the /

    UPDATE

    From the stacktrace it looks like a shared library is expected to be present. Please do what is suggested:

    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.