jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes Need Help With Compiling Head First Java Code Examples Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need Help With Compiling Head First Java Code Examples " Watch "Need Help With Compiling Head First Java Code Examples " New topic
Author

Need Help With Compiling Head First Java Code Examples

Patrick Jones
Greenhorn

Joined: May 26, 2009
Posts: 20
I am reading Head First Java Second Edition and I am trying to actually do the examples as I go. The book is great but is oddly unclear on whether the examples are ones we're supposed to code or not. I would like to code the examples as I go as I learn best that way. I successfully followed the instructions for installing the JRE and JDK and have been compiling and running the simple initial examples via javac on the command line.

The confusion for me is how I am to organize the examples on page 73 and after (the code is at the end of this post). Either I missed it, or the book doesn't really explain how the two parts of code are supposed to be integrated for compiling, or if not, how you compile the two classes from the command line. The authors advise the reader to not use any of the IDE's out there, which leaves me the command line and a text editor.

Are the code sections of the book supposed to be just read and not performed? Is this really a textbook and the authors assume a teacher is hovering around to explain the actual compiling and running?

Thanks for your help!

Patrick

-----------------------------------------------------------------------------------

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
Welcome to JavaRanch

Please look for the CODE button; I shall edit your posting so you can see how much better it looks.

There are a few examples in Head First which are incomplete, but that Dog class appears to be complete.

