Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

More Jar Help: Reading Audio Files

Posted on 2004-11-07
42
Medium Priority
?
282 Views
Last Modified: 2013-11-23
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...

0
Comment
Question by:mkandre
  • 17
  • 12
  • 10
  • +2
41 Comments
 
LVL 92

Expert Comment

by:objects
ID: 12520815
have you put the audio files in the jar, and in the correct directory?
0
 
LVL 1

Author Comment

by:mkandre
ID: 12520846
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
 
LVL 6

Expert Comment

by:CodingExperts
ID: 12520883
can you post the code of  AppletAudioClip.java and your classpath too.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 1

Author Comment

by:mkandre
ID: 12520927
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12522151
Please post output of

jar -tvf program.jar
0
 
LVL 1

Author Comment

by:mkandre
ID: 12523738
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12524027
You need to get your classpath and package base set properly in the above jar. What are these?
0
 
LVL 1

Author Comment

by:mkandre
ID: 12524239
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12524325
>>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
 
LVL 1

Author Comment

by:mkandre
ID: 12525070
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
 
LVL 1

Author Comment

by:mkandre
ID: 12525167
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12527060
Try

Applet.newAudioClip(getClass().getResource("/audio/spin.wav"));
0
 
LVL 1

Author Comment

by:mkandre
ID: 12527468
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
 
LVL 92

Expert Comment

by:objects
ID: 12529596
> The problems started after I created the newer version with the audio files included.

where was the audio previously?
0
 
LVL 1

Author Comment

by:mkandre
ID: 12530722
> 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
 
LVL 92

Expert Comment

by:objects
ID: 12530746
I see, I thought you were referring to the audio previously working.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12531748
If you add

classes/games/wheel/audio

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

getResource("/spin.wav");
0
 
LVL 1

Author Comment

by:mkandre
ID: 12535328
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12537839
>>aren't manifest class paths supposed to be the relative paths to other packages

Usually, yes
0
 
LVL 92

Expert Comment

by:objects
ID: 12539193
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12539225
>>The only inconsistency I can see is that your audio files have upper case extension in your jar.

LOL - surely that's it?
0
 
LVL 1

Author Comment

by:mkandre
ID: 12553255
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
 
LVL 92

Expert Comment

by:objects
ID: 12553313
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12554279
Try

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

and

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

if you haven't already
0
 
LVL 92

Expert Comment

by:objects
ID: 12560661


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

that won't work

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

thats already been mentioned

0
 
LVL 1

Author Comment

by:mkandre
ID: 12562850
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
 
LVL 92

Expert Comment

by:objects
ID: 12562858
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
 
LVL 1

Author Comment

by:mkandre
ID: 12569645
So the question is: What is wrong with my program? and how do I go about correcting it?

0
 
LVL 92

Expert Comment

by:objects
ID: 12569686
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
 
LVL 1

Author Comment

by:mkandre
ID: 12569761
It's really weird. i don't know what to do, it's frustrating.

0
 
LVL 92

Expert Comment

by:objects
ID: 12569787
If you can produce a small example that reproduces the problem I can do some testing here for you.
0
 
LVL 92

Expert Comment

by:objects
ID: 12569858
did u check that the case of the resource named matched that of the file?
0
 
LVL 1

Author Comment

by:mkandre
ID: 12573561
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12573566
If you post your jar somewhere public, i'll test it
0
 
LVL 1

Author Comment

by:mkandre
ID: 12574468
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12574472
Are the contained paths the same?
0
 
LVL 92

Expert Comment

by:objects
ID: 12575471
excellenr news, must have been some minor problem with the jar. as i said your code looked fine.
0
 
LVL 1

Author Comment

by:mkandre
ID: 12583781
> 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
 
LVL 1

Author Comment

by:mkandre
ID: 12639555
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
 
LVL 92

Expert Comment

by:objects
ID: 12640154
see if anything here helps you:
http://www.onjava.com/topics/java/P2P_Java
0
 

Accepted Solution

by:
modulo earned 0 total points
ID: 12857120
PAQed with points refunded (150)

modulo
Community Support Moderator
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month11 days, 16 hours left to enroll

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question