• Post Reply Bookmark Topic Watch Topic
  • New Topic

BeatBoxFinal Code is not working  RSS feed

 
Borhan Safa
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tried the BeatBoxSaveOnly and it ran perfectly but I don't know why this is not running. I have copied from the Appendix A and ran the MusicServer first and by the same time ran the BeatBoxFinal code. Still it is not executing.

BeatBoxFinal.java


MusicServer.java
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch. What does "Still it is not executing." mean, exactly?

I'm not familiar with any of the code samples you mentioned or posted - they're apparently taken from some book? If so, which one?
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Head First Java, I think.
 
Borhan Safa
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still not executing means once I run the beatboxfinal code, the GUI was not appearing... @Ulf Dittmer

The name of the book is Head First Java 2nd edition.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assuming the server is running so the client is not just sitting there waiting for the socket connection to time out I'd fill the startup method with print statements so you can see where it is getting to before stalling.
I assume it is stalling and not exiting or throwing any exceptions.
 
David Mathis
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know this is an old topic, but if you're trying to learn from this book, and the sample code is broken, it's kind of frustrating. Try setUpMidi(); AFTER buildGUI();. That was the issue for me when I ran into the same issue tonight. The book was written quite a long time ago back in the Java 5 days, so I am about to jump into a Java 7 book. I have enjoyed head first java quite a bit.

So like this:


 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Mathis wrote: . . . I am about to jump into a Java 7 book. . . .
Why? you should now be looking at Java8, which has so many improvements over Java7.

Yes, HFJ is a good book. Are you sure the code is broken? I can find nothing in the errata, and when I downloaded the code it ran all right.

And welcome to the Ranch
 
David Mathis
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It didn't work for me. The GUI would not come up and there seemed to be some lag going on, almost like my machine was going to hang.

JRE/JDK 1.7.0.80 64bit
OSX 10.10.5
Macbook Pro

When I debugged the code it came down to the order of those two methods.

Regarding Java versions to learn, Oracle is still certifying on 7 as well as 8. I figured get cerified on 7 since the OCP books got better ratings on anazon and the OCP prep book is written by the same authors as head first java, and I liked the book so much.

What are the specific improvements in 8?

When I get my OCP for 7 how hard would it be to then get certified in 8?
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Beat Box code from the downloads link worked first time for me with the only change being that I had to take note of the package name when executing it
java AppendixA.BeatBoxFinal Campbell
java AppendixA.BeatBoxServer

rather than
java BeatBoxFinal Campbell
java BeatBoxServer

Please supply more details about where the error is. Did you download the code or copy it from the book?

There are whole books about the difference between Java7 and Java8, e.g. Urma Fusco and Mycroft Java8 in Action (Manning), Horstmann Java SE8 for the Really Impatient (Addison-Wesley), Liquori and Liquori Java8 Pocket Guide O'Reilly. Java8 introduced many new classes, also a mechanism for iteration without loops, and functional features. There is also a means to avoid nulls. I do not know what the exam syllabus is.
I am not convinced it is right to restrict your learning to what will come up in the exam, though your chances of getting a job knowing Java7 are probably still good. I still think Java8 is worth learning now. Otherwise you will simply have to learn it all in six months.
 
David Mathis
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For me, I first copied the code from the book, which didn't work, and then I downloaded the code, which didn't work.

By didn't work, I mean that the code compiled fine but when I ran it, it would just hang and the GUI would not come up. There was no "error".

So I added print statements to the code, specifically inside the methods buildGUI() and setUpMidi(). Two print statements per method, one at the start of the method and one at the end. The code in the book had setUpMidi() running first and I noticed that both of the print statements in the setUpMidi() would print, but only the first print statement in buildGUI() would print. This seemed odd because the previous version of the beatbox "BeatBoxSaveOnly" method buildGUI() ran just fine. When I compared the two methods (the buildGUI() method in BeatBoxFinal and BeatBoxSaveOnly) I noticed that the setUpMidi() was called from the buildGUI() method in the BeatBoxSaveOnly version which differed from the BeatBoxFinal version. Then when I commented out the setUpMidi() and related code in BeatBoxFinal, both print statements I added to buildGUI() printed. This lead me to think that it had something to do with the setUpMidi() running first. I have no idea why this causes the code to fail on my machine, but it does. When I make buildGUI() go first, the code works fine.

