• Post Reply Bookmark Topic Watch Topic
  • New Topic

command prompt 'hangs' after executing a Java program  RSS feed

 
Allan Kiplagat
Greenhorn
Posts: 11
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. I'm learning Java using Head First Java 2nd Edition. So far, I have been compiling and running my programs smoothly through command prompt.
I'm doing a lesson on the javax.sound.midi package and I've written and compiled a simple class that's plays a single sound note. The program runs, plays the note, but then command prompt doesn't resume it's normal command-awaiting state. It doesn't accept keystrokes and I have to close and restart it.

What's up?

I've attached the command prompt screenshot showing compiling, running and the 'hanging'. I put a "Main method complete" output at the end of the main method and it is displayed, so I'm sure the code is okay (but I've included the code just in case).

PS: I'm in chapter 11, so keep responses simple. There are no dumb questions!

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.

C:\Users\Allan>cd documents

C:\Users\Allan\Documents>cd java

C:\Users\Allan\Documents\Java>cd BBox

C:\Users\Allan\Documents\Java\BBox>javac MiniMusicApp.java

C:\Users\Allan\Documents\Java\BBox>java -classpath . MiniMusicApp
Main method complete.






 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please copy and paste the output as text, not screenshots, which are difficult to read.
What are you doing when catching an Exception? An empty catch is an accident waiting to happen. If anything goes wrong, you never know about it.
Is that program meant to return to the command line or to continue waiting for the next input?
 
Allan Kiplagat
Greenhorn
Posts: 11
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the quick post Campbell. I've copy-pasted the output.

I'll insert handling code for Exceptions. Could that be the problem?

The program should only play the note (does that) and return to the command line (command prompt freezes).
 
Bill Johnston
Ranch Hand
Posts: 201
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My guess is the the player doesn't really stop.
 
William P O'Sullivan
Ranch Hand
Posts: 859
Chrome IBM DB2 Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good guess Bill.

@OP, Is there a close() method on the player?

WP
 
Allan Kiplagat
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice idea, Bill & WP. Lemme check the API for that.
 
Allan Kiplagat
Greenhorn
Posts: 11
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ Campbell: Thanks for the Exception handling tip. I've included a notification method.

You were right Bill & WP. The player has to be closed.

When I first inserted the player.close() method, command prompt worked well but no sound was produced. I figured the player was closed prematurely so I added a delay method (#34) to give enough time for the sequence to be played to completion. Now it works perfectly! Thanks



 
Bill Johnston
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may find that program is incomplete because it comes from the chapter about Exceptions. Which page was it on? If you look on page 650 you find there is a stop() method on sequencers, so there might be a close() or stop() method on the player. I Haven’t looked.
And thank you for posting the actual text (in the new version of the original post).
 
Allan Kiplagat
Greenhorn
Posts: 11
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Campbell, the code on page 342 is incomplete. Inserting a close() method solves the problem.

You were right Bill & WP. The player has to be closed.

When I first inserted the player.close() method, command prompt worked well but no sound was produced. I figured the player was closed prematurely so I added a delay method (#34) to give enough time for the sequence to be played to completion. Now it works perfectly! Thanks

 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Allan Kiplagat wrote:I figured the player was closed prematurely so I added a delay method (#34) to give enough time for the sequence to be played to completion. Now it works perfectly!

Until your sound is longer than 5 seconds. It's better to add a listener to be notified when the sound is actually done playing. I think a ControllerEventListener is the right one but you'll have to try it yourself.
 
Allan Kiplagat
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neat idea Rob. Thanks. I'll do that.
 
William P O'Sullivan
Ranch Hand
Posts: 859
Chrome IBM DB2 Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not simply look at the player API and instead of guessing how long the player runs, do this:



Output:

Player isRunning: true
Player isRunning: false
Main method complete.


Sometimes the correct solution is staring us right in the face.

WP
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don’t like that empty while (...){}. It looks like a waste of CPU cycles looking for something not to do (like in the song from “Connecticut Yankee at King Arthur’s Court”).
 
Allan Kiplagat
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks WP, using the while loop is a simple solution. Campbell does, however, have point with the cycle waste. I'll do more studying and add a listener to be notified when the player completes the sequence. Thanks Campbell.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will have to research the listener Rob mentioned earlier. Have never used it myself.
 
Allan Kiplagat
Greenhorn
Posts: 11
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The listener works. The player fires a ControllerEvent when it plays an event-triggering MidiEvent. I put a trigger in the track at the exact beat position of the noteOff MidiEvent. So when the note stops playing, the player fires the event, invoking the event-handling method which closes the player itself. I have left the initial step numbering unchanged and marked the changes with /*EXTRA STEP*/ . Thanks Bill, WP, Rob and Campbell.

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Allan Kiplagat wrote: The listener works...

But your code is hard to read. I've split your very long comment lines.

Have another look at the UseCodeTags page (admittedly recently updated).

Winston
 
Allan Kiplagat
Greenhorn
Posts: 11
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Heh, thanks Winston.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!