wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Jar file creation and .wav files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Jar file creation and .wav files" Watch "Jar file creation and .wav files" New topic
Author

Jar file creation and .wav files

Al Dunne
Greenhorn

Joined: Apr 24, 2011
Posts: 13
Hi all,

I've a problem with creating a jar file that's melting my brain. I've been at it for the last 3 days and cannot solve. I've tried pretty much everything. If anyone can help, please do.

So originally I had a File [] called soundFile that is going to store 9 files. I had a loop to import the 9 wav files. These wav files were called 0db.wav, 1db.wav etc etc and to get the code to work, I had to put these files inside my Version 4 folder (see image attached - Method 1 folder).

My original code for loading was as follows:

Code:
for (int i = 0; i < 9; i++){

soundFile [i] = new File (i + "db.wav");
}



This worked fine when I ran the project in NetBeans. However, a couple of days ago I tried to create the jar file and the section that played the sounds did not work i.e. did not play the sounds. From looking at it, when I told NetBeans to load the file it looked in the Version 4 folder (see image attached - Method 1 folder).

I then looked around everywhere to see what the problem was. I ended up getting the jar file working by hard-coding in a directory address but as you might guess, when I changed computer it stopped working.

So then after getting advice from other forums I tried numerous things. The most promising was to move the wav files into the rubberduckssoundproject folder which is inside the src folder. This folder rubberduckssoundproject contains all our other code in the project. The code for loading the wav files was then:

Code:
for (int i = 0; i < 9; i++){
try{
soundFile [i] = new File (this.getClass().getResource (i+"db.wav").toURI());
}
catch (Exception fileError){
System.out.println("Sorry but there's a problem loading your files into an array");
fileError.printStackTrace();
}

System.out.println("The soundfile located at soundFile "+i+" has the following path:"+soundFile[i].getAbsolutePath());
}


So this worked when the project was run in NetBeans. The path it returned for the files was ".........\Version 4\build\classes\rubberduckssoundproject\0db.wav"

This looked good to me as I think (I'm fairly new to Java) the jar files gets built from the build folder???

However, now when I clean and build, a jar file gets created but when I go to click this jar nothing happens. Then when I go back into the project folder it will not let me delete the jar file. It keeps saying "The action can't be completed because the file is open in Java (TM) Platform SE binary. This leads me to believe there's some sort of infinite loop or something being created?

I've thought about creating InputStreams and passing them to the
Code:
audio = AudioSystem.getAudioInputStream(soundFile[activeSound]);

The problem with this is I need an array of InputStreams and NetBeans will not let me do this. Can't remember exactly why, but it wouldn't. If anyone needs to know what it said I can try it again.

Please, please, please help if you can. I'm on the verge of giving up and I really don't want to.

Thanks a mil.

Al.


[Thumbnail for Method 1 folder.png]

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Stop using java.io.File completely. Use java.net.URL instead. The thing is, once a file goes into a JAR file, it's no longer a file on the file system. java.io.File can only handle files on the file system (and Windows shares), not files inside ZIP or JAR files.

The good news is, you hardly have to change anything. You already used this.getClass().getResource (i+"db.wav") which returns a URL. All you need to do is stop trying to convert that into a File. AudioSystem.getAudioInputStream is overloaded to also take a URL so you can just use the overloaded method.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Al Dunne
Greenhorn

Joined: Apr 24, 2011
Posts: 13
Hi Rob,

Thanks for that. So does that mean all I'll need in my code is:

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Jar file creation and .wav files