• Post Reply Bookmark Topic Watch Topic
  • New Topic

Head First Java - chapter 2  RSS feed

 
John Freshman
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone -
I am brand new to java, and have recently purchased "Head First Java" (great book - by the way). In chapter 2, on page 37, I was able to compile the simple class called "Movie" but I was not able to compile the "MovieTestDrive" class. When I tried, it reports that it "cannot find symbol - class Movie"

So I guess my question is simple:
Are the book samples missing some kind of Import or Includes statement at the beginning of the Test Drive code to include the main class file?

Thanks in advance for all the help.
 
Stuart Gray
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unlikely you are missing any classes - sounds more like a classpath related problem. What package are the Movie and MovieTestDrive classes in (is there a package statement at the very top of the code)? What directory are they stored in?
 
John Freshman
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, there are no packages being used in either of these little class tests.

I think it's interesting that I do have both Apache and Tomcat installed and have been successfully compiling servlets for about a week now. I have not needed a single package for any of my testing. I have spent weeks properly configuring all my setups... except for packages (argh!).

I do appreciate your suggestion, but do not understand why a teaching book as good as the Head First series would publish small, simple classes to play with, but omit the fact that they should be used within packages.

All that aside, both these test classes are in the same folder and I am attempting to compile them both from there. The first works, the second called MovieTestDrive does not, assuming because it is not aware of the first.

Here is the code:

(this class compiles)

class Movie {
String title;
String genre;
int rating;

void playit() {
System.out.println("Playing the movie");
}
}


(this one does not)

public class MovieTestDrive {
public static void main(String[] args) {
Movie one = new Movie();
one.title = "Gone along with my brain";
one.playit();

}
}


Thanks in advance for helping me properly set up the package which will allow this to compile and run.

~Mark
 
James Jennings
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both of these classes compile on my machine. And if one compiles, both should assuming they are in the same directory.

The rules of this website require that for your ID that you use your your real name or a name that reads like a real name.
 
L�szl� Kov�cs
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm a total noob so I'm just guessing really, but is the problem that the first class does not have the access specifier 'public'? From what I understand, no access specifier means that the class will have default access, so unless the second class is included in the same package, or extends the first class, it has no access rights.
 
L�szl� Kov�cs
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Having just tested it out, I can see that my guess was wrong. Your classes both compile and run here. I should carry on reading my book and not butt in!

However, you (or someone else reading this) might be interested to know that the first time I tried to compile MovieTestDrive.java I got the exact same error that you did. Then I tried to run some of the other packageless examples that I've made while following the book I'm reading and I got the same error: the reason, in my case was that I had set a classpath environment variable which meant that javac was looking for the class in the wrong folder - not looking in the same folder as the one I compiled the Movie class in.
 
John Freshman
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for at least trying to help, L�szl�.

And James, what is the point of posting useless replies and quoting rules instead? Seems like bureacratic nonsense to me. If all you're going to do is arrogantly quote rules, then the book AND the site are meaningless.
 
John Freshman
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by L�szl� Kov�cs:
Having just tested it out, I can see that my guess was wrong. Your classes both compile and run here. I should carry on reading my book and not butt in!

However, you (or someone else reading this) might be interested to know that the first time I tried to compile MovieTestDrive.java I got the exact same error that you did. Then I tried to run some of the other packageless examples that I've made while following the book I'm reading and I got the same error: the reason, in my case was that I had set a classpath environment variable which meant that javac was looking for the class in the wrong folder - not looking in the same folder as the one I compiled the Movie class in.



L�szl� -
Thank you for your help!
My CLASSPATH was jacked-up... I am now able to compile.

To others, my name has been changed to comply with site "rules." (By the way, the single most promenent rule that was presented to me when posting was "be nice." Hmmmm.)
 
James Jennings
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John,
That is an appropriate name. It appears that "nice" is in the eyes of the beholder.

Have a nice day.
 
Marilyn de Queiroz
Sheriff
Posts: 9082
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by John Freshman:
And James, what is the point of posting useless replies and quoting rules instead? Seems like bureacratic nonsense to me.

I'm sorry you are upset with James, but the owner of the site has respectfully requested that all posters do as James indicated. Please read the web page Paul Wheaton wrote about the subject and forgive James for his "arrogance".

I'm not quite sure how a rule about a display name on JavaRanch makes any book (HeadFirst or otherwise) meaningless, although I do understand your frustration at not getting a useful answer immediately. Please remember that we are all here voluntarily to help each other. Perhaps in the near future you will be able to try to answer a question for someone else.
[ June 19, 2005: Message edited by: Marilyn de Queiroz ]
 
Pete Low
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Have tested out the codes.
Does not seem to be any problem.
The result is as follows:-


Playing the movie
Press any key to continue . . .
 
John Freshman
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all for the help.

Regarding JavaRanch, I will gladly obey the rules of the site and voluntarily help when I can as well (if I can).

To James, I apologize for my rudeness, I am sure you have a lot to offer any newbie like myself.

Also, to get back on topic, I am now able to compile the code as it is right from the book. I thought I had correctly configured my CLASSPATH but I was wrong. It had an incorrect reference to the bin directory in my project directory, which I removed. That did it!

Thanks again to all.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!