Your first graphical Java program
This tutorial focuses on creating a graphical 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:
import java.awt.*; class FirstGraphicalProgram extends Frame{ public static void main(String[] args){ AFrame frame = new AFrame(); frame.setSize(200, 200); frame.setVisible(true); } } -
Save the file as FirstGraphicalProgram
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 FirstGraphicalProgram.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 by typing javac FirstGraphicalProgram.java and press Enter.
-
Upon successful compilation, you will get the name of the directory you are in with no output.
NOTE: 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 FirstGraphicalProgram.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 FirstGraphicalProgram, and press Enter:
-
Your output should be a graphical frame on the screen, like this one:
Linux
-
Open nano
-
Type in the following code:
class FirstGraphicalProgram{ public static void main(String[] args){ System.out.println("My first Java program"); } } -
Save the file as FirstGraphicalProgram.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 FirstGraphicalProgram.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 FirstGraphicalProgram.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 FirstGraphicalProgram and press Enter.
-
Your output should be a graphical frame on the screen.
Screenshot for linux coming soon..




