• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Jar file creation and .wav files

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
Method-1-folder.png
[Thumbnail for Method-1-folder.png]
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Al Dunne
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rob,

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

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic