Why would an academic write a textbook for free?
As the author of this particular free textbook, I can say that the previous answers have covered things pretty well. "Introduction to programming using Java" started in 1996 as a set of class notes written in HTML with embedded Java applets -- which all seemed pretty exciting in the heady early days of the Internet. I had published an earlier textbook in the usual way, and made not much money from it, which is true of the large majority of published textbooks. I had recently been promoted to full professor, possibly the most secure job in the world where the only further promotion available to me would move me into (shudder) administration, so it would make little difference to my career if I self-published a book instead of submitting it to a regular publisher.
I lost creative control over my regularly published textbook. The copyright was assigned to the publisher until it goes out of print, which it will probably never do in these days of print-on-demand. There has never been a second edition. My Java textbook, on the other hand, has gone through seven versions and has been updated fairly regularly. I get the ego boost of hearing from people all over the world who have read it (something I can never seem to get most of my own students to do). The web site still gets hundreds of visits every day. It has certainly had more impact on the world than if I were selling it. And if someone doesn't like it, I can politely point out that they can write their own textbook, or even make a modified version of mine.
So, my main motivation was to write a textbook that I could use in my own courses. Selling it would make me very little profit. And since I use free software all the time, it's also a way that I could give back something to the open source / creative commons community.
I don't know why Eck made his book freely available, but I can tell you why I make my course materials freely available, and will continue to do so even if I ever take the final step of turning them into a book.
Because I want as many people as possible to use what I've written.
As others have pointed out, the expected profit from publishing a textbook is minuscule, especially a textbook in a well-worn topic like Java programming or algorithms. By making my notes freely available instead of selling them, I potentially lose a small amount of money, but I gain a significantly wider audience. And that wider audience justifies, in my mind, the thousands of hours that I have put into writing, polishing, illustrating, rewriting, and maintaniing them, far more than any monetary compensation would.
Other answers are right but I would add about the selfish economic point. Authors get about 10% of cover price, and most academic books aren't likely to sell more than a few hundreds of issues. Therefore, a single author can't expect to get more than a few hundreds of euros or dollars in the whole lifetime of a book.
In the other hand, academics need citations, since most promotions and grants are directly or indirectly linked to citations. In fact, a large part of academic work is writing papers that aren't paid, mainly to get more citations. Publishing a book and making it widely used by distributing it for free can attract more citations than journals. Furthermore, the prestige boost that a widely known textbook produces may lead to collaborations and citations that promote the author's career, which can lead to larger income than the one lost by giving away the book for free.
Furthermore, self publishing a book in Internet is easy compared to trying to find an editorial wanting to risk their money publishing the same academic book.