当我 运行 作为导出的 jar 时,我得到一个 FileNotFoundexception

When I running as an exported jar, I get a FileNotFound exeption

我正在创建一个 运行 包含一些声音文件的程序。当我在我的 IDE (Eclipse) 中 运行 它 运行 没问题,但是当从 jar 中 运行 时,文件的路径似乎已经改变。项目路径为:

MyProject: 
           src (in build path):
                package:
                       AudioFile.java
                       SoundEffectManager.java
           assets (in build path):
                 sounds:
                      soundfile.wav

和读取文件的 AudioFile.java 的代码:

import java.io.File;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineEvent;
import javax.sound.sampled.LineListener;

public class AudioFile implements LineListener {
    private File audioFile;
    private boolean paused = false;
    private AudioInputStream ais;
    private AudioFormat format;
    private DataLine.Info info;
    private Clip clip;
    private volatile boolean playing;

   // Constructor
    public AudioFile(String fileName) {
        try {
        audioFile = new File(fileName);
        ais = AudioSystem.getAudioInputStream(audioFile);
        format = ais.getFormat();
        info = new DataLine.Info(Clip.class, format);
        clip = (Clip) AudioSystem.getLine(info);
        clip.addLineListener(this);
        clip.open(ais);
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
    }
    // Play Pause and other helpers
    public void play() {
        clip.start();
        playing = true;
    }

    public boolean isPlaying() {
        return playing;
    }

    public void stop() {
        clip.stop();
        clip.flush();
        clip.setFramePosition(0);
        playing = false;
    }

    public void pause() {
        if (!paused) {
        paused = true;
        clip.stop();
        }
    }
    public void resume() {
        if (paused) {
            clip.start();
            paused = false;
        } 
    }
    @Override
    public void update(LineEvent event) {
        if (event.getType() == LineEvent.Type.START && !paused) {
            playing = true;
        }
        else if (event.getType() == LineEvent.Type.STOP && !paused) {
            clip.stop();
            clip.flush();
            clip.setFramePosition(0);
            playing = false;
            paused = false;
        }

    } 

}

SoundEffectManager.java 创建 AudioFile 的代码(省略了助手和导入)

public class SoundEffectManager implements Runnable {
    private ArrayList<AudioFile> soundList;
    private volatile boolean running;
    public volatile ArrayList<AudioFile> playingSounds;
    public SoundEffectManager(String... sounds) {
        soundList = new ArrayList<AudioFile>();
        for(String sound : sounds) {
            soundList.add(new AudioFile("./assets/sounds/" + sound + ".wav"));
        }

    }
}

当调用 SoundEffectManager 构造函数时,它使用以下参数调用:"soundfile"

此代码在 IDE 中有效,但由于 java.io.FileNotFound 异常,在 运行nable jar 中无效。 jar 的文件路径是:

jarfile.jar:
            package:
                       AudioFile.class
                       SoundEffectManager.class

            META-INF:
                      MANIFEST.MF
            sounds:
               soundfile.wav


谢谢,如果你能帮忙。

您必须将声音文件放在资源文件夹中。然后就可以通过类加载器获取路径了。这应该适用于您的 ide 和 jar 文件。

this.getClass().getClassLoader().getResource("soundfile").getPath()