You compile that in the usual fashion.
  • Navigate to your folder where you keep your Java work.
  • Open an editor program (by the way Notepad is very poor for programming).
  • Save the text.
  • Compile with the javac tool.
  • Realise you have twice written dog when you meant Dog.
  • Correct those misspellings and save and javac again.
  • Execute with java DogTestDrive
  • Voila!
  • [Campbell@localhost java]$ gedit DogTestDrive.java
    [Campbell@localhost java]$ javac DogTestDrive.java
    DogTestDrive.java:4: cannot find symbol
    symbol : class dog
    location: class DogTestDrive
    Dog one = new dog();
    ^
    DogTestDrive.java:8: cannot find symbol
    symbol : class dog
    location: class DogTestDrive
    Dog three = new dog();
    ^
    2 errors
    [Campbell@localhost java]$ gedit DogTestDrive.java
    [Campbell@localhost java]$ javac DogTestDrive.java
    [Campbell@localhost java]$ java DogTestDrive
    Wooof! Wooof!
    Yip! Yip!
    Yip! Yip!
    [Campbell@localhost java]$
    Patrick Jones
    Greenhorn

    Joined: May 26, 2009
    Posts: 20
    Bartender,

    I'll take that whiskey straight up, by the way. Make it two. And by the way, do you have any twizzlers?

    Okay, I'm thinking I'm not understanding one of those It's-So-Obvious-It's-Assumed, and I'm sure the fault is mine.

    I compiled the one file containing all the text above at the command line, and produced 56 errors.

    I'm suspecting the code should be in two files in a single folder, or something similar, as I think I remember seeing something like that in Actionscript 3.0. I could paste the errors in but I can't seem to be able to select text from the command line console (cmd.exe) in Vista....at any rate I'm pretty sure my mistake is so obvious that such an act would be unnecessary. My foggy brain is working with some kind of concept of a package or something of different files (a main class file and individual files for each of the other custom classes?) that the compiler roots through and assembles into other file(s) which can be run by the 'java' command at the command line.

    If I can get through this sticking spot I'll be back in the saddle and fixing fence again, I promise.

    Patrick

    W. Joe Smith
    Ranch Hand

    Joined: Feb 10, 2009
    Posts: 710
    Not sure if you copied it directly or not, but shouldn't one class be declared public?

    EDIT: I compiled the class, and I noticed something that Campbell said:

    Realise you have twice written dog when you meant Dog.

    Now, I'm not trying to hint at anything, but the code above might be on the right track for another compilation test if you look it over with that in mind.


    SCJA
    When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
    Patrick Jones
    Greenhorn

    Joined: May 26, 2009
    Posts: 20
    Sheriff Smith,

    You're right...I think the first one should be declared public.

    Should I be naming the file itself in a certain way relative to one of the class names?

    After adding the public to the main class it still made a ton of errors.

    Hmmm. I saved this file as DogTestDriveTwo.java and typed

    java DogTestDriveTwo.class

    into the command line, while in the directory the file resides in.

    Hmmm.
    ---------------------------------------------------------------

    Patrick
    Patrick Jones
    Greenhorn

    Joined: May 26, 2009
    Posts: 20
    Well, well, well. Looks like I have a clue here. I found a passage at the beginning of the book .

    Here it is:

    "We don't worry about organizing and packaging your own code until the end of the book. In this book, you can get on with the business of learning java, without stressing over some of the organizational or administrative details of developing Java programs. You will, in the real world, need to know--and use--these details, so we cover them in depth. But we save them for the end of the book (chaper 17). Relax while you ease into Java, gently."

    Are they kidding? Read 579 pages before writing a single line of code? I appreciate their style, but I never imagined they thought coding is best started with nearly 600 pages of reading! The fact that they provide the sample code for download indicates they themselves are badly confused on their approach. Perhaps this the classic case of a college textbook offered to the public as a complete course. I imagine this book is read by legions of high school and college students who have a tutor or lab assistant to help them actually write the code.

    They tout their research into learning effectiveness...did their research also indicate reading and writing no code is the way to best educate?

    If nothing else, the thrill of seeing a single application of ones own is probably a good educational motivator...that is, a single application before weeks go by.

    Amazing.

    I probably should hunt for a better book...one that bothers with the actual implementation aspects. Their chapter 17 is more of an overview than anything useful. They also refer to a site called wickedlysmart.com...looks like it was mostly abandoned some time back. You'd think they'd put at least as much effort into a site mentioned by tens of thousands of printed books as we do into our blogs. The first words of the landing page, at the time of this post, are: "It's 2006. Learn like it."

    I'm still eager for any help compiling this sample application.

    It looks like I should split this code into two files, then compile out of that directory into another, but I'm not sure.

    Patrick

    W. Joe Smith
    Ranch Hand

    Joined: Feb 10, 2009
    Posts: 710
    Patrick Jones wrote:Sheriff Smith,

    I saved this file as DogTestDriveTwo.java and typed

    java DogTestDriveTwo.class



    I'm sure you meant to say:

    I typed javac DogTestDriveTwo.java, then to run the class I typed java DogTestDriveTwo



    What I typed compiles the class. What you typed...well, I'm not sure it does anything.

    As far as the book, don't give up hope. Work out some code examples on your own, come here and ask for advice, and keep going. I read the whole book myself, and am working through it again, and it does contain some wonderful pearls of wisdom.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38033
        
      22
    Patrick Jones wrote:Bartender,

    I'll take that whiskey straight up, by the way.
    More likely that you will be buying us the drinks

    Joe Smith has pointed out one error, which lots of people make, appending .class unnecessarily. I agree, it is confusing until you get used to it.

    If you declare a class public, then the file it is in has to have the same name as the class (with a .java extension); this also means you can't get two top-level public classes in the same file.
    The bit about page 579 probably refers to package declarations, which are by no means a beginner's topic. I don't have the book myself, but I believe most of its examples will compile and run. There are a few "incomplete" examples at the beginning of the book.
    I only got two errors, both relating to Dog being spelt dog. that was without changing the file structure. It would be better style, however, to have the Dog class public and in a file of its own.
    To copy text from the command line: click the icon at the top left, click the edit link, then mark, then highlight text with the mouse, then enter. On other operating systems it is a lot easier, using highlight---->ctrl-shift-C.

    Take it easy. You'll get there in the end.
    Patrick Jones
    Greenhorn

    Joined: May 26, 2009
    Posts: 20
    Awesome...thanks!

    I did mean to say javac DogTestDriveTwo.java...

    I'd like to shoot a western starring kids so that they could sidle up to the bar and ask for a shot of banana milk. My friend across the street and I were obsessed with Raman noodles. We were also obsessed with banana milk, which was something we'd make with, I think, bananas, milk, and honey in the blender. A cheap buzz for 5 year olds, if you could find the bananas.

    After we'd lay the banana peels on the floor, slippery side down, and hop on them from a running start. You could get a good 8 feet of travel out of them, if I remember right.

    As you can see, growing up in the west was rough stuff, but we managed.

    Thanks for your encouragement. The WWW: a place for people like me to whine to 5 billion humans. The book is actually quite good. I think I'll forget all about my frustrations after a couple more weeks.

    Just because I need a little more tuna helper than most: can you describe the sequence for me with the command line instructions? I'm on Vista home, if that matters. I think I should split the file into two. I'm a little confused which should be the public one...or if I make two files are they then both public? It may be helpful just to see what I should have in each at this point, and their proper file names. Do I put both in the same directory, navigate to it on the command line, then run javac on one, and which one? Should I be getting into the habit of using the compile sequence mentioned in Chapter 17, where you have a source directory file and compile into another directory? I hope I can get the exact command right in that case.

    I noticed that books refer to %javac but I've just been typing javac...does this matter?

    Thanks for all your help. I'm trying to use the command line for now so I understand what's going on and don't get addicted to NetBeans or some similar tool right now.

    Plus, I think it's cool to hop onto someone's computer and write an application from their command line!!!

    Patrick



    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38033
        
      22
    You will need more than banana skins to catch us experienced ranchers out. You need to be called Rob or Joanne.

    People all tend to learn in different ways and some people like book "A" and others like book "B".

    I would prefer to have both Dog and the DogTestDrive classes declared as public, in which case you need two files with the same names as the classes plus .java. Most compilers will complain bitterly if there is the tiniest difference in spellling between the file name and the class name.
    Look at the shell printout I posted yesterday. Apart from your not having gedit on Windows Vista, what you write after the prompt would be identical. You don't write %; I think that % symbol might denote the prompt.

    About gedit: it should be obvious it is a text editor. Don't use Notepad. Try Notepad2 or Notepad++ (despite the names they are not at all similar to Windows Notepad, but they are similar to each other) or jEdit as a text editor. They are much better for programming; they support bracket highlighting, indentation and syntax colouring.
    Sachin Adat
    Ranch Hand

    Joined: Sep 03, 2007
    Posts: 213
    Patrick Jones wrote:
    Just because I need a little more tuna helper than most: can you describe the sequence for me with the command line instructions? I'm on Vista home, if that matters. I think I should split the file into two. I'm a little confused which should be the public one...or if I make two files are they then both public? It may be helpful just to see what I should have in each at this point, and their proper file names. Do I put both in the same directory, navigate to it on the command line, then run javac on one, and which one? Should I be getting into the habit of using the compile sequence mentioned in Chapter 17, where you have a source directory file and compile into another directory? I hope I can get the exact command right in that case.

    Split the file into two. (Already suggested by Campbell)
    Make both of them public. (Already suggested by Campbell)
    Put both in the same directory (You can learn about packages afterwards - Already suggested by Campbell)
    Check that the name of the class and the file name are exactly the same, case wise also.(Already suggested by Campbell)
    run javac DogTestDriveTwo.java (this automatically compiles class Dog too)
    Then run the compiled code - java DogTestDriveTwo (don't append .class here)

    You said you already have compiled and run small examples, so the last 2 steps shouldn't be a problem.............

    Getting into the habit of using the compile sequence mentioned in Chapter 17 is good. But I don't think you have reached Chapter 17 yet. Don't jump into chapters. Just take one chapter at a time and go by the order as in the book. The authors must have had something in mind, to keep things in Ch 17 and not in Ch 1.

    Don't think I've added anything new in the post, but I felt you were not getting it that clearly
    Hope you've got the sequence now.......

    Don't give up on the book, its a very good one indeed...... and do tell us about your progress


    SCJP 6
    How To Ask Questions On Java Ranch - How To Answer Questions On Java Ranch
    Patrick Jones
    Greenhorn

    Joined: May 26, 2009
    Posts: 20
    Howdy,

    Sorry to be out of touch...I dove back into the book, largely on all your support. After browsing other books during my freakout last week, I realize what a fantastic book this is. It really takes the time to explain some of the core concepts, which the other books don't.

    I backed up and started again and am methodically reading through, as all you suggested. No doubt I am pretty excited to get through to the examples projects toward the back of the book. I'm going to be tucking into the book again today, so expect some questions!

    I suppose once I get this code to run I'll be starting new threads with new questions, right? I assume that's the protocol. This time I'll try to post the code up right in the correct format!

    I want to say this:

    I can't believe how supportive you all are. I looked at the O'Reilly courses and the Sun courses and they are all very expensive. So you are my school! In fact, as you know, you are better than those schools! So thanks very, very much for your help and encouragement. It feels good to know that you are looking out for me!! I will do the same for others when I am up to speed.

    I'm totally determined, so I'm going to continue through this book. I can't wait to get the basics down...then I'll loop back into some of the web language basics so I can whip out web pages (notice the enthusiastic "whip") before returning to Java. I'll be very interested in some of the new 'Touch Table' capabilities of Java. Check out 'Touch Table' and 'Touchtable' on YouTube if you haven't already...very cool stuff!

    Well, into the book I go...expect to hear from me soon as I become reflabbergasted!

    I apologize for introducing that last word into the world.

    Patrick

    Patrick Jones
    Greenhorn

    Joined: May 26, 2009
    Posts: 20
    Got the code compiled last week, thanks to all of you...so that wraps out this thread.

    Thanks again...

    Patrick

    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38033
        
      22
    Well done
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Need Help With Compiling Head First Java Code Examples
     
    Similar Threads
    Quick question about instance variables
    Head First GoodDog code error
    Two classes defined in the same source file ??
    working with book, code compiiles but won't run
    Doubt about private modifier