Thinking In Java

Thinking in Java by BruceEckel

ISBN 01 3659 7238

Full content of the 3rd edition available on the web at http://www.mindview.net/Books/TIJ/. The fourth edition, covering Java 1.5, will be available soon at bookstores (don't know about the web).


It is more than a good introduction. It covers some areas quite deeply. He's not afraid of pointing out where the Java designers went wrong: the piece of crud AWT, the awful I/O classes, etc. It is also has the benefit if gelling over a long period of time over the Web with lots of input before being published. It doesn't insult your intelligence. -- AnonymousDonor

It's free from his web site, it's a reasonable introduction, and it doesn't make a lot of stupid mistakes. It also doesn't really attempt to be comprehensive (one of the major failings of Core Java is that it covers a lot of ground in a very superficial way). Throw in the JavaTutorial (also free, from the Javasoft web site) and the lessons (also free, also from Javasoft, you need to be a JDC member to get them) and you've pretty much covered a lot of the basics. -- WilliamGrosso


Thinking in Java is one of the DefinitiveJavaBooks.


I like the way the example programs include self-testing - similar to the XP practice of TestDrivenDesign (AutomatedTesting).


Unfortunately, the code I downloaded from the site gives me "Invalid package declaration". Not knowing enough Java to fix it the "DoTheRightThing", I dump all the .java source files in a single directory, and comment out every

  import com.bruceeckel.simpletest.*;
and
  package com.bruceeckel.simpletest;
line. That seems to make it work.

So what is the right way to do this ?

You don't give much to go on. Not even the URL to the code or a description of how you tried to compile it.

The javac compiler doesn't care how the source is arranged, but will put the class files into a hierarchical directory structure (e.g. source com.bruceeckel.simpletest.SimpleTest.java will compile to com/bruceeckel/simpletest/SimpleTest.class). Many IDE's however, do expect the source code to be in the same hierarchical structure.

Also, unless the classes being imported are already compiled and on the classpath, javac will want them to be compiled in the same javac invocation.

Hope this helps.

Details:

I downloaded the book and source code in a single tar file following instructions on page http://www.mindview.net/Books/TIJ/

I installed "J2SE v 1.4.2_04 SDK with NetBeans 3.6 Bundle" from http://java.sun.com/j2se/1.4.2/download.html .

(I tried the EclipseIde, but I couldn't figure out how to compile the source code from the book inside that IDE, so I gave up on it.)

I skimmed through the "NetBeans IDE 3.6 Quick Start Guide" http://www.netbeans.org/kb/index.html .

When I open a random file from the

  aaa-TIJ3-distribution\TIJ-3rd-edition-code\com\bruceeckel\simpletest
directory with the NetBeans IDE, the line
    package com.bruceeckel.simpletest;
already has a red squiggly underline ("Invalid package declaration"). So I commented it out
    //package com.bruceeckel.simpletest;
.

I suspect that my problem is in my IDE or some obscure path setting somewhere. But I think it's asking too much for someone just starting to learn Java to "already know" how to fix it. It would be nice if there was a "download this, click that" step-by-step instructions for EclipseIde (or NetBeans) for someone wanting to learn Java, listing all the tools (Do I need to install Ant ?) that Bruce assumes are already installed and ready-to-go from page 1. -- DavidCary

---

Step-by-step instructions for using Eclipse with Thinking In Java

Download and unzip the source code directories.

When you get to Chapter 2, the section "Compiling and running":

Follow the instructions for installing the Java Developer's Kit (J2SDK). Be sure to follow the instructions at http://java.sun.com/j2se/1.5.0/install-windows.html about "Update the PATH variable".

Next, download and install the EclipseIde like this:

for windows users: You only need to download one Eclipse file. Ignore the files with "JDT" and "RCP" and "platform" in their names. Download a file named something like

  eclipse-SDK-3.1M3-win32.zip
. Create a corresponding directory something like
  C:\Program Files\eclipse-SDK-3.1M3-win32
and unzip that file into that directory.

Right-drag-and-drop the "eclipse.exe" file onto your desktop, creating a shortcut.

Create a fresh new "work" directory for your files - perhaps something like java_workspace/ the "My Documents" folder.

Copy the full name of that work directory. Open the "properties" tag of the shortcut, and paste the name of that directory into the "target". Add quotes to the beginning and end. OK.

Double-click that shortcut. (It may take a minute to get started). Take a few minutes to go through the "Basic tutorial".

The AutomatedCodeGeneration that happens when you go through that tutorial makes a trivial program. Add the line

  System.out.println("Hello, world.");
to that code, and do "Run | Debug as | Java Application".

It should print

  Hello, world.
in the "Console" window at the bottom. If not, I've messed up these instructions somewhere - tell me and I'll try to fill in the gaps.

Remember where you stuck the Thinking In Java source code ? Next we want to get that source code into Eclipse.

In Eclipse, right-click on the "src" directory that was created during your tutorial, then "Import ... | File System | Next | Browse". Scroll through that tiny little window, opening and closing directories until you click on "TIJ-3rd-edition-code" then OK. Click the little box next to "TIJ-3rd-edition-code" to display the check mark, then hit "finish". (This churns for quite some time).

In Eclipse, open directories until you find the "HelloDate?.java" file. Select it and run ...

Hm, I get a "select package does not match the expected package" error here, on the first line of "HelloDate?.java". That first line is a comment. The word "package" isn't even in this "HelloDate?.java" file. What am I doing wrong? -- DavidCary


First of all, you probably can solve it most easily by downloading ant - you'll get an antr.zip or ant.tar.gz, unzip it somewhere, set ANT_HOME environment variable to the root folder of ant (from where you should see for nexample $ANT_HOME/bin , respectively %ANT_HOME%\bin in windows). ANT_HOME is essential for ant to run. Then you cd into the Bruce's chapter directory where you should see a build.xml. You run $ANT_HOME/bin/ant(.sh or .bat); build.xml is the Makefile in the Java world, and ant is the make tool.

Now that does the compilation, of course it's better if you can import into an IDE. I'll get to it as soon as I have some time, but my advise is to please try Eclipse 3.01 as it is more stable (dated Nov 2004). If you have a less than spiffy computer (like only 512MB ram) Eclipse 2.1.X is highly recommended especially for beginner, as it is amazingly stable and fast. Eclipse 3 came with a lot of new and cool advanced feature, but unfortunately EconomyOfExecution took a back seat :)

