Programming

Programming intro
Java

Markup

First webpage guide
HTML
XHTML

Style & Layout

CSS

Browser scripting

Javascript
VBScript
AJAX

Server scripting

PHP
ASP

Making money online

Make money online

Your first graphical Java program

This tutorial focuses on creating a graphical Java program in:

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

  1. Open notepad by going to Start -> Programs -> Accessories -> Notepad OR go to Start -> Run -> Type notepad and press Enter.

  2. 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);
    
        }
    
    }
    
  3. 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!

  4. 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).

  5. Compile the file using the javac command by typing javac FirstGraphicalProgram.java and press Enter.

  6. 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'.

  7. After successful compilation, run the program using the java command by typing java FirstGraphicalProgram, and press Enter:

  8. Your output should be a graphical frame on the screen, like this one:

Linux

  1. Open nano

  2. Type in the following code:

    class FirstGraphicalProgram{
    
        public static void main(String[] args){
    		
            System.out.println("My first 
                                Java program");
    	
        }
    
    }
    
  3. 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!

  4. Open a console window and switch to the directory where you saved the file.

  5. Type javac FirstGraphicalProgram.java and press enter to compile the source code.

  6. Upon successful compilation, you will get the name of the directory you are in with no output.

  7. After successful compilation, run the program using the java command by typing java FirstGraphicalProgram and press Enter.

  8. Your output should be a graphical frame on the screen.

Screenshot for linux coming soon..

Step-by-step tutorials
Programming intro
HTML
XHTML
CSS
Javascript
Java

Practice

Online code editor
Practical examples
Practical exercises

Reference

Terms glossary
Reference material

Rate this site

Rate this site
Visitor comments