- Install Java Development Kit (JDK) Download and install the latest version of JDK from the Oracle website or use OpenJDK. After installation, make sure the java and javac commands are available in your system's PATH by running java -version and javac -version in the terminal.
- Set Up Your Development Environment You can use any text editor (like Notepad, VS Code, or Sublime Text) or an Integrated Development Environment (IDE) like IntelliJ IDEA, Eclipse, or NetBeans.
- Write the HelloWorld Program Open your text editor or IDE and create a new file named HelloWorld.java.
Type the following code into the file:
java Copy code // HelloWorld.java public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } } 4. Save the File Save the file with the name HelloWorld.java. Make sure the filename matches the class name (case-sensitive). 5. Compile the Program Open your terminal or command prompt.
Navigate to the directory where your HelloWorld.java file is saved.
Compile the program by typing the following command:
bash Copy code javac HelloWorld.java This command will generate a HelloWorld.class file in the same directory, which is the compiled bytecode.
- Run the Program To run your compiled Java program, type the following command:
bash Copy code java HelloWorld You should see the output:
Copy code Hello, World! Troubleshooting Tips: If you encounter errors during compilation, ensure that: The filename matches the class name (HelloWorld). The JDK is properly installed and the PATH is set correctly. Make sure to navigate to the correct directory before compiling and running the program. That's it! You've successfully written and run your first Java program.