Java Tutorial


Chapter 2 – Setting up Java Development Environment


Welcome back to the Java Tutorial series…

In the previous chapter we got introduced to the world of Java and understood why you should learn Java. In this chapter, let’s get started with setting up Java development environment to start writing Java programs.



Downloading and Installing Java

To develop and run Java programs, you need to install Java in your machine (laptop or desktop).

Start with visiting official download page of Java Development Kit (JDK) at Oracle’s website. Official Oracle Website

Java Image

Depending on your operating system (either Windows/Linux/macOS), you may want to visit the respective section, download and install the respective JDK installer.

Once the installation is completed, start your terminal or command prompt to check if Java is installed correctly or not, by running this command:

>> java –version

If Java is installed correctly you will get a message similar to the one given below mentioning the version of the Java JDK installed in your machine.

>>
java version "22" 2024-03-19
Java(TM) SE Runtime Environment (build 22+36-2370)
Java HotSpot(TM) 64-Bit Server VM (build 22+36-2370, mixed mode, sharing)

If “java” keyword is unrecognized or not detected, it is likely that it might not be installed or configured correctly.

If you are using Windows, you may be required to go through an additional step of adding the Java JDK path to Environment’s “Path” variable.

  • For this, visit the folder where your Java JDK is installed. It will be typically located under this folder structure -
    >> C:\Program Files\Java\jdk-22\bin
  • Copy this path and open Environment variables and append it (after adding a semicolon ;) to the values against the system variable titled “Path”.
  • Once done, you may check if it works by running the “java –version” command on command prompt.


Compiling and Running your first Java program:

Once Java is installed in your system, you are ready to run your first Java program. You can run java programs in 2 ways:

  • Using a text editor to write the program and using command line to run it.
  • Using a popular Java IDE such as Eclipse, Netbeans, VSCode, etc to write and run your program from within the IDE itself.

Let’s check out how you can run your Java programs in command line…

  1. Open a text editor such as notepad.
  2. Type or Paste the below sample code snippets (don’t worry if you don’t understand it, we shall explain it in the upcoming chapter).
    public class Main {
      public static void main(String[] args) {
        System.out.println("Hello World");
      }
    }
  3. Save your file with name as “Main.java”
  4. Start command prompt or terminal inside the same folder or navigate to the parent folder where the file is saved.
  5. Now, type the below command in CMD or terminal. This command will first compile your Java program.

    >> javac Main.java

  6. Once done, type the next command. This command will execute your Java program

    >> java Main

  7. On successful execution, you will be displayed a message as “Hello World” on the command prompt or terminal.

Congratulations! You have successfully ran your first Java program.

In real life projects, Professional Java developers use a Java IDE such as Eclipse, Netbeans, Jetbrains IntelliJ Idea or Visual Studio Code (VSCode) to work on Java projects. In upcoming chapters, we shall work using Eclipse IDE to write and execute our Java programs.

I hope you liked reading this tutorial. See you in the next tutorial.

Next Chapter Chapter 3 - Writing first Java program in Eclipse

Follow Me on Social Media

Advertisement
FREE Sales CRM Software

Fully customizable CRM Software for Freelancers and Small Businesses

Signup for Free

Sign up for DigitalOcean Cloud

Get FREE $200 credits, deploy your hobby projects for Free

DigitalOcean Referral Badge
Sign up for Hostinger Cloud or VPS plans and get upto 20% Discount