Page 1 of 3 123 LastLast
Results 1 to 20 of 46
Like Tree4Likes

Thread: This very simple code throws exception... and I can't understand why

  1. #1
    NotTooOld is offline Hostage
    Join Date
    Apr 2013
    Location
    China
    Posts
    128
    Rep Power
    0

    Default This very simple code throws exception... and I can't understand why

    This is so strange! It's like my computer can't see the file and they are both in the same package. I'm using Eclipse and tried this barebones code because I get the same error in the project I'm working on. I've closed Eclipse and restarted it. I've put the file in other locations, still the same error. If I double click the wav file it plays fine. Trade for other wav files and I get the same error... Stumped!!

    the error: java.io.FileNotFoundException: moo.wav (The system cannot find the file specified)

    the code:

    import java.io.File;
    import java.io.IOException;
    import javax.sound.sampled.AudioInputStream;
    import javax.sound.sampled.AudioSystem;
    import javax.sound.sampled.Clip;
    import javax.sound.sampled.LineUnavailableException;
    import javax.sound.sampled.UnsupportedAudioFileException;
    import javax.swing.JFrame;

    public class MyTestClass {

    public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setSize(300,300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setVisible(true);

    try {
    AudioInputStream audio = AudioSystem.getAudioInputStream(new File("moo.wav"));
    Clip clip = AudioSystem.getClip();
    clip.open(audio);
    clip.start();
    }

    catch(UnsupportedAudioFileException uae) {
    System.out.println(uae);
    }
    catch(IOException ioe) {
    System.out.println(ioe);
    }
    catch(LineUnavailableException lua) {
    System.out.println(lua);
    }
    }

    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,932
    Rep Power
    8

    Default Re: This very simple code throws exception... and I can't understand why

    I suggest printing out the absolute path of that file. Is Java really looking for the file where you think it's looking?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    NotTooOld is offline Hostage
    Join Date
    Apr 2013
    Location
    China
    Posts
    128
    Rep Power
    0

    Default Re: This very simple code throws exception... and I can't understand why

    Quote Originally Posted by KevinWorkman View Post
    I suggest printing out the absolute path of that file. Is Java really looking for the file where you think it's looking?
    Yes, I've tried numerous times and many locations. This is making me crazy. I'm gonna try the same code on my laptop now. <wink> I'm in China and maybe the gov is playing with my head, or my PC has problems seeing wav files in Eclipse. Oh! I Think I'll try running this from the command line. Maybe it's an Eclipse issue. ................ From command line the window opens and instantly closes. I'm not hearing my .wav.
    Last edited by NotTooOld; 04-02-2013 at 05:03 PM. Reason: add more info

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: This very simple code throws exception... and I can't understand why

    No it's not an eclipse problem.
    Put some debug in there, as Kev suggested:
    Java Code:
    File f = new File("moo.wav");
    System.out.println(f.getAbsolutePath()); // <- tells you where it is looking for moo.wav
    AudioInputStream audio = AudioSystem.getAudioInputStream(f);
    KevinWorkman and NotTooOld like this.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  5. #5
    NotTooOld is offline Hostage
    Join Date
    Apr 2013
    Location
    China
    Posts
    128
    Rep Power
    0

    Default Re: This very simple code throws exception... and I can't understand why

    Quote Originally Posted by PhHein View Post
    No it's not an eclipse problem.
    Put some debug in there, as Kev suggested:
    Java Code:
    File f = new File("moo.wav");
    System.out.println(f.getAbsolutePath()); // <- tells you where it is looking for moo.wav
    AudioInputStream audio = AudioSystem.getAudioInputStream(f);
    The System.out line works fine. (Eclipse, or at least that part of it can see the file.) I printed the .getAbsolutePath for the .java file and the .wav. The paths are identical. Line 3 of your code, which is now in my code inside the try/catch still fails. I'm still stumped.
    Last edited by NotTooOld; 04-02-2013 at 05:38 PM.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: This very simple code throws exception... and I can't understand why

    Show a run through that code then, with the output, including the full error and stack trace.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    NotTooOld is offline Hostage
    Join Date
    Apr 2013
    Location
    China
    Posts
    128
    Rep Power
    0

    Default Re: This very simple code throws exception... and I can't understand why

    Here's the errors (hope this is what you wanted when you ask for "full error") I get running the code without the try/catch block:
    C:\Documents and Settings\Chris\workspace\03-YourFirstProject\MyFirstJavaClass.java
    C:\Documents and Settings\Chris\workspace\03-YourFirstProject\moo.wav
    Exception in thread "main" java.io.FileNotFoundException: moo.wav (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(Unknown Source)
    at com.sun.media.sound.WaveFloatFileReader.getAudioIn putStream(Unknown Source)
    at javax.sound.sampled.AudioSystem.getAudioInputStrea m(Unknown Source)
    at MyFirstJavaClass.main(MyFirstJavaClass.java:31)

    Here's what I get with the try/catch:
    java.io.FileNotFoundException: moo.wav (The system cannot find the file specified)
    Last edited by NotTooOld; 04-02-2013 at 06:13 PM.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: This very simple code throws exception... and I can't understand why

    For the stack trace use
    <exception>.printStackTrace(), instead of your Sys.out.println calls in the catch blocks.

    So, this is the location of your physical file:
    C:\Documents and Settings\Chris\workspace\03-YourFirstProject\moo.wav
    ?

    ETA: Actually, I'm being dim. You've given us the stack trace, as that's this bit:
    "
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(Unknown Source)
    at com.sun.media.sound.WaveFloatFileReader.getAudioIn putStream(Unknown Source)
    at javax.sound.sampled.AudioSystem.getAudioInputStrea m(Unknown Source)
    at MyFirstJavaClass.main(MyFirstJavaClass.java:31)
    "
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    NotTooOld is offline Hostage
    Join Date
    Apr 2013
    Location
    China
    Posts
    128
    Rep Power
    0

    Default Re: This very simple code throws exception... and I can't understand why

    Yes. That is the location of both the class file containing this code and that .wav file. And you are not at all dim, and very kind to offer your help. WOW!! 2 more posts and you hit 10k! You rock!
    Last edited by NotTooOld; 04-02-2013 at 06:22 PM.

  10. #10
    NotTooOld is offline Hostage
    Join Date
    Apr 2013
    Location
    China
    Posts
    128
    Rep Power
    0

    Default Re: This very simple code throws exception... and I can't understand why

    Quote Originally Posted by NotTooOld View Post
    Here's the errors (hope this is what you wanted when you ask for "full error") I get running the code without the try/catch block:
    C:\Documents and Settings\Chris\workspace\03-YourFirstProject\MyFirstJavaClass.java
    C:\Documents and Settings\Chris\workspace\03-YourFirstProject\moo.wav
    Exception in thread "main" java.io.FileNotFoundException: moo.wav (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(Unknown Source)
    at com.sun.media.sound.WaveFloatFileReader.getAudioIn putStream(Unknown Source)
    at javax.sound.sampled.AudioSystem.getAudioInputStrea m(Unknown Source)
    at MyFirstJavaClass.main(MyFirstJavaClass.java:31)

    Here's what I get with the try/catch:
    java.io.FileNotFoundException: moo.wav (The system cannot find the file specified)
    Could the problem be in the " " (blank spaces) in the path? ie \Documents(space)and(space)Settings\
    I've seen some talk about being careful about blank space in java. Just a thought.

  11. #11
    NotTooOld is offline Hostage
    Join Date
    Apr 2013
    Location
    China
    Posts
    128
    Rep Power
    0

    Default Re: This very simple code throws exception... and I can't understand why

    Quote Originally Posted by NotTooOld View Post
    Could the problem be in the " " (blank spaces) in the path? ie \Documents(space)and(space)Settings\
    I've seen some talk about being careful about blank space in java. Just a thought.
    Nope. I moved everything to C:\Eclipse and this code still fails. wth?

  12. #12
    NotTooOld is offline Hostage
    Join Date
    Apr 2013
    Location
    China
    Posts
    128
    Rep Power
    0

    Default Re: This very simple code throws exception... and I can't understand why

    I really don't get it.

    Current Code: (I rewrote it, but is essentially identical)

    package Test;
    import java.io.File;
    import java.io.IOException;
    import javax.sound.sampled.AudioInputStream;
    import javax.sound.sampled.AudioSystem;
    import javax.sound.sampled.Clip;
    import javax.sound.sampled.LineUnavailableException;
    import javax.sound.sampled.UnsupportedAudioFileException;
    import javax.swing.JFrame;

    public class SoundTest {
    public static void main(String[] args) throws UnsupportedAudioFileException, IOException, LineUnavailableException {
    JFrame frame = new JFrame();
    frame.setSize(300,300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setVisible(true);
    File f = new File("moo.wav");
    File p = new File("SoundTest.java");
    System.out.println(p.getAbsolutePath());
    System.out.println(f.getAbsolutePath());
    try {
    AudioInputStream audio = AudioSystem.getAudioInputStream(f);
    Clip clip = AudioSystem.getClip();
    clip.open(audio);
    clip.start();
    }
    catch(UnsupportedAudioFileException uae) {
    uae.printStackTrace();
    }
    catch(IOException ioe) {
    ioe.printStackTrace();
    }
    catch(LineUnavailableException lua) {
    lua.printStackTrace();
    }
    }
    }

    Stack Trace:

    C:\Eclipse\workspaceZero\ZeroTestCodePackage\Sound Test.java
    C:\Eclipse\workspaceZero\ZeroTestCodePackage\moo.w av
    java.io.FileNotFoundException: moo.wav (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(Unknown Source)
    at com.sun.media.sound.WaveFloatFileReader.getAudioIn putStream(Unknown Source)
    at javax.sound.sampled.AudioSystem.getAudioInputStrea m(Unknown Source)
    at Test.SoundTest.main(SoundTest.java:22)

    Can someone verify that this code works on their computer please?

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: This very simple code throws exception... and I can't understand why

    Sorry, but those files are clearly not in that directory.
    Your class is in a Test package, so must exist in the directory Test.
    But you are saying it is in the directory:
    C:\Eclipse\workspaceZero\ZeroTestCodePackage

    The output from getAbsoluteFilePath is simply where Java is going to look for the file...it does not mean the file is actually there.
    And in your case it isn't.
    NotTooOld likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  14. #14
    NotTooOld is offline Hostage
    Join Date
    Apr 2013
    Location
    China
    Posts
    128
    Rep Power
    0

    Default Re: This very simple code throws exception... and I can't understand why

    Quote Originally Posted by Tolls View Post
    Sorry, but those files are clearly not in that directory.
    Your class is in a Test package, so must exist in the directory Test.
    But you are saying it is in the directory:
    C:\Eclipse\workspaceZero\ZeroTestCodePackage

    The output from getAbsoluteFilePath is simply where Java is going to look for the file...it does not mean the file is actually there.
    And in your case it isn't.
    Thanks much for the help everyone!
    I added (System.out.println(f.exists());) and sure enough that path was wrong. Why did Eclipse not look in the same folder that the call came from? It defaults to the Package level? I'll have to remember that. Now I'm getting this error:

    javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input file
    at javax.sound.sampled.AudioSystem.getAudioInputStrea m(Unknown Source)
    at Test.SoundTest.main(SoundTest.java:23)

    I could swear I read that a wav or au file would work.

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: This very simple code throws exception... and I can't understand why

    File looks based on the execution location.
    getResource (or getResourceAsStream) looks based on your classpath.
    So:
    Java Code:
    AudioInputStream audio = AudioSystem.getAudioInputStream(SoundTest.class.getResourceAsStream("your file"));
    Should do what you expect it to.
    File(s) are usually used for interacting with the underlying file system.
    Resources are used when interacting with stuff that is going to be packaged with your application.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  16. #16
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: This very simple code throws exception... and I can't understand why

    As for the audio format error, do you know exactly what encoding and properties the wav file has?
    WAV is simply a container, the underlying bit rate etc can vary and some may not be supported by your hardware or drivers, so being able to run one wav file but not another is a common occurrence.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  17. #17
    NotTooOld is offline Hostage
    Join Date
    Apr 2013
    Location
    China
    Posts
    128
    Rep Power
    0

    Default Re: This very simple code throws exception... and I can't understand why

    I have no idea what encoding and such the .wav has, but Windows Media Player opens and plays it when I double click it.
    Does this mean I need to add a codex to the JRE? Eclipse?
    Last edited by NotTooOld; 04-03-2013 at 02:04 PM.

  18. #18
    NotTooOld is offline Hostage
    Join Date
    Apr 2013
    Location
    China
    Posts
    128
    Rep Power
    0

    Default Re: This very simple code throws exception... and I can't understand why

    Quote Originally Posted by Tolls View Post
    File looks based on the execution location.
    getResource (or getResourceAsStream) looks based on your classpath.
    So:
    Java Code:
    AudioInputStream audio = AudioSystem.getAudioInputStream(SoundTest.class.getResourceAsStream("your file"));
    Should do what you expect it to.
    File(s) are usually used for interacting with the underlying file system.
    Resources are used when interacting with stuff that is going to be packaged with your application.
    Adding that code didn't fix it, but makes me wonder if I chose the correct "getAudioInputStream()" Seems there are 5 flavors of that method. It also seems like the compiler is smart enough to know which one to use based on the params I'm passing it.
    Here's the latest error:

    Exception in thread "main" java.lang.NullPointerException
    at com.sun.media.sound.SoftMidiAudioFileReader.getAud ioInputStream(Unknown Source)
    at javax.sound.sampled.AudioSystem.getAudioInputStrea m(Unknown Source)
    at Test.SoundTest.main(SoundTest.java:23)

  19. #19
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: This very simple code throws exception... and I can't understand why

    No idea I'm afraid.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  20. #20
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: This very simple code throws exception... and I can't understand why

    Quote Originally Posted by NotTooOld View Post
    Adding that code didn't fix it, but makes me wonder if I chose the correct "getAudioInputStream()" Seems there are 5 flavors of that method. It also seems like the compiler is smart enough to know which one to use based on the params I'm passing it.
    (That other reply was in reply to your previosu post).

    It wasn't intenede to fix the audio problem, but to give you the correct way to access the audio file, if that file is intended to be part of your application deployment.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Page 1 of 3 123 LastLast

Similar Threads

  1. Replies: 8
    Last Post: 03-11-2012, 04:28 PM
  2. Simple Swing throws Exception in thread "AWT-EventQueue-0"
    By sultanofswing in forum AWT / Swing
    Replies: 3
    Last Post: 10-29-2010, 09:20 PM
  3. throws exception
    By simorgh in forum New To Java
    Replies: 1
    Last Post: 07-30-2010, 12:24 AM
  4. throws Exception
    By javaplus in forum New To Java
    Replies: 1
    Last Post: 11-06-2007, 07:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •