How to Compile and Run a Java Program from Command Prompt

Java is a versatile programming language used for developing a wide range of applications. While Integrated Development Environments (IDEs) provide a convenient way to write and execute Java code, sometimes it’s necessary to compile and run programs from the command prompt. This blog post will guide you through the process of how to compile and run a Java program using the command prompt.

Prerequisites:

Before getting started, ensure that Java Development Kit (JDK) is installed on your system. You can download and install the latest version of JDK from the official Oracle website.

Step 1:

Write the Java Program First, open a text editor of your choice and write your Java program. Save the file with a .java extension, which is the standard file extension for Java source code files. For example, let’s create a simple “HelloWorld” program that prints “Hello, World!” to the console:

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

Save the file as HelloWorld.java in a location of your choice.

Step 2:

Open the Command Prompt Open the command prompt on your system. On Windows, press the Windows key + R, type “cmd” in the Run dialog, and hit Enter. On macOS or Linux, open the Terminal application.

Step 3:

Navigate to the Java Program’s Directory Use the cd command to navigate to the directory where your Java program is located. For example, if your program is in the “Documents” folder, type the following command and press Enter:

cd Documents

Ensure that you navigate to the correct directory where the HelloWorld.java file is saved.

Step 4:

Compile the Java Program To compile the Java program, use the javac command followed by the filename. In our case, the command will be:

javac HelloWorld.java

The javac command is the Java compiler, and by providing the filename HelloWorld.java, it compiles the Java source code into bytecode. If the compilation is successful, a new file named HelloWorld.class will be generated in the same directory.

Step 5:

Run the Java Program To run the compiled Java program, use the java command followed by the class name (without the .class extension). In our example, type the following command:

java HelloWorld

The java command executes the compiled bytecode, and by providing the class name HelloWorld, it looks for the HelloWorld.class file in the current directory. The output, “Hello, World!“, should be displayed in the command prompt.

Conclusion:

Compile and run Java program from the command prompt provides a fundamental understanding of the Java development process. By following the steps outlined in this blog post, you can easily compile and execute Java programs without relying on an Integrated Development Environment. This knowledge can be particularly useful in scenarios where IDEs are unavailable or when working with automated build systems.

Remember to keep practicing and exploring more advanced Java concepts to enhance your programming skills. By gaining proficiency in compiling and running Java programs from the command prompt, you’ll have a solid foundation for Java development. Happy coding!

That concludes our expanded blog on compiling and running a Java program from the command prompt. We hope you find it helpful. If you have any further questions or need assistance, feel free to ask!

Recommended:

Guide on How to Download Java

Exploring the Java Development Kit (JDK)

What is arraylist in Java?

JavaScript Array

What is variable in JavaScript?

How to define constant in java?

What is the Console in JavaScript.

SQL introduction

What are the basic SQL Command ?

What is array reduce()method in JavaScript?

Reference:

learnwithshikha20@insta

learnwithshikha@facebook

Leave a Comment