1

I've got a problem regarding audio.

I am coding in Java using Netbeans, and I want to export my project so that it doesn't require Netbeans to run, so I decided to create a JAR File. I put all the images, and audio files (aiff) for my program in the src folder so that they could be accessible from outside the coding console.

I have declared my InputStream and AudioStream as global variables, and I am using them within a try catch block, where the code is run when a button is clicked.

AudioPlayer.player.stop(audios);
    try{
        in = new FileInputStream (new File("src/glory.aiff"));
        System.out.println("Button Clicked");
        audios = new AudioStream (in);
        AudioPlayer.player.start(audios);
    }
    catch (Exception e)
    {
        JOptionPane.showMessageDialog(null, e);
    }

This code perfectly fine when I run the program on Netbeans, however when I create a JAR file, the audio doesn't play, and an error pops up saying that the file cannot be found. I've tried this on my computer, and other computers, and the same problem occurs.

What I find strange, is that the pictures I mentioned earlier are in the same SRC folder, and they display correctly. I have linked these pictures through the use of icons, and Netbeans auto-generates code for that.

    jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IMG_2138.JPG")));

I have done research of my own, however other people who have this problem don't seem to be using the same AudioStream as myself and I don't know how to adapt it to solve my problem! I've also tried changing the file path of the audio, but whenever it has worked in Netbeans, it has not worked in my JAR file.

Would anyone please be able to tell me how I could fix this problem?

Thanks,

Rohan

Rohan Iyer
  • 21
  • 2
  • Is the audio file being bundled into the jar? (probably is) If so, look into `ClassLoader.getResourceAsStream()` instead of using `FileInputStream`. – Mad Physicist Dec 17 '15 at 13:20
  • Hi Mad Physicist, Thank you so much! Changing the FileInputStream to getSystemResourceAsStream() worked!! :) Do you know why the FileInput Stream wasn't working though? – Rohan Iyer Dec 18 '15 at 03:32
  • Yes. Once your project is in a jar, the audio file is no longer a file on the file system, but part of the zip archive that is the jar. FileInputStream only works on standalone files that you can open directly. – Mad Physicist Dec 18 '15 at 12:52
  • Ahhh alright, thank you very much for your explanation. :) I've just realised that I may have a similar problem trying to open PDF's through a JAR as well! I now to see how I can change the file type from InputStream or URL to File...! – Rohan Iyer Dec 19 '15 at 11:35

1 Answers1

-1

I think it would be easier for you to create an setup for your java program in Netbeans and it will still be able to run without Netbeans.

You can do so using a software called excelsior jet.

All you would need to do is watch a short 5 min video on youtube on how to use this and you will have your setup which you can install in your computer and other computers.

Also before you use the software to make the setup it would be good for you to check whether your program actually runs properly as a jar file.

To do so just go to where you save your Netbeans projects, click the project for your program and then enter the "dist" folder(This is where the jar file for project is stored). Click the jar file to check whether the project runs normally.

If you do not find your jar file in the dist folder then its most probably because you tampered with your "manifest.mf" file or your "build.xml" file in your java project.

  • This in no way offers an explanation of what is happening in the OP's situation. While your workaround may work, an explanation of why it is necessary would be nice. – Mad Physicist Dec 17 '15 at 13:19