Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8236159

[gtk] Remove "Failed in XOpenDisplay" warning message (JDK-8097484)

XMLWordPrintable

      ADDITIONAL SYSTEM INFORMATION :
      Linux 64
      # javac -version
      openjdk version "1.8.0_232"
      # java -version
      OpenJDK Runtime Environment (build 1.8.0_232-b09)
      OpenJDK 64-Bit Server VM (build 25.232-b09, mixed mode)


      A DESCRIPTION OF THE PROBLEM :
      When running a sound javax application that does not use X11, it shows the error message "XOpenDisplay failed" because the DISPLAY environment variable is set for a host that does not exist. Also, the system variable [ java.lang.System.setProperty("java.awt.headless", "true")] is set to don't start graphics.

      If the DISPLAY environment variable is not set or empty, the error message is not displayed.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :

      #export DISPLAY="THIS HOST DOES NOT EXIST"
      #java -cp test/ testSound

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      PLAY A SOUND BEEP
      ACTUAL -
      XOpenDisplay() failed
      XOpenDisplay() failed
      XOpenDisplay() failed
      XOpenDisplay() failed
      XOpenDisplay() failed
      XOpenDisplay() failed
      PLAY A SOUND BEEP

      ---------- BEGIN SOURCE ----------
      import javax.sound.sampled.AudioFormat;
      import javax.sound.sampled.AudioSystem;
      import javax.sound.sampled.LineUnavailableException;
      import javax.sound.sampled.SourceDataLine;

      public class testSound {
        public static float SAMPLE_RATE = 22050f;
        private static final int BITS_PER_SAMPLE = 16; // 16-bit audio
        
        private static final double MAX_16_BIT = Short.MAX_VALUE; // 32,767
        private static final int MONO = 1;
        private static final int STEREO = 2;

        private static final boolean LITTLE_ENDIAN = false;
        private static final boolean BIG_ENDIAN = true;
        private static final boolean UNSIGNED = false;
        private static final boolean SIGNED = true;
        
        
        public static void main(String[] args){
      // The jvm should not start the graphics engine because of this variable
          java.lang.System.setProperty("java.awt.headless", "true");
          tone(note(1000,365,1));
        }
          

        public static void tone(byte[] buffer){
          AudioFormat audioFormat=new AudioFormat(SAMPLE_RATE, BITS_PER_SAMPLE, MONO, SIGNED, LITTLE_ENDIAN);

          try{
      // THE MESSAGE ERROR APPEARS AFTER INITIATE THIS CLASS
              SourceDataLine sourceDataLine = AudioSystem.getSourceDataLine(audioFormat);
              sourceDataLine.open(audioFormat);
              sourceDataLine.start();
              sourceDataLine.write(buffer,0,buffer.length);
              
              sourceDataLine.drain();
              sourceDataLine.stop();
          }catch(LineUnavailableException ex){
            System.out.println("LineUnavailableException: "+ex.getMessage());
          }catch(IllegalArgumentException ex){
            System.out.println("IllegalArgumentException: "+ex.getMessage());
          }
        }
        
        public static byte[] note(double hertz, double duration, double volume){
          int n = (int) (SAMPLE_RATE * duration / 1000d);
          double[] angle = new double[n+1];
          byte[] buffer=new byte[angle.length*2];
          for (int i = 0; i <= n; i++){
            angle[i] = volume * Math.sin(2 * Math.PI * i * hertz / SAMPLE_RATE);
            if (angle[i] < -1.0) angle[i] = -1.0;
            if (angle[i] > +1.0) angle[i] = +1.0;
            // convert to bytes
            short s = (short) (MAX_16_BIT * angle[i]);
            buffer[i*2] = (byte) s;
            buffer[i*2+1] = (byte) (s >> 8); // little endian
          }
          return buffer;
        }
          
          
          
        
        
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      The workaround should be based on the following variable.

          java.lang.System.setProperty("java.awt.headless", "true");


            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated: