Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
jdk
communitycreator

How to run Java programs using cmd

Linga Richitha

Prerequisites

  1. JDK should be installed.

To download and install JDK, follow the link below:

Executing a Java program

Step 1:

Before running any Java program, the first step is to check if the Java Development Kit (JDK) is available on our Systems.

To check for the availability of Java, run the following command on any terminal:

java -version

Step 2:

In the next step, we will rite a Java program. Here, we will consider a simple “Hello World!” program, as shown below.

class test
{
    public static void main( String args[] )
    {
        System.out.println( "Hello World!" );
    }
}

Now, since Java is both a compiled as well as an interpreted language, we first need to compile our Java-Script. This then creates a Java Byte code that is independent of the platform upon which it has to run. Then, the Java Byte code is converted, line-by-line, to machine code by JVMJava Virtual Machine ,i.e., the Java Interpreter.

Step 3:

To compile our Java program, we need to tell the Java compiler which program to compile. This can be done by:

javac test.java

If no errors exist, then the program will be successfully compiled and the terminal will take you to the next line. If an error exists, we are bound to get some error messages.

Step 4:

Now, after a successful compilation, we need to execute the byte code to get our output from the program. This is done as follows:

java test
Exectuting java program using cmd.
Exectuting java program using cmd.

Note: While executing the Java program, we should be on the same directory where the file is present.

Assumption: Path Variable is set.

RELATED TAGS

java
jdk
communitycreator
RELATED COURSES

View all Courses

Keep Exploring