Skip to content

Instantly share code, notes, and snippets.

@MeNiks
Created September 11, 2024 03:18
Show Gist options
  • Save MeNiks/a1f81bc61db8562b4d91663a0e859852 to your computer and use it in GitHub Desktop.
Save MeNiks/a1f81bc61db8562b4d91663a0e859852 to your computer and use it in GitHub Desktop.
First Java Program
  1. 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.
  2. 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.
  3. 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.

  1. 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment