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 Java program

This tutorial focuses on creating a 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:

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

  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):

    Switch directory
  5. Compile the file using the javac command:

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

    Successful compilation

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

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

    Run the program
  8. Your output should say "My first Java program" as it does in this screenshot:

    Result displayed

Linux

  1. Open nano

  2. Type in the following code:

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

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

  5. Type javac FirstProgram.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 FirstProgram and press enter.

  8. Your output should say "My first Java program"

Screenshots 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