More Jar Help: Reading Audio Files

I've defined a class SoundEffects that generates AudioClip references.

Public class SoundEffects
{
   String path = "audio/";
   
   public AudioClip getAudio(String file)
   {
      return Applet.newAudioClip(getClass().getResource(path + file));
   }
   
   public void setPath(String string)
   {
      path = string;
   }
}

Now this works fine and my program works fine. The problem is that now I want to put
everything into an executable jar file. However, on attempting to execute the jar file:

c:\>java -jar program.jar

I get the error message:
   Exception in thread "main" java.lang.NullPointerException
   at sun.applet.AppletAudioClip.<init>(AppletAudioClip.java:59)
   etc....

Basically , it's saying it can't find the audio files.

Why and how do i reslove this?

mkandre...

LVL 1
mkandreAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

objectsCommented:
have you put the audio files in the jar, and in the correct directory?
0
mkandreAuthor Commented:
everything is in the right place, i know because i've used some images using the same getClass.getResource() method and they are read from the jar. I'm just trying to figure out why the audio won't be read.

0
CodingExpertsCommented:
can you post the code of  AppletAudioClip.java and your classpath too.
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

mkandreAuthor Commented:
thats a predifined class, as shownin the first line of the printStackTrace() error message

   ...at sun.applet.AppletAudioClip.<init>(AppletAudioClip.java:59)

Interface AudioClip is from package java.applet

0
CEHJCommented:
Please post output of

jar -tvf program.jar
0
mkandreAuthor Commented:
its just jar -tf program.jar

the 'v' not neccessary with the 'tf' instruction

META-INF/MANIFEST.MF
classes/games/wheel/audio/timer.wav
classes/games/wheel/GamePuzzlePlay.class
classes/games/wheel/AboutHelp$About.class
classes/games/wheel/AboutHelp.class
classes/games/wheel/AboutHelp$Help.class
classes/games/wheel/Puzzle.class
classes/games/wheel/PuzzleFile.class
classes/games/wheel/GameBoard$SolveTimer.class
classes/games/wheel/GameBoard$AHandler.class
classes/games/wheel/GameBoard$1.class
classes/games/wheel/GameBoard.class
classes/games/wheel/audio/lost.WAV
classes/games/wheel/audio/bankrupt.WAV
classes/games/wheel/BonusPuzzlePlay.class
classes/games/wheel/WheelPlay$Listener.class
classes/games/wheel/WheelPlay$1.class
classes/games/wheel/WheelPlay$Handler.class
classes/games/wheel/WheelPlay.class
classes/games/wheel/GamePuzzle.class
classes/games/wheel/audio/notfound.WAV
classes/games/wheel/audio/spin.WAV
classes/games/wheel/Wheel.class
classes/games/wheel/audio/round.WAV
classes/games/wheel/PuzzleBoard.class
classes/games/wheel/audio/found.wav
classes/games/wheel/WheelPlayer.class
classes/games/wheel/WheelItem.class
classes/games/wheel/GameData.class
classes/games/wheel/audio/win.wav
classes/games/wheel/LetterButton.class
classes/games/wheel/PlayerPack.class
classes/games/wheel/PlayerPack$Pointer.class
classes/games/wheel/SoundEffects.class
classes/games/wheel/BonusPuzzle.class
classes/games/wheel/BonusBoard$SolveTimer.class
classes/games/wheel/BonusBoard.class
classes/games/wheel/BonusBoard$AHandler.class
classes/games/wheel/images/wof.jpg
classes/games/wheel/LogoBoard.class
classes/games/wheel/BonusData.class
classes/games/wheel/PLetter.class

0
CEHJCommented:
You need to get your classpath and package base set properly in the above jar. What are these?
0
mkandreAuthor Commented:
what are you talking about?

Guyz i need to know how why the files are not being read.
It has nothing to do with classpaths nor packages;

those are all correctly set. because everything else works, except this audio thing;
0
CEHJCommented:
>>It has nothing to do with classpaths nor packages;

It has everything to do with it. The argument to getResource is dependent on both
0
mkandreAuthor Commented:
i KNOW, but it's obviously, correct as is presently since it works for the images:
  new ImageIcon(getclass().getResource("image/wof.jpg"));

if you look at the jar details printout
-all source code(class files) are in package classes.games.wheel;
-all audio files in sub-folder(package) audio
-the image file in sub-folder(package) image

so effectively the SoundEffects method getAudio would basically do something like
  Applet.newAudioClip(getClass().getResource("audio/spin.wav"));

note the (path+file) statement. where for example, file = "spin.wav";

0
mkandreAuthor Commented:
Before u ask the manifest details are:

Manifest-Version: 1.0
Created-By: My Name
Main-Class: classses.game.wheel.WheelPlay

This jar file executed perfectly when origanlly created. The problems started after I created the newer version with the audio files included. so whats wrong?

0
CEHJCommented:
Try

Applet.newAudioClip(getClass().getResource("/audio/spin.wav"));
0
mkandreAuthor Commented:
that makes no difference; the path as stands works fine running from my local package structure on my pc, but it just doesn't work in the jar.

