refresh java logo

My First Java Program

It's easy to start writing program in java. To write your first java program, open an editor like notepad, notepad++ or any other editor and write bellow line of code.

 class MyFirstProgram
   {
      public static void main(String [] args)
         {
            System.out.println("refresh java");
         }
   }							

The program given above is a very basic java program which will just print a string message "refresh java" in console after execution. We will understand different terms and their meaning used in above program in next tutorial. The image given below shows the above program written in Notepad editor.


first Program

After writing the code in editor, the next step is to save the file as per the convention given by java which says the file name should be same as the class name with .java extension. So let's save the above program file as MyFirstProgram.java. In above program MyFirstProgram is the name of class. .


first Program

Is it mandatory to save file name as class name ?

No, if it doesn't contains a public class. If it contains a public class then it must be saved with public class name. It's good to follow the convention given by java.

After saving the file, the next step is to compile the java program using javac command. Before compiling ensure that Path variable for java is set in your system. To compile above program, open the terminal CMD, and move to the directory where above program is saved. Now execute the following command

javac MyFirstProgram.java// Don't forget to add .java extension while compiling.

To compile a program in java, you need to write program file name with .java extension after javac command while compiling the program.

Once programmer executed above command, java compiler will check for any syntax error in the program, if it doesn't find any error, a .class file as MyFirstProgram.class will be generated at the same location where program is saved.

What is .class file ?

A .class file is a compiled code of your java program into another language which is called Bytecode. It's an intermediate representation of your java program. Refer Bytecode section for more detail about this.

Once the .class file is generated, the next step is to run it using java command which uses only this class file at the time of execution. Execute following java command to run the above java program.

java MyFirstProgram // Don't add .class extension while running.

To run a java program, you need to write .class file name without .class extension after java command while running the program.

Once you execute the above command, it will display the output in console that is CMD in this case. The image below shows the compilation and execution of above program. In above case program is saved inside D:\JavaPrograms directory.

java command to run program

If you are in a directory other than your classpath(.class) directory, you can use -cp or -classpath attribute of java to provide the classpath directory while executing the program. The current directory in the image below is D:\Softwares while the .class file is in D:\JavaPrograms. Using the -cp or -classpath variable we can execute the program MyFirstProgram available in D:\JavaPrograms directory.

classpath variable in java

From where java starts execution of program ?

From main method. It's the starting point of program execution.

Does java uses .java file while running the program ?

No, it uses only .class file while running the program.

The next tutorial will give you a brief definitions of different terms that we have used in above program and will cover some of the other aspects of a program as well. Detailed descriptions will be given in later sections.

★★★
  • Every program must have at least one class, it can have more than one class.
  • There should be only one main method having argument type as String [] in a class.
  • Multiple classes in a program can have same main method.
  • If there are multiple classes in a program, It must be saved by public class name if any.
  • Every starting { must have a balanced } for it.