0

I have a FileInputStream in a class in the package com.nishu.ld28.utilities, and I want to access sound files in the folder Sounds, which is not in the com.nishu.ld28 package. I specify the path for loading like so:

"sounds/merry_xmas.wav"

And then try to load it like this:

new BufferedInputStream(new FileInputStream(path))

When I export the jar, the command line prompt that I run it through says it can't find the file. I know how to access the files when I am running the program in Eclipse, but I can't figure out how to point the FileInputStream to the Sounds folder when I export it.

Edit: As requested, here's my code:

public void loadSound(String path)  {
    WaveData data = null;
    data = WaveData.create(GameSound.class.getClassLoader().getResourceAsStream(path));
    int buffer = alGenBuffers();
    alBufferData(buffer, data.format, data.data, data.samplerate);
    data.dispose();
    source = alGenSources();
    alSourcei(source, AL_BUFFER, buffer);
}

WaveData accepts an InputStream or other types of IO.

  • http://stackoverflow.com/questions/18250236/java-images-not-drawn-when-running-java-jar/18250364#18250364 – Luca Basso Ricci Dec 16 '13 at 14:15
  • What am I supposed to do with that? FileInputStream doesn't accept a InputStream. –  Dec 16 '13 at 14:17
  • Is the `sounds/merry_xmas.wav` embedded in your jar ? – Stephan Dec 16 '13 at 14:20
  • Things like this have been asked many times. Please search before asking. – aalku Dec 16 '13 at 14:43
  • My question is different though, I'm using OpenAL, not just standard file IO. –  Dec 16 '13 at 14:45
  • That does not make any difference. Adding a leading slash has been suggested many times to many similar questions and you didn't try it before asking, did you? – aalku Dec 16 '13 at 14:47
  • Uh yes I did, but for some reason WaveData does not act normally after exporting. I know what I'm talking about, I've been trying to debug this for the past two days now. –  Dec 16 '13 at 14:48

2 Answers2

2

You don't need a FileInputStream, because you aren't reading from the filesystem. Use the InputStream returned by ClassLoader.getResourceAsStream(String res) or Class.getResourceAsStream(String res). So either

in = ClassLoader.getResourceAsStream("sounds/merry_xmas.wav");

or

in = getClass().getResourceAsStream("/sounds/merry_xmas.wav");

Note the leading slash in the second example.

Raffaele
  • 20,627
  • 6
  • 47
  • 86
0

I would put the com.nishu.ld28.utilities in the same package of your class , let's call it MyClass.

Your package:

enter image description here

Your code:

package com.nishu.ld28.utilities;

import java.io.InputStream;

public class MyClass {

   public static void main(String[] args) {
      InputStream is = MyClass.class.getResourceAsStream("sound/merry_xmas.wav");

      System.out.format("is is null ? => %s", is==null);
   }
}

Output

is is null ? => false
Stephan
  • 41,764
  • 65
  • 238
  • 329
  • But com.nishu.ld28.utilities is a package containing the class that I am trying to load the Sound files with? –  Dec 16 '13 at 14:24
  • @opiop65 For Java it's not a problem. You can have both the sound and the class located in the same package. The sound is actually seen as a resource. – Stephan Dec 16 '13 at 14:25
  • There's no need prefix the name of a resource with the package name of the class the needs it – Raffaele Dec 16 '13 at 14:25
  • Oh never mind, I thought you were trying to say that com.nishu.ld28 was a class. –  Dec 16 '13 at 14:25
  • That's fine. So then I can just call my resources like this?: merry_xmas.wav –  Dec 16 '13 at 14:50
  • @opiop65 You can, have look at the picture in my answer. – Stephan Dec 16 '13 at 14:51
  • And this will work after exporting too? Edit: Didn't work after exporting! –  Dec 16 '13 at 14:54
  • The wav file is found relatively to the class, so it'll work after exporting. – Stephan Dec 16 '13 at 14:59
  • Well it didn't :P It threw an NPE at this line: alBufferData(buffer, data.format, data.data, data.samplerate); Which takes the file I just loaded as the data. So the WaveData is never found. –  Dec 16 '13 at 15:11
  • @opiop65 I don't use `getClassLoader` in my sample code, check that buffer is not null, check that the path variable is correctly set. – Stephan Dec 16 '13 at 15:20
  • I checked that all of those aren't null already... thanks for your help though! –  Dec 16 '13 at 15:21