When reading Head First Design Patterns, I need to run a few examples in Java. Plus that I also want to learn more about Java for two reasons: it’s well-known as a strongly typed programming language, and I have heard quite a lot that it’s a good language to learn OO (object-oriented) deeply.
Some notes:
- I use Mac and I got this error when trying to run
javacorjava:The operation couldn’t be completed. Unable to locate a Java Runtime.That’s because Mac does not come with JDK by default. I needed to download JDK here https://www.oracle.com/java/technologies/javase-jdk16-downloads.html - Here is the GitHub repo containing examples https://github.com/bethrobson/Head-First-Design-Patterns. I tried a few things to run it but got this error:
Error: Could not find or load main class headfirst.designpatterns.strategy.MiniDuckSimulator1. Reading this post helped me solve the issue.
Finally, these commands make Java examples of the book work:
$ cd ~/path/to/Head-First-Design-Patterns/src/headfirst/designpatterns/strategy
$ javac *.java
$ cd ~/path/to/Head-First-Design-Patterns/src
$ java headfirst.designpatterns.strategy.MiniDuckSimulator
Quack
Squeak
<< Silence >>
I can't fly
I'm flying with a rocket