Step-by-Step Guide to Executing a Java Program via Command Prompt

Step-by-Step Guide to Executing a Java Program via Command Prompt

Java consistently ranks among the most widely utilized programming languages across the globe, serving as a foundation for enterprise software, web applications, desktop solutions, and Android development. If you’re using Windows, you can swiftly compile and execute Java programs directly from the Command Prompt. This guide will walk you through the steps to successfully get started with Java on your Windows machine.

Confirming Your Java Installation

Before you can execute Java programs, ensure that Java is installed on your system. Follow these steps to verify your installation.

Launch the Command Prompt with Administrator privileges and enter the command: java -version. If Java is installed, you will see version information displayed. If you encounter an error indicating that “java”is not recognized as an internal or external command, you’ll need to install Java.

Command prompt displaying that Java is not recognized as an internal or external command.

There are several methods to install Java on Windows, but the simplest is through WinGet.

Installing Java Using WinGet

To obtain the latest version of Java, visit its official Oracle download page. Although the most recent release is Java 24, we’ll focus on the Long-Term Support (LTS) version, Java 21, for its enhanced stability.

Open Command Prompt or PowerShell as an administrator and execute the following command:

winget install Microsoft. OpenJDK.(Version number)

Downloading and installing Java using WinGet.

During this process, additional components, including the Microsoft Build of OpenJDK, may also be installed.

Installation of auxiliary program OpenJDK along with Java via WinGet.

Once completed, you should see a Successfully installed message.

Successful installation of Java displayed in Windows PowerShell.

Traditional Installation of JDK (Java Development Kit)

The Java Standard Edition Development Kit (JDK) is crucial for Java development across various platforms. Begin by visiting the official site and selecting the latest Java SE download. Again, we’ll opt for the latest LTS version to ensure stability.

Downloading the latest stable version of Java from Oracle's website.

On the Windows tab, you can download one of the following options: an x64 installer, an x64 compressed archive, or an x64 MSI installer.

Downloading the x64 installer for Java on Windows.

Run the downloaded installer and follow the prompts. If you don’t already possess an Oracle account, you will need to complete the registration process.

Note the installation directory, typically found in Program Files, as this information will be important later for executing Java via Command Prompt.

Folder location of the Java Development Kit in Windows.

The installation should conclude within a few moments. Should it take longer than expected, consider closing other applications via Task Manager and attempt the reinstallation.

You will see a confirmation message stating Successfully Installed at the end of the process.

Executing a Java Program from the Command Prompt

Create a simple Java program using Notepad or any text editor, like the example below:

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

Ensure you save your file with the “.java” extension, rather than “.txt”.

Saving a file in Notepad with a. JAVA extension.

Open Command Prompt from the Windows Start Menu, making sure to run it as Administrator.

Change the working directory to the location of your Java file using the cd command:

cd Documents[Java-program-folder]

Using the cd command to navigate to the Java program folder.

In earlier versions, you were required to specify the Java version while accessing the bin directory. However, currently, you only need to navigate to the specific folder where your. JAVA file is stored.

To compile your Java program, use:

javac "Program Name".java

Executing a Java program in Command Prompt using the

Note that compilation produces no immediate output. However, by using the dir command, you will see a new file with a “.class” extension in your directory, indicating successful compilation.

The. CLASS file created after compiling a Java program.

Run your program now with the following command:

java "Program Name"

Output of a simple Java program displaying 'Hello, World!'

You should see the output within the Command Prompt window.

Java allows for various operations through the Command Prompt, such as performing arithmetic calculations. To do so, you can create a simple program and execute it through Command Prompt to observe the results.

public class Sum { public static void main(String[] args) { int a = insert value, b = insert value; System.out.println("Sum: " + (a + b)); } }

Saving a sum calculation Java file in Notepad.

After saving, return to the Command Prompt to recompile using javac followed by running the program with java to see the sum output.

Result of an arithmetic sum displayed after executing the Java program.

As you begin utilizing Java in the Command Prompt, here are a few additional commands for beginners:

  • javac *.java: Compile all Java files in a folder
  • javac Timenow.java: Display the current local time in Command Prompt
  • javac -Xlint Hello.java: Show warnings during compilation

Configuring a Permanent PATH for Java

The temporary changes made in the command prompt do not establish a permanent PATH for your Java compiler. To ensure your Java programs run seamlessly across sessions, you can modify the PATH environment variable.

Follow these steps to set your PATH variable for future use:

  1. Open the Control Panel and navigate to System and Security, then select System.
  2. Click on Advanced System Settings and go to the Advanced tab.
  3. Click on the Environment Variables button.
  4. Locate the Path variable and click Edit.
Editing the Path variable to include Java's directory.

Next, click on New to insert a new directory into your PATH. Paste the directory path of your Java Development Kit (JDK), ensuring it is not the Java Runtime Environment (JRE) directory.

Once added, the environment variable JAVA_HOME will be established, making it easy to reference later.

JAVA_HOME path saved in Environment Variables.

Finally, click OK to save your changes.

Frequently Asked Questions

How can I resolve “You don’t have permission to save in this location” when saving Java files?

This error may occur despite being logged in as an administrator. To remedy this, right-click on the Java folder, select Properties, and navigate to Security -> Advanced -> Select User or Group. In the security settings, amend the Owner from System to your current user account. You can verify the correct name using Check names. Enter text like Desktop, Administrator, or Users to grant full permissions. Click OK to save changes.

How can I fix the “Java is not recognized as an internal or external command” error?

To resolve the issue of “Java is not recognized”, ensure that the JDK’s bin directory is added to your computer’s PATH, as outlined earlier in this guide.

Why doesn’t Windows Command Prompt display results of Java commands?

If your Command Prompt isn’t showing results from Java commands, try running it in Administrator Mode or verify the properties of your “Java.exe” in the installation directory. In the Compatibility tab, uncheck Run this program as an administrator.

What distinguishes Java from JavaScript?

Java and JavaScript are fundamentally different languages:

Java was developed by Sun Microsystems between 1991-1995, while Javascript was created later by Netscape, a browser company. Java is a compiled language; JavaScript is interpreted. Java is statically typed, whereas JavaScript is dynamically typed. Java utilizes class-based structures, while JavaScript employs prototype-based structures.

Source & Images

Leave a Reply

Your email address will not be published. Required fields are marked *