aspose file tools*
The moose likes Beginning Java and the fly likes Head First Java Frustration! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Head First Java Frustration!" Watch "Head First Java Frustration!" New topic
Author

Head First Java Frustration!

Thomas Hasselbeck
Greenhorn

Joined: Jul 12, 2005
Posts: 2
I'm about to remove my reference variable to my Head First Java book, so that it will end up on the garbage collection heap and go away. (huh maybe I am learning something)

I have been struggling through this book, mostly because of complier errors. It seems that reserved words are extremely case sensitive; using IF instead of if gives a compiler error, system.out.print instead of System.out.print gives you a compiler error as well. It took me awhile to figure that out! (grrrr)

I'm only on page 64 and I'm seriously thinking about tossing this book.
Just now I tried to work out the Code Magnet exercise on page 64. I came up with the right answer (according to the answer page) but when I try to compile the code I get this error;

Exception in thread "main" java.lang.NoClassDefFoundError: TestArrays/java

I have no idea what that means.

Here�s my code for those pleasant souls who don�t have this book that might be willing to help me;



Output should be;
Island = Fiji
Island = Cozumel
Island = Bermuda
Island = Azores

I have to walk away from this now so I can calm down.

Thanks in advance
Tom
[ July 12, 2005: Message edited by: Mark Spritzler ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60057
    
  65

using IF instead of if gives a compiler error


Yes, Java is a case-sensitive language. That's something that you are going to have to come to terms with right off the bat or you'll be taking a whole lot of "cool down walks".

Why does that make you frustrated with the book? Is the case incorrect in the examples?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Kristin Stromberg
Ranch Hand

Joined: May 17, 2005
Posts: 91
Java is case sensitive, and that can be very irritating at first! As for your compilation error, I have a feeling you're making a typo when compiling the class. Instead of javac TestArray.java, it looks like you might have typed javac TestArray/java.

Good luck!
Kristin Stromberg
Ranch Hand

Joined: May 17, 2005
Posts: 91
On second thought, it could also be a classpath issue, in which case you've encountered the second most irritating feature of Java (configuring your classpath). I promise you that things will get better from here on out!
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
Any language can be frustrating in the beginning. The problems you're describing don't seem to be related to the book. Most languages I've worked with are case-sensitive, especially with keywords. Only toy languages like BASIC are not, but that's probably not true in all implementations either.

I can only recommend patience, attention to detail (typos are the #1 case of programming errors), and a sense of humor. Oh, and don't leave a loaded hunting rifle too close to your computer, or you may have to buy a new pc!


Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

First thing why it isn't compiling.

The class needs to be public

so



Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

He typed

java TestArray.java

The Java runtime (java.exe) thinks you want to run a class named "java" in a package named "TestArray".

"Compiling" means running the compiler "javac". The main argument to the compiler is the name of a Java source code file; these always must be named *.java, and you always must give the complete filename to the compiler. You feed a *.java file to the compiler like

javac TestArray.java

This produces a file named TestArray.class .

"Running" the program means executing the runtime program "java". "java" doesn't use *.java files -- it has no idea what they are. It uses *.class files. But here's the tricky part: the arguments to "java" as not file names -- they're class names. Since you've written a class named TestArray, you must run it using

java TestArray

If you say "java TestArray.java", then Java goes looking for a class named "TestArray/java" (both the dot and the slash are used a separators in class names; the dot is used in source code and on the command line, while the slash is used internally -- which is why you see it in the error message.)

As for the case sensitivity: that's true for the majority of modern computer languages; Java's not alone here.


[Jess in Action][AskingGoodQuestions]
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Originally posted by Mark Spritzler:
First thing why it isn't compiling.

The class needs to be public

so



Mark



Actually it still ran in my IDE.

The other aspect of why it wouldn't compile is always classpath.

So what does classpath mean.

Simply put you set the base directory from which all your code will be in.

So if I put all my code in

c:\myJavaApp\src\

then that is what I set my classpath too.

If I have TestArrays.java in that directory, then I go to that directory in DOS and type "javac TestArrays.java". <B>Now if you type 'java -cp . TestArrays' from this directory you will get your code to run </B>

And as you might have learned already. There are packages to better group your classes. So if you put TestArrays into the com.yournamehere package, then you need to create a c:\myJavaApp\src\com\yournamehere directory and place the TestArrays.java file in that directory.

Now , here is the sometimes tricky part. You still keep your classpath set to "c:\myJavaApp\src". So now when you want to compile you have your DOS prompt in the c:\myJavaApp\src directory and type "javac com.yournamehere.TestArrays.java" and to run it just like above you use. "java -cp . com.yournamehere.TestArrays"

you can set your classpath using an environment variable in Windows. so in DOS you can type "set classpath=c:\myJavaApp\src" and now you no longer need that "-cp ." in the java call. So now to run it...

"java TestArrays" - no packages here

or

"java com.yournamehere.TestArrays" if the TestArrays.java file is in a package and in the c:\myJavaApp\src\com\yournamehere directory.

Mark
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9043
    
  10
Originally posted by Thomas Hasselbeck:
I have been struggling through this book, mostly because of complier errors. It seems that reserved words are extremely case sensitive; using IF instead of if gives a compiler error, system.out.print instead of System.out.print gives you a compiler error as well. It took me awhile to figure that out!


Not only reserved words are case sensitive; every word in Java is case sensitive.

when I try to compile the code I get this error;

Exception in thread "main" java.lang.NoClassDefFoundError: TestArrays/java


"javac TestArrays.java"
usually does not give that error.

Perhaps you typed
"java TestArrays/java"
??


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9043
    
  10
Originally posted by Mark Spritzler:
First thing why it isn't compiling.

The class needs to be public


A class does not need to be public to compile. It's pretty much only when you start getting into packages that you need to worry about whether or not it is public.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9043
    
  10
Also, if you are sitting in the directory where the *.java file is when you compile, you don't need to worry about the classpath issue.

e.g. type
C:\Java>javac TestArrays.java

and you should see a
C:\Java>TestArrays.class file in the same directory.

(I like to start as simple as possible.)
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60057
    
  65

So Thomas, as you can see there's no dearth of people willing to help you out here! Hang in there guy, and we'll help you through all this.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Originally posted by Marilyn de Queiroz:


A class does not need to be public to compile. It's pretty much only when you start getting into packages that you need to worry about whether or not it is public.


Actually in my second post, I rephrased that. I meant to run.

Mark
Thomas Hasselbeck
Greenhorn

Joined: Jul 12, 2005
Posts: 2
Holy Smokes! I'm completely humbled by the responses I got.

Yeah I typed in �Java TestArrays.java�
When I typed in �Java TestArrays� I saw the desired output.

I think part of my problem is that I�m a product of VB programming and I�m use to some of the creature comforts that application provides. Plus I was using Word to make my java files (I�m now using notepad) and Word automatically capitalizes words on you. I guess the case sensitive point was not driven home when reading the book and during one of my previous walk-aways I forgot that to run the java program you leave off the .java part.

When I�m finished wiping the egg from my face I will continue on with my Java journey.
And it�s very comforting to know that there is a bartender close by� I�ll need a few stiff belts before the journey is over.

Thanks again.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Originally posted by Thomas Hasselbeck:
Holy Smokes! I'm completely humbled by the responses I got.

Yeah I typed in �Java TestArrays.java�
When I typed in �Java TestArrays� I saw the desired output.

I think part of my problem is that I�m a product of VB programming and I�m use to some of the creature comforts that application provides. Plus I was using Word to make my java files (I�m now using notepad) and Word automatically capitalizes words on you. I guess the case sensitive point was not driven home when reading the book and during one of my previous walk-aways I forgot that to run the java program you leave off the .java part.

When I�m finished wiping the egg from my face I will continue on with my Java journey.
And it�s very comforting to know that there is a bartender close by� I�ll need a few stiff belts before the journey is over.

Thanks again.




Great, I am glad you got it too work. I also have experience with VB and understand your plight. Don't worry, it will eventually, some day, get easier.

Word is a pain when it automatically capitalized things on you.

You can try some other text editors like TextPad, or JEdit, that are Java friendly.

Mark
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9043
    
  10
A class does not need to be public to run.

Regarding text editors, there are many that are much better in my opinion than notepad. Mark named a few. Also, UltraEdit and even DOS's old 'edit' program (at least it maintains the indents). I think that Word and Notepad are two of the worst.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Originally posted by Marilyn de Queiroz:
A class does not need to be public to run.

Regarding text editors, there are many that are much better in my opinion than notepad. Mark named a few. Also, UltraEdit and even DOS's old 'edit' program (at least it maintains the indents). I think that Word and Notepad are two of the worst.


When the class was not public, and I ran java, it gave the error, when I added public to the class, then it would run through java.

Mark
Jacquie Barker
author
Ranch Hand

Joined: Dec 20, 2000
Posts: 201
Originally posted by Thomas Hasselbeck:
Holy Smokes! I'm completely humbled by the responses I got.


Tom, don't be humbled! A beginning book should make such things clear ... NOTHING should be left to the imagination.

Best,

Jacquie


Author of Beginning Java Objects, Beginning C# Objects, and Taming the Technology Tidal Wave
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Marilyn de Queiroz:
A class does not need to be public to run.

Regarding text editors, there are many that are much better in my opinion than notepad. Mark named a few. Also, UltraEdit and even DOS's old 'edit' program (at least it maintains the indents). I think that Word and Notepad are two of the worst.


I agree that NotePad is a poor choice for a text editor, although it is slightly better than Word for this purpose. As yet another alternative, you should check out TextPad. It is a great text editor for Windows in my opinion. You can use it "out of the box" as it is. Once you get more comfortable with Java and the editor, you can also customize it to help with some of the more mundane tasks that you encounter when programming in Java.

Good luck and don't give up

Layne


Java API Documentation
The Java Tutorial
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9043
    
  10
Originally posted by Mark Spritzler:
When the class was not public, and I ran java, it gave the error, when I added public to the class, then it would run through java.

Strange I've never encountered that.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Originally posted by Marilyn de Queiroz:

Strange I've never encountered that.


I just think it is my machine doing a practical joke on me. It works fine now. No need for public.

Mark
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Head First Java Frustration!
 
Similar Threads
whats wrong with my code ?
join()
Book Answer vs JVM, Same Code Different Output?
Be the compiler excercise in chap 3
Head First Java Code Magnets Chapter 3 Q