×

How to run Java Program in Command Prompt

How to run Java Program in Command Prompt

In this section, we will learn how to write, save, compile, and execute or run a Java program in the Command Prompt.

Note: One must ensure that Java is installed in one’s system and PATH variable is set properly. To check whether the PATH variable is set or not, open the command prompt by writing cmd in the search area of the taskbar and press enter.

The other approach to open the command prompt is to press Windows + R, then type cmd and press the OK button.

 In the command prompt, type the command javac and press enter.

If the PATH variable is set properly, the information mentioned in the above snapshot is shown; otherwise, the command not found! message is shown.

After checking for the javaccommand, enter the command java and press enter.

Observe that after the command java, some information is shown. It denotes that Java is installed in the system.

Choosing Text Editor

To write a Java program, a text editor is required. Any editor of one’s choice does the job. However, in this section, we will stick with notepad++.

In the search area of the taskbar enter notepad++ and press enter.

or press Windows + R, then type notepad++ and press the OK button.

After pressing enter key or OK button, it opens the notepad++.

Now, write any Java program on one’s choice. Let’s write a simple Hello-World Java program.

FileName: HelloExample.java

 public class HelloExample
{
// main method
public static void main(String argvs[])
{
// A given character array
char[] chArr = {'w', 'e', 'l', 'c', 'o', 'm', 'e', ' ' , 't', 'o', ' ', 'T', 'u', 't', 'o', 'r', 'i', 'a', 'l', 'A', 'n', 'd', 'E', 'x', 'a', 'm', 'p', 'l', 'e'};
// Converting the character array to a string
String str = new String(chArr);
// printing the string
System.out.println(str);
}
} 

Save the file and provide the name of the file. The name of the file should match with the class name. The extension of the file has to be .java. We have saved it to the Java folder of the D drive.

Now, open the Command Prompt.

Now move to the folder where we save our file. Currently, drive C is selected. Therefore, first, we have to change our drive from C to D.

Type D: and press enter to change the drive from C to D.

Now, we have to select the Java folder where we saved our file. Type cd Java and press enter.

Eventually, we have reached our destination folder. Now, to compile the file, use the javac command along with the filename and its extension, and press enter.

If the code is error-free, we get a .class file. Otherwise, a compilation error is shown by the compiler, and .class file does not get generated. The compilation error must be resolved to proceed further.

After the compilation step comes the execution step. To execute a Java program, the java command is used along with the file name. This time the extension of the file is excluded.

On execution, the program prints the welcome statement.


Related Topics

Heap Implementation in Java

The root node or parent node of a Java heap is compared to its left and right offspring, and the children are then ordered in line with the comparison.Assuming that...

9 minutes read.

Group by in Java 8

By using this groupingBy() method, developers can directly able to perform the "GROUP BY" operation. The thing is, now, the Java 8 programming language allows the programmers to do this...

3 minutes read.

Java Program to remove duplicate characters in a string

In strings, we can find many characters present one or more times in a string; accessing a particular character is difficult due to repetition. Duplicate characters will present in the...

5 minutes read.

New Features of Java 14

 Features like Switch expressions and text blocks which are previewed in Java 13 version are standardized in Java 14. Features in Java 14 Switch ExpressionText BlocksRecordsNull Pointer ExceptionsInstance ofPackaging ToolGarbage collectors 1.Switch Expressions Switch...

3 minutes read.

Int vs Integer in Java

In Java, numerical data can be stored using int or Integer. int and Integer both have different definitions but similar usage in Java. What is int in Java? int is a primitive...

4 minutes read.

How to Convert Octal to Decimal in Java

How to Convert Octal to Decimal in Java There are two methods to convert Octal to Decimal: Using parseInt() method Using user-defined logic Using Integer.parseInt() method The Integer.parseInt() method is a static method...

2 minutes read.

Anonymous Function in Java

A function defined as being unbound from an identifier is called an anonymous function. Because they permit access to variables within the scope of the contained function, these are a...

4 minutes read.

Program to Find Square Root of a Number Without sqrt Method in Java

The Java Math class sqrt () function can be used to determine the square root of a number. In this section, we'll write a Java program to find a number's...

3 minutes read.

Inheritance Program in Java

Inheritance Program in Java Inheritance is one of the important pillars of Object-Oriented Programming that facilitates parent-child relationships in programming. Using inheritance, we can create a new class with the help...

7 minutes read.

This Operator Using in Java

this keyword in Java has a wide range of applications. this reference variable in Programming language refers to the object of interest. This keyword is used in Java. this keyword is...

5 minutes read.

How to write basic Java Programs

Java Basic Programs In this section, we will learn how to write basic Java programs. But first we need to take care of the following requirement list. To execute a Java program,...

4 minutes read.

Magic Square in Java

A square with numbers is referred to as a magic square. The numbers in a magic square of order n are arranged to ensure that the sum of all of...

4 minutes read.

User Defined Exception in Java

An exception is an error (run time error) that happened while a program was being executed. The program stops abruptly whenever the Exception occurs, and the code after the line...

3 minutes read.

Java Write File

In this post, we'll examine various Java programming methods for writing into files. Since this class is character-oriented due to how it is used in file handling in Java, it...

4 minutes read.

Java Math hypot() Method

The hypot() method of Math class returns the square root for the expression x2 + y2 without the intermediate underflow or overflow . Syntax: public static double hypot(double x, double y) Parameters: The parameters...

2 minutes read.

The Maximum Rectangular Area in a Histogram in Java

Continuous bars should be used to form the largest possible rectangle. We'll assume in the interest of convenience that each bar's width is 1. Naive Approach In this method, each bar will be...

6 minutes read.

How to add 24 Hours to Date in Java?

In this tutorial, we will learn how to add 24 hours to the local or current date in Java language. We will begin our topic with basic concepts and would...

2 minutes read.

Number Pattern Programs in Java

Number Pattern Programs in Java: Number pattern programs are part of pattern programs. In the previous section, we have learned the approach to print the pattern program in Java. To...

6 minutes read.

Nth node from the end of the Linked list in Java

In talks with leading IT organizations like Google, Amazon, TCS, Accenture, etc., this extremely intriguing subject is constantly brought up. The goal of the problem-solving exercise is to evaluate the...

6 minutes read.

Java Integer remainder Unsigned() method

The remainderUnsigned() method of Java Integer class returns the unsigned remainder by dividing the first and second argument. Syntax public static int remainderUnsigned(int dividend, int divisor)  Parameters The ‘dividend’ and ‘divisor’ represents the value...

1 minute read.