Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Can't stop the music  RSS feed

 
John Daniel
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm about as much of a Java beginner as they come. I'm trying to learn Java by reading Kathy Sierra's Head First Java (2nd E.) book. Anyway, I have typed in a sample program of her's that simply plays one note using Java's sound.midi package. It plays the note just fine, but the program doesn't end (that's the program that doesn't end, not the note). In order to end the program, I have to hit Cntl-C. I've pasted the program below. I'm running Java 5.0 on an XP SP2 machine.

Also, on a completely unrelated topic . . .
In all of Kathy's examples, she prefixes both javac and java with a percent-sign(%). When I try that, I get an error message about "%", or when I tried it without a space "%javac", "is not recognized yadayadayada". When I leave off the %, it works fine. What is it that % is suspposed to do?

Here's the note program that won't end. It's on page 342 in chapter 11, if you happen to have a copy of her book.


[ March 27, 2005: Message edited by: JR Daniel ]
 
K Riaz
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The program was probably not designed as a full-fledge application, rather just to teach you about playing audio files. If you want it to close, add "System.exit(1);" at the end of the main() method. This will cause the Java VM to close. I wouldn't worry too much about this.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a close look at Sequencer in the JavaDoc. Do you have to stop it? Maybe it's waiting around forever for something to happen.
 
M Beck
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the percent sign is a unixism - in many unix-like operating systems, the prompt is a percent sign, or some string ending with a percent sign. dollar signs are also common, either on their own or at the end of some string. think of them as unix's equivalent of windows' "C:\>" prompt.

so no, you're not expected to type in the percent sign; it's just there to remind you that everything after it is supposed to be typed in at a command prompt.
 
John Daniel
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you.
 
Mark Knutsen
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Kashif Riaz:
The program was probably not designed as a full-fledge application, rather just to teach you about playing audio files. If you want it to close, add "System.exit(1);" at the end of the main() method. This will cause the Java VM to close. I wouldn't worry too much about this.

I'm not worried, but I would like to know why it doesn't exit without an explicit System.exit(1). Can anyone explain?
 
Marilyn de Queiroz
Sheriff
Posts: 9080
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe because you have
player.start();
but you never call
player.stop();
 
Mark Knutsen
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Marilyn de Queiroz:
Maybe because you have
player.start();
but you never call
player.stop();

Thanks, that's worth a try. However,

Inserting a delay loop and then "player.stop();" at the end of the try{} block does in fact stop the sequencer (I can hear it cut off the note prematurely), but it doesn't allow the program to exit.

Can anyone provide more insight?
 
Stephen Boston
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mark Knutsen:

Thanks, that's worth a try. However,

Inserting a delay loop and then "player.stop();" at the end of the try{} block does in fact stop the sequencer (I can hear it cut off the note prematurely), but it doesn't allow the program to exit.

Can anyone provide more insight?


The System.exit method forces termination of all threads in the Java virtual machine.
Without that your console thread won't end even if it does not have any furthers instructions to run.

GUI apps use a different method to exit.
 
Mark Knutsen
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stephen Boston:


The System.exit method forces termination of all threads in the Java virtual machine.
Without that your console thread won't end even if it does not have any furthers instructions to run.

GUI apps use a different method to exit.


Hmm; OK, I suppose I'll learn more as I continue to read "Head First Java".

I will also submit an errata entry suggesting that "System.exit(1);" be added to the sample code to avoid confusion.
 
Stephen Boston
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mark Knutsen:


Hmm; OK, I suppose I'll learn more as I continue to read "Head First Java".

I will also submit an errata entry suggesting that "System.exit(1);" be added to the sample code to avoid confusion.


I'm fairly new to Java as well.


But this code example puzzles me.


Why isn't System.exit(0); required here? Is it because the main method and the Hello method don't start a new thread like the MiniMiniMusicApp?
 
Marilyn de Queiroz
Sheriff
Posts: 9080
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Normally you don't need System.exit except for a GUI. I'm not familiar, however, with midi sequencer stuff, so I'm not quite sure why it would be needed here.
 
Mark Knutsen
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Marilyn de Queiroz:
Normally you don't need System.exit except for a GUI. I'm not familiar, however, with midi sequencer stuff, so I'm not quite sure why it would be needed here.

If you insert a print statement just before the end of main(), it will display before the note is finished playing. It's clear that the sequencer is running in a separate thread of execution, so maybe that has something to do with it.

I don't know enough about Java to explicitly destroy the sequencer, but I guess that wouldn't be appropriate for this point in the book, either.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!