Search code examples
javajava-11java-17

Why can't I import from com.sun.media.sound?


I want to use audio synthesizer and midi classes that are included in the Java JDK. MidiSystem.getSynthesizer() returns a Synthesizer. Debugging reveals that the actual class is SoftSynthesizer. So this test passes:

    @Test
    void defaultSynth() throws MidiUnavailableException {
        Synthesizer synthesizer = MidiSystem.getSynthesizer();
        assertEquals("SoftSynthesizer", synthesizer.getClass().getSimpleName());
    }

But I'm unable to use the class SoftSynthesizer and the interface AudioSynthesizer (which extends Synthesizer, and which SoftSynthesizer implements).

Below, AudioSynthesizer and SoftSynthesizer are commented out, because they do not resolve.

   void foo() {
        // Resolved symbol
        Synthesizer s = null;

        // Unresolved symbols
        //AudioSynthesizer as = null;
        //SoftSynthesizer ss = null;
    }

One difference is that these libraries and interfaces come from different packages. Importing from com.sun.media.sound doesn't work.

import javax.sound.midi.Synthesizer;
//import com.sun.media.sound.AudioSynthesizer;
//import com.sun.media.sound.SoftSynthesizer;

But both packages seem to be included in the JDK, so why does it matter (if it does)?

Tested with Java-11 and Java-17 (Azul), on IntelliJ, MacOS. Using Maven.


Solution

  • most of the internal APIs (since) JDK 9 are inaccessible to code outside the JDK Please see JEP 260: Encapsulate Most Internal APIs for details and potential workarounds.