JAVA Fundamentals | Part 5 | Running JAVA programs using Command Prompt/ Terminal

Leave a Comment
"Write Once, Run Anywhere!"

Hello everyone once again!

I set up the first initial posts with a hindsight of making anyone capable enough to know the jist of making and running a JAVA Program within the first few posts. Today's post is the last installment in that pursuit, teaching you how to make and run a JAVA program code WITHOUT using ANY IDE like NetBeans or Eclipse.
This will surely help you if you are having troubles with downloading/installing/running any respective IDE for JAVA Development Purposes.
Hence, lets begin.


JAVA implements two compilers for the complete execution of its code. The two compilers used are:
  1. JAVAC Compiler
  2. JIT (Just In Time) Compiler
The JAVAC Compiler is used in the Stage 1 of the entire compilation process while the JIT Compiler is used in the Stage 2.

Let us first visualise the entire process as a flowchart:

Execution of a JAVA Program
Execution of a JAVA Program

The source program is first converted into an 8 bit intermediate bytecode by the JAVAC Compiler. This bytecode is the secret to JAVA's platform independence. This bytecode is generated by a compiler (JAVAC) which is uniform across all the platforms JAVA is run upon.
Further this bytecode is acted upon and compiled by the JIT Compiler. The JIT Compiler is also known as Just In Time Compiler. The JIT Compiler is an extremely fast compiler used to convert the bytecode obtained in the previous step into platform-dependent machine code. This platform-dependent machine code is then executed by the CPU.

The JVM or JAVA Virtual Machine is the framework upon which the JAVA bytecode execution takes place. The presence of a virtual machine means that the coder doesn't directly interact with the machine he/she is working upon and instead interacts with the Virtual Machine which in turn interacts with the machine directly. The JVM is responsible for converting the JAVA bytecode into the host machine's machine code.

The JDK we downloaded in the earlier posts, is actually a package which contains the JRE and the tools needed to develop JAVA Applications, such as the JAVAC Compiler. The JRE or JAVA Runtime Environment is responsible for executing the applets as we will discuss in the next paragraph. However for all the developmental purposes we need the JAVAC Compiler. And the JDK contains both of those tools. Unlike us, people who are not into developmental field, need just the JRE.

The JRE contains the JVM and the associated class libraries needed for execution of JAVA bytecode. JVM then uses those libraries for execution. For the sole purpose of execution of JAVA programmes, the JRE is sufficient. However for writing JAVA programmes yourself, a JDK is a must.

JAVA's Structural Hierarchy
JAVA's Structural Hierarchy

At this point perhaps it should be useful knowing that JAVA was developed majorly for providing a uniform standard language across browsers. For example all the applets we see on websites are coded in JAVA. HTML5 has largely taken over this in today's world but at the time when browsers were new, JAVA was the standard used across all browsers. However as the world exploited the platform independence of JAVAM it began appearing in a lot more places than conventional web browsers.


And with that we end with forming the background for today's lesson. Lets start with learning how to run a JAVA Code from the Command Prompt (in Windows) or Terminal (in Linux).

Open your basic text editor, like Notepad in Windows. Write down all of your code in it.
For example let us write down the following code for the addition of two numbers onto notepad.

import java.util.Scanner;
public class AddTwoNumbers
{
    public static void main(String[] args)
    {
        System.out.printf("%n%n%s", "Enter the first number: ");
        Scanner input=new Scanner(System.in);
        int num1=input.nextInt();
        System.out.printf("%n%s", "Enter the second number: ");
        int num2=input.nextInt();
        System.out.printf("%n%s%d%n%n", "Hence sum of the two numbers entered is: ", (num1+num2));
    }
}


Notepad Code
Notepad Code


Save the file as filename.java at

C:\Users\(your_user_name)

For example, I'll save it as

AddTwoNumbers.java at C:\Users\Shubham 


Now that you've written and saved your code,
Open the Command Prompt/ Terminal. Before you will be able to see its output, you need to compile it. Remember the stage 1 of compilation of any JAVA Program we just discussed? Yes! You need to invoke the JAVAC Compiler. For this you need to execute the command

javac filename.java 

In the above case, the command would look like:

javac AddTwoNumbers.java

and press Enter.
A normal execution would look like this.


Invoking JAVAC Compiler using Command Prompt (CMD)
Invoking JAVAC Compiler using Command Prompt (CMD)

If the JDK is not properly installed on your system you'd need to look if you modified the Path Environment Variables properly. If you are not sure, take a look at our following tutorial on the same here:

JAVA Fundamentals | Part 1 | Installing The Tools (JDK And IDEs)

Another common mistake is that the filename and the classname should be the same. Notice:

classname and filename both should be same
classname and filename both should be same

Okay. So now assuming your program is compiling normally, you need to type these final set of  commands for seeing the output of your code:

java filename

In our case, it'd be

java AddTwoNumbers

Following this you should be able to see the valid output of your code as following.

Running your JAVA program
Running your JAVA program

It should be noted here that I entered those two numbers as input in just the same manner as I would have entered them using any IDE.
Also, the filename.java could have been saved at any place in the computer. But for the sake of simplicity, I saved it at

C:\Users\(user_name)

because as you can see, the command prompt opens at that very directory by default. If you save your file at a location different than the default one, you'll have to navigate to that location before you can compile your program.
For example, if you saved your program at the Desktop, you'll need to enter the following commands to reach the Desktop Directory and then compile and run the program as shown below:

Compiling and Running our JAVA Program from a different than default directory
Compiling and Running our JAVA Program from a different than default directory

As you can see, the command

cd C:\Users\Shubham\Desktop

was used for navigating to the folder Desktop. The cd command refers to the Change Directory command. It is followed by the complete path address of the directory which contains the .java file we want to execute.

And with this we complete today's lesson and also the fundamental series of our JAVA Tutorials. Hope you enjoyed them! Keep tuned in for the advanced lessons from now on. I'd be happy to answer any queries in the comments section.

Take care :)



0 comments:

Post a Comment

Powered by Blogger.