Make the speaker beep in Java

The first example simply beeps the speaker through an ASCII code:

public class Main {
   public static void main(String args[]) {
      System.out.print("7");
      System.out.flush();
   }
}

The second example makes use of the AWT Toolkit’s method beep. Note that even though we only use the AWT to make a beep, it automatically creates a non-daemon thread that keeps the program alive when it reaches the end of the main method. This is why there is a call to System.exit.

import java.awt.*;
 
public class beep {
   public static void main(String args[]) {
      Toolkit.getDefaultToolkit().beep();     
      System.exit(1);
   }
}