gpt4 book ai didi

java - 程序在 Eclipse 中可以运行,但是当我导出到可运行的 JAR 时,它就不能运行。文件未找到异常

转载 作者:行者123 更新时间:2023-12-02 09:10:21 27 4
gpt4 key购买 nike

我尝试制作一个可运行的 JAR,但由于某种原因我无法玩我的游戏。我做了一些研究并通过命令提示符运行它以尝试找到错误,我在下面得到了这个。显然我知道问题所在,我只需要解决它。我的 res 文件夹中有音频文件,该文件夹位于我的 src 中。因此,如果它已经在程序中,我无法弄清楚为什么会出现此错误。想法?

Exception in thread "main" java.lang.NullPointerException
at java.base/java.util.Objects.requireNonNull(Unknown Source)
at java.desktop/javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)
at builder.AudioPlayer.playMenuSound(AudioPlayer.java:20)
at builder.Game.<init>(Game.java:56)
at builder.Game.main(Game.java:61)
package builder;

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.FloatControl;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

public class AudioPlayer {

private static Clip play;

public static void playMenuSound()
{
try {
//AudioInputStream menuSound = AudioSystem.getAudioInputStream(new File("src/res/introSong.wav")); //Take in audio from res folder
AudioInputStream menuSound = AudioSystem.getAudioInputStream(AudioPlayer.class.getClassLoader().getResourceAsStream("introSong.wav"));
play = AudioSystem.getClip(); //
play.open(menuSound); //Play the sound
FloatControl volume = (FloatControl) play.getControl(FloatControl.Type.MASTER_GAIN); //Get control of volume
volume.setValue(1.0f); //0.0 - 1.0 volume
play.loop(Clip.LOOP_CONTINUOUSLY); //Loop once clip is over
}catch (LineUnavailableException | IOException | UnsupportedAudioFileException e){
e.printStackTrace();
}
}

public static void playGameSound()
{
try {
//AudioInputStream gameSound = AudioSystem.getAudioInputStream(new File("src/res/inGame.wav")); //Take in audio from res folder
AudioInputStream gameSound = AudioSystem.getAudioInputStream(AudioPlayer.class.getClassLoader().getResourceAsStream("inGame.wav"));
play = AudioSystem.getClip(); //
play.open(gameSound); //Play the sound
FloatControl volume = (FloatControl) play.getControl(FloatControl.Type.MASTER_GAIN); //Get control of volume
volume.setValue(0.5f); //0.0 - 1.0 volume
play.loop(Clip.LOOP_CONTINUOUSLY); //Loop once clip is over
}catch (LineUnavailableException | IOException | UnsupportedAudioFileException e){
e.printStackTrace();
}
}

public static void stopMusic()
{
play.close(); //Stop music
}
}

最佳答案

代码中的问题在于您尝试使用引用实例化文件,此处:

AudioInputStream gameSound = AudioSystem.getAudioInputStream(new File("src/res/inGame.wav"));

您需要使用 ClassLoader 将文件作为资源获取,而不是这样做,因为它位于资源 (res) 文件夹内。

您的代码应如下所示:

AudioInputStream gameSound = AudioSystem.getAudioInputStream(AudioPlayer.class.getClassLoader().getResourceAsStream("inGame.wav"));

同样应该在 playMenuSound() 方法中的 introSong.wav 中完成。代码应该类似于

AudioInputStream gameSound = AudioSystem.getAudioInputStream(AudioPlayer.class.getClassLoader().getResourceAsStream("introSong.wav"));

我真的希望这能解决您的问题。

关于java - 程序在 Eclipse 中可以运行,但是当我导出到可运行的 JAR 时,它就不能运行。文件未找到异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59475560/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com