Software Development Exam  >  Software Development Notes  >  Basics of Java  >  How to Setup Java?

How to Setup Java? | Basics of Java - Software Development PDF Download

Introduction

Java is a popular programming language used for developing a wide range of applications. Setting up Java on your computer is the first step towards writing and running Java programs. This article will guide you through the process of setting up Java on different operating systems and provide examples and explanations along the way.

Installing Java on Windows

To install Java on a Windows machine, follow these steps:

  • Download Java: Visit the official Java website (https://www.oracle.com/java/technologies/javase-jdk11-downloads.html) and download the Java Development Kit (JDK) for Windows.
  • Run the Installer: Once the download is complete, run the installer and follow the on-screen instructions. Choose the appropriate installation options based on your requirements.
  • Set Environment Variables: After the installation, you need to set up the JAVA_HOME environment variable. To do this, right-click on the "This PC" or "My Computer" icon on your desktop, select "Properties," and then click on "Advanced system settings." In the System Properties window, click on the "Environment Variables" button. In the "System Variables" section, click "New" and add a new variable named JAVA_HOME with the value set to the JDK installation directory (e.g., C:\Program Files\Java\jdk-11.0.1).
  • Update PATH Variable: To run Java programs from the command line, you need to add the Java bin directory to the PATH environment variable. In the same "Environment Variables" window, find the "Path" variable under "System Variables" and click "Edit." Add %JAVA_HOME%\bin at the end of the variable value and click "OK."

Installing Java on macOS

To install Java on a macOS system, follow these steps:

  • Download Java: Visit the official Java website (https://www.oracle.com/java/technologies/javase-jdk11-downloads.html) and download the Java Development Kit (JDK) for macOS.
  • Run the Installer: Once the download is complete, run the installer package (.dmg file) and follow the on-screen instructions. Double-click the package and follow the prompts to complete the installation.
  • Set Environment Variables: By default, macOS does not require additional environment variable setup for Java. The JDK installation automatically sets the necessary variables.

Installing Java on Linux

To install Java on a Linux system, follow these steps:

  • Open Terminal: Open the terminal by pressing Ctrl+Alt+T or searching for "Terminal" in the applications menu.
  • Update Package Lists: Run the following command in the terminal to update the package lists:
    • sudo apt update
  • Install Java: Run the following command to install the default Java Development Kit (JDK):
    • sudo apt install default-jdk
  • Verify Installation: After the installation is complete, you can verify it by running the following command:
    • java -version

This command will display the installed Java version.

Verifying the Java Installation

To ensure that Java is installed correctly on your system, you can run a simple test program.

1. Create a Java file: Open a text editor and create a file named "HelloWorld.java" with the following contents:

public class HelloWorld {

    public static void main(String[] args) {

        System.out.println("Hello, World!");

    }

}

2. Compile and Run: Open a terminal or command prompt, navigate to the directory where you saved the "HelloWorld.java" file, and execute the following commands:

javac HelloWorld.java

java HelloWorld

If everything is set up correctly, you should see the output: "Hello, World!".

Sample Problems

1. Problem: Write a Java program to calculate the sum of two numbers and display the result.

public class SumCalculator {

    public static void main(String[] args) {

        int number1 = 10;

        int number2 = 20;

        int sum = number1 + number2;

        System.out.println("The sum is: " + sum);

    }

}

Output

The sum is: 30

2. Problem: Write a Java program to calculate the area of a rectangle.

public class RectangleAreaCalculator {

    public static void main(String[] args) {

        int length = 5;

        int width = 10;

        int area = length * width;

        System.out.println("The area of the rectangle is: " + area);

    }

}

Output

The area of the rectangle is: 50

The document How to Setup Java? | Basics of Java - Software Development is a part of the Software Development Course Basics of Java.
All you need of Software Development at this link: Software Development
60 videos|37 docs|12 tests

Up next

60 videos|37 docs|12 tests
Download as PDF

Up next

Explore Courses for Software Development exam
Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

mock tests for examination

,

video lectures

,

shortcuts and tricks

,

How to Setup Java? | Basics of Java - Software Development

,

Free

,

pdf

,

practice quizzes

,

Important questions

,

How to Setup Java? | Basics of Java - Software Development

,

Previous Year Questions with Solutions

,

Objective type Questions

,

study material

,

Semester Notes

,

Viva Questions

,

Exam

,

Summary

,

ppt

,

Extra Questions

,

Sample Paper

,

How to Setup Java? | Basics of Java - Software Development

,

MCQs

,

past year papers

;