Software Installation
Before you can start programming in Java, you need to install the Java Development Kit (JDK) and set up your development environment. Here's how you can install Java and set up your workspace.
Step 1: Download and Install JDK
- Visit the official Oracle website: Oracle JDK Downloads.
- Choose the version of JDK you want to download and select the appropriate installer for your operating system (Windows, macOS, or Linux).
- Run the installer and follow the on-screen instructions to complete the installation.
- Once installed, set up the Java environment variables (like PATH) to run Java from the command line.
Step 2: Install Notepad++ (Optional, for Windows users)
- Notepad++ is a popular text editor that you can use to write Java code. Download it from here.
- Install Notepad++ following the on-screen instructions.
- Ensure you have the JDK properly installed and configured before you begin coding in Notepad++.
Step 3: Install Eclipse IDE
- Download Eclipse IDE from the official website: Eclipse Downloads.
- Choose the version that suits Java development (Eclipse IDE for Java Developers) and install it.
- Launch Eclipse after installation and set up a new workspace for your projects.
- Ensure Eclipse is configured with the correct JDK (set up in Preferences under Java > Installed JREs).
HelloWorld in Java
Let's write and run a simple "Hello World" Java program using Notepad and Eclipse.
Option 1: Using Notepad
Follow these steps to create a Java program using Notepad:
- Open Notepad and write the following code:
- Save the file with a .java extension (e.g., HelloWorld.java).
- Open the command prompt or terminal and navigate to the directory where the file is saved.
- Compile the program using the following command:
javac HelloWorld.java
- Run the program using the following command:
java HelloWorld
- You should see the output: Hello, World!
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Option 2: Using Eclipse
To create a "Hello World" program in Eclipse, follow these steps:
- Open Eclipse and create a new Java project by selecting File > New > Java Project.
- Give your project a name (e.g., "HelloWorld") and click Finish.
- Inside the new project, create a new Java class by selecting File > New > Class.
- Name the class HelloWorld and check the box for public static void main(String[] args) to automatically add the main method.
- In the editor, write the following code inside the main method:
System.out.println("Hello, World!");