gpt4 book ai didi

java - 无法从 jar 读取输入

转载 作者:行者123 更新时间:2023-12-02 00:48:54 26 4
gpt4 key购买 nike

当我运行java -jar MidiTest.jar时,输入MIDI文件,它会抛出:

Exception in thread "main" java.lang.NullPointerException        at com.sun.media.sound.StandardMidiFileReader.getSequence(StandardMidileReader.java:209)        at javax.sound.midi.MidiSystem.getSequence(MidiSystem.java:802)        at MidiTest.playMidi(MidiTest.java:56)        at MidiTest.(MidiTest.java:44)        at MidiTest.main(MidiTest.java:25)

If I use java MidiTest instead it could play without issue. What wrong with the code? I have already add Main-Class: MidiTest with newline on Manifest file

My code:

        private void playMidi() {
if(isPlaying.equals("0")) {
try {
song = MidiSystem.getSequence(
getClass().getResource(filename));
sequencer = MidiSystem.getSequencer();
sequencer.setSequence(song);
sequencer.open();
sequencer.addMetaEventListener(this);
sequencer.start();
} catch (InvalidMidiDataException e) {
System.out.println("Bad midi file: "
+ filename);
System.exit(1);
} catch (MidiUnavailableException e) {
System.out.println("No sequencer available");
System.exit(1);
} catch (IOException e) {
System.out.println("Could not read: "
+ filename);
System.exit(1);
}
displayMidiInfo(filename);
} else {
updateTempoFactor(speed);
}
}

最佳答案

您似乎没有检查您尝试获取的资源是否返回非空值。具体来说:

song = MidiSystem.getSequence(
getClass().getResource(filename));

导致了这个特殊问题。可能还有一个更深层次的问题,那就是除非 filename 表示的资源实际上位于 jar 中、类路径上或与 jar 文件 getResource()< 位于同一目录中 不会找到它。如果您尝试访问通用文件系统中任何位置的文件(而不是 jar 文件中),那么您应该使用 File:

song = MidiSystem.getSequence(new File(filename));

关于java - 无法从 jar 读取输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4029653/

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