[appendix]
== Installing Java JDK
To use jshell and run the examples in this book, you need Java JDK version 9 or later installed on your computer. This appendix will help you check if you already have Java installed and guide you through installation if needed.
=== Checking Your Current Java Installation
First, let's see if you already have Java installed and what version it is.
==== Step 1: Open Your Terminal or Command Prompt
*On Windows:*
- Press `Windows + R`, type `cmd`, and press Enter
- Or search for "Command Prompt" in the Start menu
*On macOS:*
- Press `Cmd + Space`, type `terminal`, and press Enter
- Or go to Applications > Utilities > Terminal
*On Linux:*
- Press `Ctrl + Alt + T`
- Or search for "Terminal" in your applications
==== Step 2: Check Java Version
Type the following command and press Enter:
[source]
----
java -version
----
You might see output like this:
[source]
----
java version "17.0.2" 2022-01-18 LTS
Java(TM) SE Runtime Environment (build 17.0.2+8-LTS-86)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.2+8-LTS-86, mixed mode, sharing)
----
*If you see a version number 9 or higher (like 11, 17, 21), you're all set!*
*If you see an error like "java is not recognized" or "command not found", you need to install Java.*
*If you see a version number lower than 9 (like 1.8), you should update to a newer version.*
==== Step 3: Check if You Have the JDK (Not Just JRE)
To use jshell, you specifically need the JDK (Java Development Kit), not just the JRE (Java Runtime Environment). Check by typing:
[source]
----
javac -version
----
You should see something like:
[source]
----
javac 17.0.2
----
*If you get "javac is not recognized" or "command not found", you have the JRE but need the full JDK.*
=== Installing Java JDK
If you need to install or update Java, here are the recommended approaches:
==== Option 1: Oracle JDK (Recommended for Beginners)
1. Go to https://www.oracle.com/java/technologies/downloads/
2. Download the latest LTS (Long Term Support) version for your operating system
3. Run the installer and follow the setup wizard
4. Restart your terminal/command prompt
5. Test the installation using the commands from Step 2 above
==== Option 2: OpenJDK (Free Alternative)
OpenJDK is a free, open-source implementation of Java:
*For Windows:*
1. Go to https://adoptium.net/
2. Download the latest LTS version
3. Run the installer
4. Make sure "Set JAVA_HOME variable" is checked during installation
*For macOS with Homebrew:*
[source]
----
brew install openjdk
----
*For Ubuntu/Debian Linux:*
[source]
----
sudo apt update
sudo apt install openjdk-17-jdk
----
*For Red Hat/CentOS/Fedora Linux:*
[source]
----
sudo yum install java-17-openjdk-devel
----
==== Option 3: SDKMAN (For Advanced Users)
SDKMAN is a tool for managing multiple Java versions:
1. Install SDKMAN: https://sdkman.io/install
2. Install Java:
[source]
----
sdk install java 17.0.2-tem
----
=== Verifying Your Installation
After installation, open a new terminal/command prompt and run:
[source]
----
java -version
javac -version
jshell
----
The first two commands should show version 9 or higher. The third command should start jshell with a welcome message like:
[source]
----
| Welcome to JShell -- Version 17.0.2
| For an introduction type: /help intro
jshell>
----
Type `/exit` to quit jshell.
=== Troubleshooting Common Issues
==== "java is not recognized as an internal or external command"
*On Windows:*
1. The Java installer should have set the PATH automatically
2. If not, you may need to add Java to your PATH manually:
- Find where Java is installed (usually `C:\Program Files\Java\jdk-XX\bin`)
- Add this to your system PATH environment variable
- Restart your command prompt
*On macOS/Linux:*
1. Java might be installed but not in your PATH
2. Try: `which java` to see if it's found
3. You may need to add Java to your PATH in your shell configuration file
==== Multiple Java Versions
If you have multiple Java versions installed:
*On Windows:* The last installed version usually becomes the default
*On macOS:* Use `java_home` to manage versions:
[source]
----
/usr/libexec/java_home -V
----
*On Linux:* Use `update-alternatives`:
[source]
----
sudo update-alternatives --config java
----
==== Permission Issues on macOS
If you get security warnings on macOS:
1. Go to System Preferences > Security & Privacy
2. Click "Allow" for Oracle or the Java installer
3. Try the installation again
=== Recommended Java Versions
For this book, we recommend:
- **Java 21 LTS** - Latest long-term support version (recommended)
- **Java 17 LTS** - Widely used long-term support version
- **Java 11 LTS** - Older but still supported LTS version
Avoid:
- Java 8 and earlier (no jshell support)
- Non-LTS versions unless you have a specific need
=== Setting Up Your Development Environment
Once Java is installed, you have several options for writing and running Java code:
==== Using jshell (Recommended for This Book)
jshell is perfect for learning and experimenting:
- No need to create full programs
- Immediate feedback on code snippets
- Great for testing small pieces of code
- Comes built-in with Java 9+
Start jshell by typing `jshell` in your terminal.
==== Code Editors and IDEs
For larger projects, consider these tools:
**Beginner-Friendly:**
- **Visual Studio Code** with Java extensions - Free, lightweight
- **IntelliJ IDEA Community Edition** - Free, powerful
- **Eclipse** - Free, widely used
**Professional:**
- **IntelliJ IDEA Ultimate** - Commercial, full-featured
- **NetBeans** - Free, Oracle-supported
**Simple Text Editors:**
- **Notepad++** (Windows) - Basic syntax highlighting
- **TextEdit** (macOS) - Simple text editing
- **Gedit** (Linux) - Basic code editing
For this book, jshell is sufficient, but having a good editor helps when you want to save your programs.
=== Getting Help
If you're still having trouble:
1. **Check your Java vendor's documentation:**
- Oracle: https://docs.oracle.com/en/java/
- Adoptium: https://adoptium.net/support/
2. **University/School IT Support:** Many institutions provide Java installation help
3. **Online Communities:**
- Stack Overflow: https://stackoverflow.com/questions/tagged/java
- Reddit: r/learnjava
4. **Verify Installation:**
- Make sure you downloaded the JDK, not just the JRE
- Ensure you downloaded the correct version for your operating system (32-bit vs 64-bit)
- Try restarting your computer after installation
Once you have Java JDK 9 or later installed and working,
you're ready to start using jshell and following along with all the examples in this book!