Another solution, Windows dependent is to use JNA and call directly Windows Beep function, available in kernel32. Unfortunately Kernel32 in JNA doesn't provide this method in 4.2.1, but you can easily extend it.
public interface Kernel32 extends com.sun.jna.platform.win32.Kernel32 {
/**
* Generates simple tones on the speaker. The function is synchronous;
* it performs an alertable wait and does not return control to its caller until the sound finishes.
*
* @param dwFreq : The frequency of the sound, in hertz. This parameter must be in the range 37 through 32,767 (0x25 through 0x7FFF).
* @param dwDuration : The duration of the sound, in milliseconds.
*/
public abstract void Beep(int dwFreq, int dwDuration);
}
To use it :
static public void main(String... args) throws Exception {
Kernel32 kernel32 = (Kernel32) Native.loadLibrary("kernel32", Kernel32.class);
kernel32.Beep(800, 3000);
}
If you use maven you have to add the following dependencies :
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>4.2.1</version>
</dependency>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna-platform</artifactId>
<version>4.2.1</version>
</dependency>