title: How to change Master Volume with Java?
body: <p>I'm trying to write a small program in order to change the master volume of a given device. Currently it looks like this</p>
  
  <pre><code>import java.util.*;
  import javax.sound.sampled.*;
  
  public class VolumeControl {
  
      public static void main(String[] args) throws Exception {
          Mixer mixer = findMixer(args[0]);
  
          changeVolume(mixer, Integer.valueOf(args[1]));
      }
  
      private static Mixer findMixer(String name) {
          Mixer.Info mixerInfo = Arrays.stream(AudioSystem.getMixerInfo())
              .filter(info -&gt; name.equals(info.getName()))
              .findFirst()
              .orElseThrow(() -&gt; new IllegalArgumentException(String.format("no mixer with the name '%s' found", name)));
          return AudioSystem.getMixer(mixerInfo);
      }
  
      private static void changeVolume(Mixer mixer, int level) throws LineUnavailableException {
          for(Line.Info info : mixer.getSourceLineInfo()) {
              try(Line line = mixer.getLine(info)) {
                  if (!line.isOpen()) line.open();
  
                  FloatControl control = (FloatControl) line.getControl(FloatControl.Type.MASTER_GAIN);
                  control.setValue(limit(control.getMinimum(), control.getMaximum(), (float) level));
              }
          }
      }
  
      private static float limit(float min, float max, float level) {
          return Math.min(max, Math.max(min, level));
      }
  }
  </code></pre>
  
  <p>When I compile and run this with my device name I always get the following exception</p>
  
  <pre><code>Exception in thread "main" java.lang.IllegalArgumentException: illegal call to open() in interface Clip
          at com.sun.media.sound.DirectAudioDevice$DirectClip.implOpen(Unknown Source)
          at com.sun.media.sound.AbstractDataLine.open(Unknown Source)
          at com.sun.media.sound.AbstractDataLine.open(Unknown Source)
          at VolumeControl.changeVolume(VolumeControl.java:23)
          at VolumeControl.main(VolumeControl.java:9)
  </code></pre>
  
  <p>Am I doing anything wrong? I searched for this error on the internet but didn't found anything useful.</p>
  
  <p>How can I get rid of this exception and/or understand what this actually means?</p>