Win a 3 month subscription to Marco Behler Videos this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

"Head First Java" Help  RSS feed

 
Reegan Cantrelle
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I've recently decided I would like to begin android app programming. I've been advised to begin by familiarizing myself with Java. The first book I'll be going through on the subject is "Head First Java" (second edition). I'm already somewhat lost at the beginning.

It requires me to download the java SDK, the API documentation, and to "add an entry to your PATH environment variable that points to the /bin directory inside the main java directory". I am fairly certain that I already have the newest SDK (I believe I downloaded it when I downloaded the android development tools), though I am unsure about the API documentation and the entry to the path environment variable bit.
 
Knute Snortum
Sheriff
Posts: 3837
91
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a wiki page that will help you.
 
Campbell Ritchie
Marshal
Posts: 54901
155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Pass the following instructions to a command line:-
java -version
javac -version
Tell us what happens. If you get a version number, then your PATH is set up all right. The latest version numbers are 1.8.0_101 and 1.8.0_102.
 
Reegan Cantrelle
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This was my results:
output.png
[Thumbnail for output.png]
 
Campbell Ritchie
Marshal
Posts: 54901
155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is not as difficult to read as some screenshots, and seems to show that you have a correct installation and correct PATH, so you should start with the MoooooooooseGreeting program or I Rule the World in HFJ.
 
Reegan Cantrelle
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps my confusion is coming from how to use the command prompt (my limited coding experience was all done using IDEs). I'm trying to follow along with the "mooooooooo" program however I keep getting "... is not recognized as an internal or external command, operable program or batch file."

I am storing my text files in a folder that is located on my desktop, I am unsure if this makes a difference or not.

(On another note, I appreciate the continued assistance as I will likely be a frequenter of these forums for quite a while until I get a grasp on things).
 
Campbell Ritchie
Marshal
Posts: 54901
155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is very peculiar; if you get 1.8.0_101 from javac −version, you shou‍ld be able to run the javac command from the command line without let or hindrance. Please show us what you get from
echo %PATH%
assuming you are using Windows®. If not Windows® use echo $PATH

[edit]You must be using Windows® because other OSs give different error messages.
 
Reegan Cantrelle
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've gotten it now; it was as I thought, a simple error for lack of knowledge of the command prompt. I was incorrectly using the cd command to navigate to the correct directory where my files were stored. Hopefully now I can get through the introduction of the book at least before encountering more errors.
 
Campbell Ritchie
Marshal
Posts: 54901
155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done
Please show us the output from your first program, so everybody knows you have got it working.
 
Reegan Cantrelle
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the end result. As an aside question, do you believe there is significant value in learning to code from the command prompt/notepad without using an IDE? I suppose it has something to do with learning to crawl before learning to walk; however I wasn't sure if there was more to it than that.
output2.png
[Thumbnail for output2.png]
 
Campbell Ritchie
Marshal
Posts: 54901
155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Continue with the command line/terminal until you reach the stage of adding things to your CLASSPATH; you sh‍ould become familiar with that syntax. The real problem with IDEs is that they have such a steep learning curve that they can exhaust all your brainpower, which you shou‍ld be using for the code.
You appear to be running successfully, but that screenshot is very difficult to read. The link I posted contains another link reminding you how to copy'n'paste from the command line. It is much easier to do that from a Unix‑like terminal.
 
Reegan Cantrelle
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apologies for the screenshot, figured out how to copy/paste from the command prompt now

Can someone explain packages to me? I ran a sample of code from my book, a simple program called "BeerSong". In the source code it includes "package chap01;" before the class begins (this is also the name of the folder that the file is located in, if that makes a difference). However, when I go to run the code I get the following error:


C:\Users\reega_000>cd\

C:\>cd \Users\reega_000\Desktop\Coding\Head-First-Java-master\chap01

C:\Users\reega_000\Desktop\Coding\Head-First-Java-master\chap01>javac BeerSong.java

C:\Users\reega_000\Desktop\Coding\Head-First-Java-master\chap01>java -classpath . BeerSong
Error: Could not find or load main class BeerSong


I can get the program to run fine if I go into the source file and remove the package statement at the beginning. What is the cause of this?
 
Dave Tolls
Ranch Hand
Posts: 2727
30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java maps package names to folders.
So package foo.bar will be in a folder on the classpath called foo/bar.

This also means that, when executing or compiling, the classpath should include the root directory (or directories, since there may be more than one) for your packages.
In your case that root would be the Head-first-java-master folder.
 
Reegan Cantrelle
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:Java maps package names to folders.
So package foo.bar will be in a folder on the classpath called foo/bar.

This also means that, when executing or compiling, the classpath should include the root directory (or directories, since there may be more than one) for your packages.
In your case that root would be the Head-first-java-master folder.


Thanks for the quick reply. Can you provide the exact syntax for how I would type this into the terminal? I'm still a bit confused on it. I'm assuming you're meaning that it should be included somewhere on the "java -classpath . BeerSong" line. And for the root directories, how far back do you go? Currently the file is buried within three folders on the desktop (folder "chap01" inside folder "head-first-java-master" inside folder "coding").
 
Dave Tolls
Ranch Hand
Posts: 2727
30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, in your case, what I would do is compile and run from the Head-First folder.

So your compile command would be:
javac chap01/BeerSong.java

And then the execute command would be:
java chap01.BeerSong

This will work so long as you have not create a CLASSPATH environment variable, as Java defaults the classpath to '.' (ie the current directory).
 
Campbell Ritchie
Marshal
Posts: 54901
155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suggest you remove the package declarations from the HFJ classes. Close examination of the  code shows there are no package declarations in the printed versions, only the downloaded ones, which makes compiling more difficult. You will learn more if you copy the code by hand and change it as you go. You can read about packages in the Java™ Tutorials. I would suggest you put the code into your folder minus the package name:-
javac BeerSong.java
java BeerSong

With package names, you can put the .class files into their correct location like this, but there are other ways to compile files in packages from the command line:-
javac -d . BeerSong.java
java chap01.BeerSong

That assumes the .java file is in the current folder and it creates a chap01 folder, so you would be in the head-first-java-master folder. I think the annoyance of more complicated compilation outweighs the benefits of using packages at this stage.

[edit]I see Dave Tolls has noticed something I missed; if you have already put the BeerSong.java file inside the chap01 folder, you use the syntax he showed you.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!