The whole idea into the Java world is that if a class has on top of it the package declaration

 package chapter5;
Than the class should be in the directory structure in the path chapter5/MyClass.java and chapter5/myClass.class relative to a directory root. Now in Eclipse you can have several roots for the same project, allowing you some flexibility, so if your project is called MyProject? (File -> New -> Java Project -> MyProject?), than the directory's root folder will be by default in c:\eclipse\workspace\MyProject?. If You want to have it elsewhere, say you downloaded all the zips and unzip them into C:\temp\TIJ( from where you have c05\myClass.java and so on), you just set in the new Java Project dialog (you cannot change it later, after your pressed finnish!!!) "create project at external locatio" option and point to c:\temp\TIJ. The Eclipse will figure most of it on it's own.

Ok, now I did that myself, and yes, it turns out Bruce's code is organized somwehwat peculiarly. I unzip the xxxsrc.zip into a c:\temp\TIJ, and I created a new project choosing "external location" as C:\Temp\TIJ", lots of errors but those will be easy to fix.

As I was telling you, there's a root of the code from where a class in the package myPackage has to be in myPackage/MyClass.java relative to that root. Classes who don't have any

  package myPackage ;
declaration at the top are said to be in the "default" package (but default is not a name, it's a package with no name !!!) and they have to be directly under the root. So unlike in C++ where a namespace is just a language feature hidden inside source files, Java packages are both namespaces and they mandate a directory layout for file organization (not 100% accurate because by using advanced feature you can get rid of this constraints, but nobody is crazy to use those features).

The roots can be real directories for source code file, and also .zip and .jar for compiled .class files. Eclipse projects support several source roots per projects, and Eclipse correctly detects that for my new TempTIJ Java project the root sources should be in ch1,c02 ... subdirectories. To see that, you right click on the project -> Properties -> Java Build Path -> Source . However there's the problem that there's also the root source (C:\temp\TIJ) where he puts common classes that are used throughout the book, for example com/bruceeckel/util/Arrays2.java. Now we cannot add the root directory of the project to the roots in the source path, because then we will have imbricated root paths (a root is within the directory tree of another root) and that's a no-no in Eclipse and in NetBeans and in all other Java IDEs that I know of. We have to fix that, so I make sure that I am in the "Resource View" (Window -> Open Perspective -> Resource) . This is a "View" where menus are organized so that it is simple to manipulate directories and files. other views are beter for debugging , Java editing, class browsing, etc. So once I am in resource, I'll create a new subdir. Select TempTIJ project, right click > New -> Folder and I type the name of the folder src. Now I should see src subfolder under TempTIJ project. I now move the "com" subdirectory inside the "src" by dragging and dropping. (You can also do that externally using Explorer or a shell tool and then in Eclipse you select refresh, and Eclipse will read again the reality on the disk).

Now I have many more files compiling correctly, there are still a few with compilation problems. Some of them I can trace to the project being dependent on JavaUnit library. So download junit.jar put it somwhere (anywhere), go to project -> Properties -> Java Build Path -> Add External Jar and select junit.jar. I still got compilation problems. Ok, now I've got it. The directory appendixA under the project tree is a package directory, so it needs to be moved somewhere under a source root. I go to Resource View, select "appendixa" right click -> Move and select src. This moves "appendixa" to "src", and now all the compilation problems are fixed. actually not quite

Hope, that would help. Next time, I'll avoid writing up so early in the morning. The thing is I had to work another 45 minutes to fix the problems that come from a code that isn organized in total disarray. The instructions are too complex to put them here right now, maybe I'll come with some details later, but anybody interested can send me an email, and I'll can arrange to send the final package.

The code will work (probably) if you follow step by step instruction about how to build it, but that's not the point. The point is that a newcomer to Java (the likely audience for Bruce Eckel) will want to use a modern IDE like Eclipse and the way the code is organized, it'll just trap you and put you off if you are not already quite experienced with Java to fix the countless problems that could have been easily avoided if Bruce followed minimally some decent organization rules about where to put his files. Very bad, considering this, I wouldn't recommend the book to beginners. But then to advanced people is not very good either. -- CostinCozianu


See also DefinitiveJavaBooks, LearningJava


CategoryBook CategoryOnlineBook


EditText of this page (last edited July 27, 2010) or FindPage with title or text search