×

How to run Java Program?

How to run Java Program

In this section, we will learn how to write, compile and run a Java program in Command Promptusing notepad.

In order to run a Java program, we have to install JDK (Java Development Kit) in oursystem that provides the runtime environmentfor the applications. After the JDK installation, path must be set properly otherwise we cannot run the code. There are two ways to set the path of JDK, click here to learn.

Follow the steps given below to run a Java program:

  • Open Notepad.
  • Write a Java program into it, and save the file with the extension .java

Note: The name of the file should be same as that of the class name.

  • Open the command prompt (CMD), enter the commands and run the Java program.

Note: Before moving ahead in this section, ensure that JDK is successfully installed and class path is properly set.

Let’s follow below steps to run a Java program:

Step 1: Open the Code Editor and type the basic Java code. Save the file with the name MyJavaProgram.java.

 public class MyJavaProgram{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number");
int n = sc.nextInt();
System.out.println("Your given number is "+n);
}
} 

Step 2: Open the Command Prompt. Set the directory where we have saved the Java file. In our case, the Java file is saved in D:\Project directory.

run Java Program

Step 3:First, we will compile the Java program by using the javac command.

javac MyJavaProgram.java

It generates a .class (byte code) file in the same folder. If there is any error in the code, it shows that error with line number.

run Java Program

Oncewe resolve the error and run the command again, a .class file will be created in the same folder (D:\Programs).

run Java Program
run Java Program

Step 4: To run the .class file (byte code), we use the java command followed by the class name.

java MyJavaProgram

Once the program is error free, and the class file is created, the Java program runs successfully, as shown below:

run Java Program

Note: You can also a run the Java program directly using different user-friendly code editors like Visual Studio Code, Eclipse, Atom, etc. as these editors have their own virtual environments to run the program.

In this way, we have learned how to run a Java Program.


Related Topics

What’s new in Java 12

On March 19th, 2019, the Java 12th edition was released. After releasing this edition, they have decided to release every new edition every six months. This version is the advanced...

5 minutes read.

Determine the Upper Bound of a Two-Dimensional Array in Java

Multi-Dimensional Array Multi-faceted exhibits in Java are regular and can be named various clusters. Information in a two-layered bunch in Java is put away in 2D even structure. A two-layered collection...

3 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.

Fall through in Java

In this article, you will be acknowledged about the fall through condition in Java along with an example program. Fall through It is a situation where there isn't a break statement in...

3 minutes read.

Java Enum

Enumerations are used in programming languages to represent collections of named constants. For instance, the four suits in a deck of playing cards might represent four iterators named Club, Diamond,...

3 minutes read.

Java Math sinh() Method

The sinh() method of Java Math class returns the hyperbolic sine of the specified double value. Syntax: public static double sinh(double x) Parameters: The parameter ‘a’ represents the number whose hyperbolic sine is to...

2 minutes read.

Java Macros

Macros are additions to the JDK 7 compiler in Java. Compile time macros are added and supported. Macros are Java classes that are instantiated and executed during the compilation process....

2 minutes read.

Java Networking

Networking Networking is a way of communicating the devices. It is made up of various technologies like computers, switches, routers that are interconnected and share data among themselves. The two common network protocols: 1. TCP/IP TCP stands for...

10 minutes read.

Java FileNotFoundException

FileNotFoundException is another exception class accessible in the java.io bundle. The exemption happens when we attempt to get to that document which isn't accessible in the framework. It is a checked...

5 minutes read.

Java Math decrementExact() Method

The decrementExact() method of Math class returns the argument which is decremented by one, throwing an exception is the result overflows an int or long. Syntax: public static int decrementExact (int a) Parameters: The...

1 minute read.

Converting Roman to Integer Numerals in java

In this article, you will be acknowledged about the process of conversion of roman numbers to integers in java. The most important approaches are discussed and also the implementation of...

3 minutes read.

String Manipulation in Java

String Manipulation in Java In Java, string manipulation is a common task performed by programmer. Java String class provides many built-in functions that are used to manipulate string. The manipulation of...

4 minutes read.

Java RMI

What is RMI in Java? A framework for developing distributed Java applications is provided by the RMI (Remote Method Invocation) API. An object can call methods on an object running in...

4 minutes read.

Bucket Sort in Java

Bucket Sort in JavaBucket sort is also called bin sort. Bucket sort first puts the elements of the array or list into different buckets. The first bucket contains elements of the smallest value. The...

11 minutes read.

Java Command Line Arguments

Java command line arguments Command line argument is an input or argument to the program when you run the program. In Java, we can take the inputs from the console, and...

2 minutes read.

Java AWT

Java AWT Java programming is used to develop different types of applications like window-based applications, web applications, Enterprise applications, or mobile applications. For creating standalone applications, Java AWT API is used....

11 minutes read.

Concurrent Linked Deque in Java with Examples

Introduction Java's concurrent-linked deque, which holds its items as linked nodes, is unconstrained and thread-safe. Concurrent Linked Deque allows for element removal and addition on both sides because it implements the...

4 minutes read.

How to sort a String in Java

Sorting is the process of putting the elements in a certain order, either ascending or descending. Mostly the alphabetical order or natural order is used for a string. In other...

5 minutes read.

Java Math sin() Method

The sin() method of Java Math class returns the trigonometric sine of the specified angle. Syntax: public static double sin(double a) Parameters: The parameter ‘a’ represents an angle measured in radians. Return Value: The sin() method...

1 minute read.

User Defined Custom Exceptions in Java

In this tutorial, we will discuss user-defined custom exceptions with examples. Introduction In Java, we have proactively characterised, Exception classes, for example, ArithmeticException, NullPointerException, ArrayoutOfBound and so on. These built-in exceptions are...

3 minutes read.