0
objectsCommented:
> The problems started after I created the newer version with the audio files included.

where was the audio previously?
0
mkandreAuthor Commented:
> where was the audio previously?

there was no previous. I think the statement speaks for itself
> The problems started after I created the newer version with the audio files included.

in other words i decided to spice my program with some audio, which worked, and works, just not from in the jar file.

0
objectsCommented:
I see, I thought you were referring to the audio previously working.
0
CEHJCommented:
If you add

classes/games/wheel/audio

as a classpath to the jar manifest, you may be ok with

getResource("/spin.wav");
0
mkandreAuthor Commented:
aren't manifest class paths supposed to be the relative paths to other packages, (archives) that contain files needed to run the program?

e.g. Class-Path: classes/other.jar

0
CEHJCommented:
>>aren't manifest class paths supposed to be the relative paths to other packages

Usually, yes
0
objectsCommented:
Theres no need to distribute your audio seperately from your jar (nor would you want to).
What you already have looks fine, and as the type of file is totally irrelevant anyway to what you are doing so theres no logical reason why it works for images and not audio. The only inconsistency I can see is that your audio files have upper case extension in your jar.
Other than that I see no problem with what you have.

Am more than happy to help resolve yopur problem, but would need to do some closer inspection of your code and jar.
0
CEHJCommented:
>>The only inconsistency I can see is that your audio files have upper case extension in your jar.

LOL - surely that's it?
0
mkandreAuthor Commented:
Is there any other way of playing sounds/audio without using class Applet. Surely there must be other ways. I thing applet properties are probably the reason for my problem. because i'm using applet's static method newAudioClip(), but i'm not designing and Applet object. so its getAppletContext() method would throw a NullPointerException.

But I'm just guessing here. Any help?

0
objectsCommented:
From what you've posted thats not the problem. Problem appears to be getResource() returning null which indicates it cannot find the name resource.
0
CEHJCommented:
Try

Applet.newAudioClip(getClass().getResource("/audio/spin.WAV"));

and

Applet.newAudioClip(getClass().getResource("audio/spin.WAV"));

if you haven't already
0
objectsCommented:


> Applet.newAudioClip(getClass().getResource("/audio/spin.WAV"));

that won't work

> Applet.newAudioClip(getClass().getResource("audio/spin.WAV"));

thats already been mentioned

0
mkandreAuthor Commented:
why don't u guys try creating a program that uses audio files and run it from a jar and see if u have the same probs i have.

0
objectsCommented:
i've run lots of apps using audio from a jar without a problem :)
Your problem is with finding the resource, not the playing of it.

0
mkandreAuthor Commented:
So the question is: What is wrong with my program? and how do I go about correcting it?

0
objectsCommented:
from what you've posted there is nothing wrong with your code, what needs to be determined is why it isn't finding the resources in your jar.
0
mkandreAuthor Commented:
It's really weird. i don't know what to do, it's frustrating.

0
objectsCommented:
If you can produce a small example that reproduces the problem I can do some testing here for you.
0
objectsCommented:
did u check that the case of the resource named matched that of the file?
0
mkandreAuthor Commented:
yep, i checked. i event re-did the audio files end esured the extensions are lower case (.wav)

the SoundEffects class is used exactly as is above.

and inside the classes that use the sounds:

SoundEffects effects = new SoundEffects();

AudioClip clip = effects.getAudio("sipn.wav"); // the error occurs at this point

0
CEHJCommented:
If you post your jar somewhere public, i'll test it
0
mkandreAuthor Commented:
strangely enough, i just decided to startfom scratch and rebuild the jar (after several previous attempts failed to find the audio), it suddenly now executes perfectly. i don't know what the problem was or what fixed it, i don't think i did anything differently. but hey it's working. thanks anyway guys. you all tried.

mkandre
0
CEHJCommented:
Are the contained paths the same?
0
objectsCommented:
excellenr news, must have been some minor problem with the jar. as i said your code looked fine.
0
mkandreAuthor Commented:
> Are the contained paths the same?

Yep nothing changed. probably some error with the IDE. I use Sun(R) ONE (built on Net Beans).

but everthings fine now. so thanks again.



0
mkandreAuthor Commented:
Hey Guyz, i got something else here that maybe you can still earn those points from, plus i dont want to start a new question.

Now i wan't to improve my application by making network frielndly. i.e i want multiple players (yes its a game). I already have the capability to perform the network functionallity, except that Right now my appliction requires that the IP address or computer name (alias) be known to complete the connection (which i understand completely). Now lets say each instance of the application can either host or participate in a network game. i want that if the host option is selected the "server" (thats the same appliction) detects attempts by others to connect, and aslo that if the appliction, if the join option is selected, to seek out all "servers" and connect to the first server it finds.


mkandre
0
objectsCommented:
see if anything here helps you:
http://www.onjava.com/topics/java/P2P_Java
0
moduloCommented:
PAQed with points refunded (150)

modulo
Community Support Moderator
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.