0

Why didn't the SourceDataLine work when I export my project into JAR file?

I am working on a project to cope with AUDIO-DATA. Receive BYTE Stream in PCM format and transform these binary streams into the sound that can be heard. I use javax.sound.sampled.SourceDataLine to realize my requirements.

When I'm debugging the program in Eclipse, everthing goes well,but when I package all the codes and libs into a JAR file (Exactly, it's a RUNNABLE one which can run directly when double-clicked). I can not hear the sound. The data streams are received in a right way but the javax.sound.sampled.SourceDataLine doesn't work.

(My program interface is based on AWT (not swing),but I don't think this has anything to do with my problem)

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
MR.LANG
  • 35
  • 6
  • 1) For better help sooner, post an [SSCCE](http://sscce.org/). 2) Why AWT rather than Swing? See this answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. If you need to support older AWT based APIs, see [Mixing Heavyweight and Lightweight Components](http://www.oracle.com/technetwork/articles/java/mixing-components-433992.html). – Andrew Thompson Dec 13 '13 at 08:34
  • BTW - to get error and exception output, be sure to run the Jar from the command line using `java -jar the.jar`. – Andrew Thompson Dec 13 '13 at 08:40
  • Thanks a lot.It works when I command it on java -jar;But I am quitely confused why the excutable jar file exported by Eclipse should lead to my above problem,now that it is a Runnable one,then I can double-click it and run,but it just didn't work as well as expected.I wonder if this is a BUG on Eclipse? – MR.LANG Dec 13 '13 at 13:50
  • *"It works when I command it on java -jar;"* What?!? I did not expect that at all. Does your app. even appear on-screen when you double click it? – Andrew Thompson Dec 13 '13 at 13:52
  • 1
    I am terrible sorry not to explain Why AWT rather than Swing.Because I am just doing some basic tests for the main function(Audio-Data Handling).To design the interface is not my task,so I choose AWT just for convenience.And I do believe SWING is much better than AWT – MR.LANG Dec 13 '13 at 14:02
  • *"I choose AWT just for convenience"* Huh. That is probably the *only good* reason for using AWT I've ever heard. Good one! – Andrew Thompson Dec 13 '13 at 14:04
  • Uh...Ok,in a better fact,I am comparatively more familiar with AWT so I use it.And "Does your app. even appear on-screen when you double click it?" Yes,it runs well when I double-click it,the interface shows up, but the SourceDataLine doesn't work.So there may be something wrong when the Eclipse exports a Runnable(Excutable) jar file. – MR.LANG Dec 13 '13 at 14:23
  • *"there may be something wrong when the Eclipse exports a Runnable(Excutable) jar file."* Running it from the command line successfully - suggests otherwise to me. – Andrew Thompson Dec 13 '13 at 14:24

1 Answers1

0

There are quite a few differences between the Eclipse environment and a jar file. In order to answer your question, we will need to know what your error messages you might be getting.

Common problem: file locations may not work because the OS file system does not apply to the inside of a jar. In this case, URL's are a better way to identify the resource file. There are also ways in which specifying the location of the resource can go awry, pertaining to how the relative vs absolute addressing is used. Some people also run into the mark/reset error when attempting to load a resource into an InputStream.

However, you are saying that the data IS getting loaded properly, but only the SourceDataLine is not working? I haven't run into those sorts of problems and I use Eclipse to develop programs with sound output frequently. Could you show some code, and let us know the OS environment? (For example, with some flavors of Linux, there can be contention problems over the output sound line.)

Also, without seeing anything, it is entirely possible that you have not set up the errors/exceptions to tell you if the input is failing in the jar form, and the output exists and consists of silence.

Phil Freihofner
  • 7,645
  • 1
  • 20
  • 41