Login    New User    Forgot Password    Help      

My First Program

Java Tutorials

How to run first program

It’s traditional to start learning a new programming language by writing a simple program . You can think of it as a very simple initiation into the ranks of Java programmers.

The basic steps we will follow for our Hello World program are:

  1. Write the program in Java
  2. Compile the source code
  3. Run the program

Writing the first program

The first thing you need is a place to write your Java code. All Java programs are written using plain text files, therefore you can write them without any fancy software. For your first program, open up the simplest text editor you have on your computer. I’ll be using Notepad.

The program code looks like this:

//Program no 1
// Multiple of Two
class one
{
public static void main(String args[])
{  int i,j;

for(i=1;i<=10;i++)
{
System.out.println(i*2);
}

}
}

You could cut and paste the above code into your text editor, but it’s better to get into the habit of typing it in. It will help you learn Java quicker because you will get a feel for how programs are written, and you will make mistakes! It may sound odd, but any mistakes you make will help you become a better programmer in the long run. Just remember that your program code must match the example code, and you’ll be fine.

For this first program, I’m not going to explain how it works or what all the code means; that will come in later tutorials. At the moment, it’s more important for you to experience the steps you should follow to create and run a program.

Save the program

Save your program file as "one.java". I’ve created a directory in My Documents called "Java" to store all the example Java programs in one place. It makes it easy for me to find them when I need them. You might consider creating a similar directory on your computer.

It’s very important that you save the text file as "one". Java is a little bit picky about filenames. If you look at the code you will see the statement:

class one{

It’s an instruction to call the class "HelloWorld". The filename must match this class name, hence the name "one.java". The extension of ".java" tells the computer that it’s a Java code file.

Open Terminal Window

Most programs that you run on your computer are windowed applications; they work inside a window that you can move around on your desktop. The HelloWorld program is an example of a console program. It does not run in its own window; it has to be run through a terminal window instead. A terminal window is just another way of running programs stored on your hard drive.

To open a terminal window, press the "Windows key" and the letter “R”.

You will see the "Run Dialog Box". Type "cmd", and press "OK".

A terminal window will appear on your screen. Think of it as a text version of Windows Explorer; it will let you navigate to different directories on your computer, look at the files they contain, and run programs. This is all done by typing commands into the window

Java Compiler

Another example of a console program is the Java compiler called "javac". This is the program that will read the code in the HelloWorld.java file, and translate it into a language your computer can understand. This process is called compiling. Every Java program you write will have to be compiled before it can be run.

To run javac from the terminal window, you first need to tell your computer where it is. On my machine it’s in a directory called "C:\Program Files\Java\jdk\1.6.0_06\bin". If you don’t have this directory, then do a file search in Windows Explorer for "javac" to find out where it lives.

Once you’ve found its location, type the following command into the terminal window:

 set path= *the directory where javac lives*

E.g.,

 set path=C:\Program Files\Java\jdk\1.6.0_06\bin 

Press Enter. The terminal window won’t do anything flashy, in fact it will just return to the command prompt. However, the path to the compiler has now been set.

Changing Path

Next, navigate to where your HelloWorld.java file is saved. As I said earlier, my file has been saved in a directory called "Java" in My Documents. Its location is "e:\\Java".

To change the directory in the terminal window, type in the command:

cd *directory where HelloWorld.java file is saved*

E.g.,

cd e:\Java 

You can tell if you’re at the right directory by looking to the left of the cursor.

Compling program

We’re now ready to compile the program. To do so, enter the command:

javac one.java

After you hit Enter, the compiler will look at the code contained within the one.java file, and attempt to compile it. If it can’t, it will display a series of errors to help you fix the code.

Hopefully, you should get the same result as me and have no errors. If that’s not the case, go back and check the code you’ve written. Make sure it matches the example code and re-save the file. Keep doing this until you can run javac without getting any errors.

Tip: Once your one program has been successfully compiled, you will see a new file in the same directory. It will be called “one.class”. This is the compiled version of your program.

Run Program

All that’s left to do is run the program. In the terminal window type the command:

java one

When you hit Enter, the program runs and you will see "Outpur" written to the terminal window.

Well done. You’ve written your very first Java program!

Go Through Again

I’ll admit it’s not the most exciting program ever written, but you now know the steps every Java program has to go through:

  1. Write the Java code in a text file
  2. Save the file
  3. Compile the source code
  4. Fix any errors
  5. Run the program

As the programs get more interesting, these steps will become second nature.

Note : here "one" is name of our program becuase we butit the class in java with this name

 

Java Tutorials