Let's create our first Java file, called MyClass.java, which can be done in any text editor (like Notepad).
The file should contain a "Hello World" message, which is written with the following code:
MyClass.java
public class MyClass {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
- Save the code in Notepad as "MyClass.java". Open Command Prompt (cmd.exe), navigate to the directory where you saved your file, and type "javac MyClass.java":
- C:\Users\Your Name>javac MyClass.java
- This will compile your code. If there are no errors in the code, the command prompt will take you to the next line. Now, type "java MyClass" to run the file:
- C:\Users\Your Name>java MyClass
The output should read:
Hello World.