Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does Java Sound behave differently when I run from a .jar?
    primarykey
    data
    text
    <p>The <code>play</code> method below is from a class which, upon instantiation, reads a .wav file into a byte array called <code>data</code>, and stores the sound format in an <code>AudioFormat</code> object called <code>format</code>.</p> <p>I have a program that calls <code>play</code> from a <code>java.util.Timer</code>. When I go into the folder with all the relevant .class files and I run the program using the command <code>java MainClass</code>, everything works as expected. However, when I put all the .class files in an executable .jar and run the program using the command <code>java -jar MyProgram.jar</code>, sounds played using the <code>play</code> method are cut off after something like 50 to 150 ms.</p> <pre><code>public void play() throws LineUnavailableException { final Clip clip = (Clip)AudioSystem.getLine(new DataLine.Info(Clip.class, format)); clip.open(format, data, 0, data.length); new Thread() { public void run() { clip.start(); try { Thread.sleep(300); // all sounds are less than 300 ms long } catch (InterruptedException ex) { /* i know, i know... */ } clip.close(); } }.start(); } </code></pre> <p>A few comments:</p> <ul> <li><p>I've tried increasing the sleep time in the <code>play</code> method up to 1000 ms, with no change in behavior.</p></li> <li><p>Timing the <code>Thread.sleep</code> using <code>System.nanoTime</code> confirms that the thread is sleeping exactly as long as expected.</p></li> <li><p>Since the sound file to be played is pre-loaded into memory, I don't think the act of extracting the sound resource from the .jar can be causing the problem.</p></li> <li><p>I've tried running the program from both inside and outside the jar using the memory pool size options <code>-Xms2m</code> and <code>-Xmx64m</code> (separately), with no change in behavior.</p></li> </ul> <p>I'm running OpenJDK Java 6 on Ubuntu 11.04. Any idea what's going on?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload