×

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

Generics vs Wildcard in Java

In generic programming, the question mark (?) is often referred to as the wildcard. It stands for a mysterious type. The wildcard can be used for many different contexts, such as...

4 minutes read.

Rehashing in Java

The most crucial idea mostly in the data structure is hashing, which is utilized to change a particular key into some other value. The hash function can be used to...

7 minutes read.

Java Return Keyword

The return keyword in Java is used to end a method's execution. the caller receives the return, followed by the appropriate value. The return type of the method, such as...

3 minutes read.

Java LinkedHashSet

LinkedHashSet in Java with Example Java LinkedHashSet extends HashSet and Implements the Set interface. It doesn’t contain only duplicate values like HashSet. It also permits the null elements. It maintains the order...

5 minutes read.

Java Math.multiplyExact() method

In java, we are provided with multiplyExact method in the Math module. This Math module belongs to the java.lang package. In general, this method returns the product of the provided...

2 minutes read.

Client Server Program in Java

Client Server Program in Java The client and server are the two main components of socket programming. The client is a computer/node that request for the service and the server is...

7 minutes read.

Java Session

Session indicates interval of time. A session is a simple  time interval in which servers and client interacts. To maintain the state of the client or user we use technologies...

5 minutes read.

MVC in Java

A well-known design pattern is Model-View-Controller. The discipline of web development. We can organize our code in this manner. The document stipulates that a program or application must include a...

4 minutes read.

Java Math rint() Method

The rint() method of Java Math class returns the double value which is close to the specified argument and is equal to mathematical integer. Syntax: public static double rint(double a) Parameters: The parameter ‘a’...

1 minute read.

Java InputStreamReader

What is InputStreamReader?An InputStreamReader is a converter between byte and character streams: It reads bytes and converts them to characters with the help of a charset. The charset it uses can...

4 minutes read.

Deadlock Prevention and avoidance in Java

This tutorial will discuss deadlock prevention and Avoidance in Java programming language. Introduction Multithreading in Java includes deadlock. We can multitask by running several threads concurrently in the multithreading environment. Deadlock occurs...

4 minutes read.

Tug of War in Java

As in the tug-of-war issue, we must divide the given collection of n numbers into two groups of sizes that are equal or nearly equivalent. A minimum difference must exist...

5 minutes read.

Java Math acos() Method

The acos() method of Math class computes the trigonometric Arc Cosine (inverse of cosine ) of an angle. The value returned is between 0.0 to pi. Syntax: public static double acos(double a) Parameters: The...

1 minute read.

Java Integer longValue() method

The longValue()  method of Java Integer class returns a long value for this Integer after a widening primitive conversion. Syntax public long longValue() Parameters NA Specified by This method is specified by longValue in class Number Return...

1 minute read.

Difference between this and super in Java

In this article, you will be very well equipped with the knowledge of this keyword and super keyword in java. You will be able to understand where to implement this...

3 minutes read.

Sorting Algorithms in Java

Sorting Algorithms in Java Sorting is the technique that puts the elements of an array or list either in descending or ascending order. For example, take an array A, whose elements...

3 minutes read.

Java String getChars() Method

Java String getChars() method copies characters from current String to the destination character array . Syntax: public void getChars(int srcBeginIndex, int srcEndIndex, char[] destination, int dstBeginIndex) Parameters: srcBegin - index of the first character...

1 minute read.

Race Condition in Java

Java is a multi-threaded programming language, race conditions are more likely to arise. Mostly because data can change when multiple threads visit the same resource simultaneously. Race conditions are concurrency...

3 minutes read.

Three-way operator in Java

The ternary operator in Java is the only conditional operator that takes three operands. It's a popular one-line substitute for the if-then-else expression among Java programmers. If-else clauses can be...

3 minutes read.

GCD Program in Java

GCD Program in Java The GCD program in Java outputs the GCD of the given numbers. In mathematics, Greatest Common Divisor (GCD), Greatest Common Factor or Highest Common Factor (HCF) of...

14 minutes read.