How to install Java and Set up a Development Environment
To compile and execute Java codes you need to install Java in your machine and set up your environment. In this guide, I will walk you through how to download Java, install it on your machine, and set up your development environment.
The installation process will be split into different steps, because of different Operating Systems. so, therefore, go with your OS.
Download and Install Java
Mac and Window: Simply head to Oracle, pick the LTS(Long Term Support) version as the time of this writing is Java 21, select your OS(Mac or Windows) type, and download the installer preferably. Upon installing, Java leave everything default to follow along.
Open your terminal or command prompt and run this command below to check for the version you just installed:
java --version
That should spit out java version "21.0.1" 2023-10-17 LTS
which you just installed.
Linux:
Open your terminal and simply run the command below:
sudo apt install
sudo apt install oracle-java17-installer
java -version
I hope these work for you. If so, skip this manual setup and head to installing an IDE. There are chances you will need to manually set up your PATH Variable if you are running Windows 10.
Manual set-up your PATH Variable
Follow these steps to manually set your PATH Variable:
- Open your window File Explorer and navigate to your C-Drive
- Open your program files folder(this folder contains your installed programs). In it, there is a folder named Java and open it.
- You should see one folder named
jdk-21
, open it inside there will be many sub-folders and files. Open the bin folder. - Preview:
- Within the highlighted yellow shade, click right on the last arrow > after the bin. It should turn to an actual address bar similar to that of a browser’s address bar, this
C:\Program Files\Java\jdk-21\bin
will be highlighted,ctrl+c
to copy. - Now you have to go to your Environment variables. Press the Windows key, and type env you should see ‘Edit the system environment variables’, press on Enter key to open it.
- Advance tap will be selected by default, at the bottom you should see a button
Environment Variables
Click on it another window will open. - There you will see the
User variables
(this will set the path to the only logged user), and System Variables(which will set the path for the entire system, and any user can access it). I prefer theSystem Variables
. - First Instruction: On the System, Variables select the Path and click the edit button. Another small window will pop up. If it doesn’t show like the image below try out the second or the third instructions after the image.
- The way my system shows it:
- Click on the New button a new line will be for you write below the last line.
ctrl+v
to paste what you copied from your bin folder which looks like thisC:\Program Files\Java\jdk-21\bin
. You can click on the OK button. - Second Instruction: Suppose you don’t see all of the paths like the above it’s ok. Click the New button a text area will be below the path, paste using
Ctrl+v
and then OK. - Third Instruction: All the paths will appear in one line separated by a semicolon. The last path might not have a semicolon at the end add it and
Ctrl+v
to paste and then OK.
Verify Java
Open your command prompt and run java -version. This should show you Java version "21.0.1" 2023-10-17 LTS
. Congratulations on setting up for dev environment.
Install An IDE(Integrated Development Environment)
Well, learning Java requires having an IDE(integrated development environment). This will help us with many things like syntax highlighting, code formatting, suggestions, and many more.
We will be using IntelliJ IDE, but you can use any IDE of your choice. To download IntelliJ navigate to the official site, and make sure you download the Community Edition(free for everyone to download and use), the ultimate is paid.
Finish the installation:
- Create Desktop Icon(choose your OS type).
- Update PATH variable(Add launchers dir to PATH).
- Update context menu(Add Folder Project).
- Create associates(.java)
Finish with next button and wait your PC to restart.
Java installation, setting up your path, verifying the Java version, and downloading an IDE is done. All is set, you can start writing your Java code. Below you will learn how to compile and execute Java codes using a terminal or command prompt. This is not mandatory.
Compile Java Code Using Terminal
To be able to compile your Java codes using the terminal or command prompt you first need to create a folder on your Desktop preferably called Java.
Next, open your text-edition example Note Path in Windows, copy the below code snippets, and paste it there. Click on the file from the menu and select Save As
so that we can save it as a .java
file.
public class Main {
public static void main(String[] args){
System.out.println("Hello World!");
}
}
Navigate to Desktop or anywhere you have created your java folder:
Select on the drop down, select All files as shown above and save your file.
Next, you need to navigate to your Desktop using your File Explorer and open your Java folder you should see a file named Main
or Main.java
depending on your OS type. You will need to open your Java folder in a terminal or command prompt window. Simply right anywhere with your File Explorer from the context menu and select Open Terminal a terminal window will open. Watch out for directories reading from the right on Window: \Desktop\java>
, Mac and Linux: Desktop/java
.
Follow these few steps to compile and execute your Java codes:
- List your directory within your
java
folder with thedir
command for Windows andls
for Mac and Linux then hit on enter key. This should display only one fileMain.java
. - Compile your Java code in your Main.java file by invoking the Java Compiler. Write
javac Main.java
and hit on enter. Nothing should show up if all is well all right. - Run the
dir
orls
command now you should see two filesMain.java
andMain. class
. TheMain.class
contains the compiled byte code. - Execute or Run your Java code by running
java Main.java
. This should print Hello World in text in your terminal window. You can add some other print statements like so;System.out.println(2+2)
, compile, and execute 4 should be printed.
Congratulations! You can now compile your Java without an actual IDE. Anyway, an IDE does the boring stuff you when through under the hood, saving you a lot of time, showing you compile-time errors, warnings, and suggestions, and easing your dev experience as. As a developer, it is worth knowing the command line basis as it is so unavoidable.
Summary
- You have learned how to download and install JDK on your preferred OS
- You have gained an inside into how to set up an environment,
- You have learned how to download and install an IDE
- You have learned how to compile Java codes using the terminal or command prompt.