Your first Java program
This tutorial focuses on creating a Java program in:
- Windows
- Linux
NOTE: For this program, you must have the Java Standard Development Kit and the Java Runtime Environment installed and configured on your system, otherwise it will not work.
If you do not have the necessary requirements, please refer to our guide on Installing and configuring Java.
Windows
-
Open notepad by going to Start -> Programs -> Accessories -> Notepad OR go to Start -> Run -> Type notepad and press Enter.
-
Type in the following code:
class FirstProgram{ public static void main(String[] args){ System.out.println("My first Java program"); } } -
Save the file as FirstProgram.java
NOTE: Make sure that you give the file a .java extension and that you save it as type 'All Files', otherwise you will get plain text. Also, make sure that you save the file as FirstProgram.java with the same capitalization. Java is a case sensitive language!
-
Open the command prompt by going to Start -> Run -> Type cmd and press Enter and switch to the directory where you saved the file (to do so type CD and the absolute path of the directory where you saved the file):
-
Compile the file using the javac command:
-
Upon successful compilation, you will get the name of the directory you are in with no output:
If you got an error, make sure the code is the same as the one shown on this page. Also, make sure that the name of the file is FirstProgram.java with the same capitalization, and the file must be saved as type 'All files'.
-
After successful compilation, run the program using the java command by typing java FirstProgram, and press enter:
-
Your output should say "My first Java program" as it does in this screenshot:
Linux
-
Open nano
-
Type in the following code:
class FirstProgram{ public static void main(String[] args){ System.out.println("My first Java program"); } } -
Save the file as FirstProgram.java
NOTE: Make sure that you give the file a .java extension and that you save it as type 'All Files', otherwise you will get plain text. Also, make sure that you save the file as FirstProgram.java with the same capitalization. Java is a case sensitive language!
-
Open a console window and switch to the directory where you saved the file.
-
Type javac FirstProgram.java and press enter to compile the source code.
-
Upon successful compilation, you will get the name of the directory you are in with no output.
-
After successful compilation, run the program using the java command by typing java FirstProgram and press enter.
-
Your output should say "My first Java program"
Screenshots for linux coming soon..