Regarding Java8, I will need to maybe reconsider my decision to certify in 7 first. Perhaps it might actually be wise to just work on 8 instead. Trying to be as efficient as possible.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can always learn Java8 and sit the Java7 exam.
I think we would have to see the code you ran, please, to be able to comment on it. As I said, the code I downloaded from the HFJ site compiled and ran first time.

How many different JVMs were you using?
 
David Mathis
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the code I am using, that was not working. Like I said above, if I just change the order of when the two methods are called, all works well. I am thinking this might be machine / jvm specific, but don't know. I am just using JRE7 and that's all.

Regarding Java8, in our multithreaded discussion here, I just realized that the next book I have lined up IS Java8. "Java: A Beginner's Guide, Sixth Edition". I am trying to get the basics right now and then I am going to jump right into a (I'm now thinking 8) OCA prep book. I am thinking "OCA: Oracle Certified Associate Java SE 8 Programmer I Study Guide: Exam 1Z0-808"

 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. That code appears identical to the code I downloaded, except for the absence of a package name. It ran first time, but you have only half the app there. You are missing the MusicServer code.
What happened when you ran that code? In my case I got a complaint
the Beat Box wrote:couldn’t connect - you’ll have to play alone.
That is because there wasn't a music server. Is that the code which you couldn't get to run? It should make no difference which machine you are using the code on, nor which version of JVM as long as it is ≥5.
 
David Mathis
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, it's not identical, I changed one of the components (look at the westPanel) and added a type to the JList. If you look close you will see those changes. However it's the code right out of the book, other than these two changes, and both my slightly updated version and the book version fail to run (as the author intended), for me, unless I change the order of those two methods. And yes, the code I pasted for the client above IS the code that will not run for me (tried it again right before I pasted it here).

Here's the server code I use, which is running when I run the client ;). It's good to ask though, because I am just a stranger off the street that you know nothing about. When I ran the code, I got a swirling rainbow circle where my pointer was, as if my machine was going to hang, or was hanging, and the GUI never showed up. No, errors or messages on the command line, for either the client or server terminals. I must use "kill -9" to get rid of it as ctrl-c from the command line does not work to close the client once it appears to be in this hanging state.

 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You missed out on of the buttons and one of the listener classes, but I can still get your code to run. You also have a different socket number so I couldn't get a connection. But both parts of the code ran. I had to use ctrl‑C to stop the server.
I presume you are using a different JVM for the two classes?
 
David Mathis
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, for the responses, still getting up to speed on Java. Can you please explain what you mean by using a different JVM for the two classes? I basically have two different terminals open. One running the server on port 5000 and one running the client.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, two different terminals. You need one for the server and one for the GUI. Actually two for each GUI then you can pass beat patterns back and forth between the two GUIs. So that makes three. You start the server first.

So that isn't the cause of your problem. Please show used the instructions you use to start the app, which terminal they are on (term1, term2, etc) and which order they are in. Also what error messages you get.
Remember that the server will show no output on its terminal (unless there is an Exception), and I had to terminate it with ctrl‑C.

I can't think of any other reason why you are getting that problem. I presume your Java® installation is all right. The downloads seem to use Windows‑style line ends (\r\n) but I don't think that is any cause of the problem.

Anybody else got any ideas?
 
David Mathis
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, and yeah, I was running more than one client for my testing, but was trying to keep it simple for the sake of this forum.

Terminal 1: java MusicServer
Terminal 2: java BeatBoxFinal <name1>
Terminal 3: java BeatBoxFinal <name2>

There's no GUI for the server part (Terminal 1), just a bare bones simple server listening on my specified IP / Port.

Then for Terminal 2 and 3, there are two GUIs up where I can type back and forth and load tracks for each message by clicking on the messages.

All can be ended with ctrl+c.

Everything works flawlessly as long as the client code loads the methods in this order. buildGUI() and then setUpMidi(). lol

I wonder if this is what thew original op was experiencing?
 
David Mathis
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I just uninstalled JRE/JDK 7 and installed 8 and I can no longer reproduce the issue. Go figure.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you now get it to run with those two method calls in the order in the book?
 
David Mathis
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. It now runs as expected regardless of the order, now that i am using version 8. Seems like version 7 OR my installation may have been corrupted?
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!