First Program in Java

 

Procedure to develop first Java program

Step 1 :

Create a folder to store all your programs. This folder is called the "JavaTechWordWorkingDirectory" Assuming the folder path as (C:JavaTechWorldJavaTechWordWorkingDirectoryAjay01Basics).

Folder Structure

Step 2 :

Open notepad (start > run > type notepad ) and type below code.

Example Program

Step 3 :

Save this file in 01Basics folder with name Example.java

Example Program

Note : Java file name can be user defined name. it is not always mandatory to save java file name with same class name. if class is declared as "public" then only it is mandatory to save the file with class name.

Let us try with different java file name.

Program development is completed. now lte us compile and execute.

Compilation and execution

Step 4 : Compilation

  1. Open command prompt
  2. Change Drive and then directory to current working directory to 01Basics folder.
  3. Then use javac tool to compile Example java file, as shown below.

Compilation of first java program

Compiler has generated ".class" file successfully with name "FirstProgram.class" in 01Basics folder.

.class file

Note : Compiler generates ".class" file with the class name not java file name. check 01Basics folder for ".class" file name.

Step 5 : 

Use java tool to execute FirstProgram class bytecodes file, as shown below.

First Program Output

Compiler Activity :

It takes java file name as its input and generates bytecodes for all classes defined in that java file and store each class bytecodes in seperate ".class" file with name same as class name.

JVM Activity :

It takes class name as its input and searches for a .class file with the given .class name. if it found it reads and loads that .class file bytecodes into JVM, then starts that class logic execution by calling "main" method.

What happen if compiler do if the given java file is not found ?

It throws CE: javac: file not found:

What does JVM do if the given class .class file is not found ?

It throws Runtime Error: Could find or load main class FirstProgram

Error

Leave a Comment

%d bloggers like this: