Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 导出到jar后没有声音_Java_Eclipse_Audio_Embedded Resource - Fatal编程技术网

Java 导出到jar后没有声音

Java 导出到jar后没有声音,java,eclipse,audio,embedded-resource,Java,Eclipse,Audio,Embedded Resource,我的应用程序有问题。当我在Eclipse中运行应用程序时,声音播放得很好,但若我将应用程序导出到runnable jar,声音就不起作用了 方法,其中播放声音: public static synchronized void playSound() { new Thread(new Runnable() { // The wrapper thread is unnecessary, unless it b

我的应用程序有问题。当我在Eclipse中运行应用程序时,声音播放得很好,但若我将应用程序导出到runnable jar,声音就不起作用了

方法,其中播放声音:

public static synchronized void playSound() 
    {
            new Thread(new Runnable() 
            {
                // The wrapper thread is unnecessary, unless it blocks on the
                // Clip finishing; see comments.
                public void run() 
                {
                    try
                    {
                        Clip clip = AudioSystem.getClip();
                        AudioInputStream inputStream = AudioSystem.getAudioInputStream(getClass().getResourceAsStream("sound.wav"));
                        clip = AudioSystem.getClip();
                        clip.open(inputStream);
                        clip.start(); 
                    } 
                    catch (Exception e) 
                    {
                        System.err.println(e.getMessage());
                    }
                }
            }).start();
        }
哪里会出错

问题就在这里

AudioInputStream inputStream = AudioSystem.getAudioInputStream(getClass().getResourceAsStream("sound.wav"));
在JAR文件中,由于任何原因,getResourceAsStream无法工作。因此,我将其替换为
getResource

AudioInputStream inputStream = AudioSystem.getAudioInputStream(getClass().getResource("sound.wav"));

这很好。

JAR中是否存在wave文件?我尝试解包JAR文件,发现wave文件在其中。哇,这太奇怪了