'Issues with javax.sound.midi with MidiUnavailableException and IllegalArgumentException

I've been digging around to find a solution to this problem for a while and I'm losing it a little. I've got a basic program that should play a middle C for 1 second:

import javax.sound.midi.*;

public class PracticeMidi {

    public static void main(String[] args) { 

        try{
            
            Synthesizer midiSynth = MidiSystem.getSynthesizer(); 
            midiSynth.open();
        
            //get and load default instrument and channel lists
            Instrument[] instr = midiSynth.getDefaultSoundbank().getInstruments();
            MidiChannel[] mChannels = midiSynth.getChannels();
            
            midiSynth.loadInstrument(instr[0]);//load an instrument
        
            mChannels[0].noteOn(60, 100);//On channel 0, play note number 60 with velocity 100 

            try { Thread.sleep(1000); // wait time in milliseconds to control duration
            } catch( InterruptedException e ) {
                e.printStackTrace();
            }
            mChannels[0].noteOff(60);//turn off the note                
        
        } catch (MidiUnavailableException e) {
            e.printStackTrace();
        }
    }

}

However, I get the following error when trying to compile from command line:

javax.sound.midi.MidiUnavailableException: Can not open line
        at java.desktop/com.sun.media.sound.SoftSynthesizer.open(SoftSynthesizer.java:1180)
        at java.desktop/com.sun.media.sound.SoftSynthesizer.open(SoftSynthesizer.java:1090)
        at PracticeMidi.main(PracticeMidi.java:11)
Caused by: java.lang.IllegalArgumentException: No line matching interface SourceDataLine supporting format PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian is supported.
        at java.desktop/javax.sound.sampled.AudioSystem.getLine(AudioSystem.java:425)
        at java.desktop/javax.sound.sampled.AudioSystem.getSourceDataLine(AudioSystem.java:534)
        at java.desktop/com.sun.media.sound.SoftSynthesizer.open(SoftSynthesizer.java:1120)
        ... 2 more

I've tried all kinds of things such as reinstalling my jdk, downloading an external midi package and including it (it broke the program saying the import came from 2 places), and just trying all kinds of different syntax for the actual program. All to no avail. I also tried running using sudo to run but this didn't work either.

I'm using windows 11 alongside a linux terminal (MobaXterm) to compile and run. My main program (for which this snippet is a practice for) requires javafx so I'm using jdk-11.0.14.

Any help would be greatly appreciated.

Thanks!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source