简体   繁体   中英

How do I load a .wav file from a jar file without the resources in the same directory as the jar file

Whenever I open my .jar file, the sound will not work unless the files are in a folder titled "res" in the same directory as the .jar file. Now it will now work regardless of where the res folder is Here's my code

try {
         InputStream defaultSound = Game.class.getResourceAsStream("/TrailsGameMusic.wav");
         // getClass().getSy.getResource("/images/ads/WindowsNavigationStart.wav");

         System.out.println("defaultSound " + defaultSound);  // check the URL!
         AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(defaultSound);
         Clip clip = AudioSystem.getClip();
         clip.open(audioInputStream);
         clip.loop(Clip.LOOP_CONTINUOUSLY);
    } catch (Exception ex) {
         ex.printStackTrace();
    }

It's probably a problem with absolute path: If you declare it like that: "/res/TrailsGameMusic.wav" , you are referring to absolute path - it works. If you will write only "TrailsGameMusic.wav" , it will be a relative path - it will not work.

You can configure your resources using for example this link: How do I add a resources folder to my Java project in eclipse

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM