How to Run a Java Program from the Command Prompt

featured img

Unleash the power of Java, a language that fuels everything from sprawling enterprise systems to sleek Android apps. Still a cornerstone of modern coding, Java lets you wield its might directly from your Windows Command Prompt. Ready to compile and execute with lightning speed? Dive in and discover how to command Java from your command line.

Verify Java Installation

Ready to unleash your Java potential? First, ensure Java’s installed and ready to rock on your system. Let’s confirm it’s there!

Unleash Java’s Secrets: Administrator Mode Holds the Key!

Want to know if Java’s lurking within your system? It’s easier than you think! Simply summon the Command Prompt with administrator privileges and whisper the magic words:java -version. Prepare to be amazed as Java, if present, reveals its version number, unlocking a hidden piece of your system’s inner workings.

Otherwise, “java” would fail to be recognized an external or internal command. This means you have to install it manually.

The command prompt shows "Java is not recognized as an internal or external command."

Also read : you can use the Command Prompt to find and open PC files.

There are multiple ways to install Java in Windows. The easiest way is to use WinGet software.

Install Java in Windows Using WinGet

Want the newest Java? Oracle’s got it – currently Java 24. But for rock-solid stability, we’re installing the Java 21 LTS release. Consider it the dependable workhorse.

Open Command Prompt or PowerShell in administrator mode, and run the following.

“` winget install Microsoft.OpenJDK.

(

Version number

)

“`

Java being downloaded and installed via Winget.

Some additional programs such as the Microsoft Build of OpenJDK will be installed within the same window.

Auxiliary program OpenJDK being installed along with Java in Winget.

Eventually, you will see aSuccessfully installedstatus.

Java installation is shown as successful in Windows PowerShell.

Related : check how to fix WinGet is not recognized in Windows problem which takes care of all its issues.

Conventional Installation of Java Development Kit (JDK)

Unleash the power of Java with the JDK your code’s personal forge. This indispensable toolkit fuels development on any platform, transforming ideas into dynamic realities.

Head over to the official Java SE download page and grab the newest version. For rock-solid stability, we’re sticking with the latest LTS (Long-Term Support) release.

Java latest LTS stable version being downloaded from official Oracle website.

Ready to dive in? Under the Windows tab, your adventure begins! Choose your weapon: the sleek x64 installer (as seen on screen), the stealthy x64 compressed archive, or the powerful x64 MSI installer. Each path leads to the same destination – your download awaits!

Heads up!If you’re just looking torunJava programs, steer clear of the “Java SE Development Kit for Java SE Subscribers” on the download page that’s for developers. Need the JRE for Windows? It’s moved! You’ll find it on a different page now. Happy coding (or running!).

Downloading the latest x64 installer (EXE) for Java in Windows.

Installation is a breeze – just like setting up any other application! Simply launch the installer and follow the on-screen prompts. New to Oracle? A quick registration will get you started.

Heads up! Java typically sets up shop in yourProgram Filesfolder on Windows. Jot down that location; you’ll need it later to unleash Java from the Command Prompt. After installation, a quick peek in File Explorer will confirm everything’s in its place.

File Explorer location of Java Development Kit in Microsoft.

Installation should only take a blink. If it’s dragging, unleash Task Manager and banish background apps. Then, give the software another shot.

You will see aSuccessfully Installedstatus in the end.

Related : learn to enable copy-paste in the Windows Command Prompt.

Running a Java Program From the Command Prompt

Create a simple Java program like the one below using Notepad or another text editor.

“`

public

class

HelloWorld

}

“`

Make sure to save the file with the extension “.java” rather than “.txt.”

Save Notepad edit as .JAVA file instead of the regular extensions.

Open the Command Prompt from the Windows Start Menu, and don’t forget to run it asAdministrator.

Dive into your Java project! First, navigate to its home using thecdcommand. This directory is your Java control center in Windows, where you can tweak all the necessary settings.

“`

cd

Documents

[

Java-program-folder

]

“`

Using cd command to map to exact folder path containing Java installation.

Remember the days of painstakingly updating your system’s PATH variable every time you upgraded Java? Those days are over. Forget wrestling with directory paths just to compile your code. The good news is you can ditch the PATH juggling act. Now, simply point your command line directly to the folder holding your .JAVA file and compile.

Compile the Java program with thejavaccommand as shown below.

“` javac

“Program Name”

.java “`

Java program executed in the Prompt using "javac" command.

Don’t blink, because the magic happens behind the scenes. But typedirin your terminal, andpoof!A new file, stamped with the “.class” extension, materializes – proof your program has been brought to life.

The .CLASS extension of a Java program that was compiled in a folder.

Use thejavacommand to run your program:

“` java

“Program Name”

“`

Viewing the output of a simple Java program that displays a common Hello World printout.

You’ll see the program run within the Command Prompt window.

Unleash the power of Java directly from your Command Prompt! Forget clunky interfaces – with a few lines of code in Notepad, you can transform your command line into a dynamic calculator. Imagine instantly adding, subtracting, multiplying, and dividing numbers with ease. A simple program is all it takes to bend the Command Prompt to your will.

“`

public

class

Sum

}

“`

Save a sum file in Java in Notepad as shown above.

Back to the Command Prompt! Unleash the power ofjavacto compile your Java creation, then summon it to life with thejavacommand. Witness the grand finale: the summation, revealed in glorious output.

The arithmetic sum of two pre-stored numbers displayed when java is run.

Ready to ditch the Java jitters in Windows Command Prompt? Beyond the basic “Hello, World!”, a universe of ridiculously simple commands awaits even the greenest coder. Unlock the secrets, and watch your Java skills blossom!

  • javac *.java: compile all the java files saved in a folder
  • javac Timenow.java: display local time on your cmd
  • javac -Xlint Hello.java: display warnings while compiling a program

Related : learn how to use Windows Defender from the Command Prompt.

Setting a Permanent PATH for Java Folder

Think of that command like a temporary tattoo for your system’s brain. It tells your Java compiler where to find its tools forthissession only. Poof! Gone with the next reboot, leaving no trace behind.

Tired of your Java programs sputtering after a restart? Permanently setting your Java compiler PATH is your one-way ticket to smooth sailing. Think of it as planting a flag that tells your computer exactly where to find the Java tools, ensuring lightning-fast launches from the Command Prompt, every single time.

Follow the steps below to change your PATH variable for all future sessions.

Unleash hidden power! Dive into your system’s core. From the Start Menu, access Control Panel, then venture into System and Security. Spot “System” and click. Now, hunt for “Advanced System Settings.” A new window appears. Click the “Advanced” tab, then the gateway to customization: “Environment Variables.”

Select thePathvariable, then click the Edit button.

Setting a Path variable in the list by clicking Edit.

ClickNewto add a new directory to your path.

Point your system to the heart of Java development. Drop theJDKdirectory path – not its weaker JRE sibling – into the designated field. Make sure you got the right path.

You will now find JAVA_HOME added as one of the system variables that you can directly point to.

The finalized path with folder location of Java saved in Environnent variables.

Lastly, clickOKto commit to the change.

Frequently Asked Questions

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

“Access Denied! Taking Control When Windows Blocks Your Java Saves”

Frustrated by the “You don’t have permission to save in this location” error when working with Java files in Windows? Even with admin rights, this roadblock can appear unexpectedly. But don’t worry, you can reclaim control. Here’s how to bypass the permission snag: Rightclick on your Java folder, dive intoProperties, then navigate toSecurity>Advanced>Select User or Group. It’s time to tell Windows who’s boss!

Unlock Java’s Full Potential: Reclaim Ownership and Unleash Your System!

Tired of Java’s security restrictions holding you back? Take control! Dive into the Advanced Security settings and swap the “System” ownership for your personal user account. Not sure which account is yours? Simply use the “Check Names” feature – a lifesaver! Type in common options like “Desktop,” “Administrator,” or “Users” until your account pops up, granting you the keys to full folder permissions. Click “OK,” seal the deal, and experience Java without limitations!

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

Banish the “Java is not recognized” error forever! The secret? Unleash Java by adding itsbindirectory to your system’s PATH. Consider it like giving your OS the map to Java’s treasure.

Windows Command Prompt doesn’t show the results of Java command. How can I fix it?

Java command vanished into the Windows void? Command Prompt swallowing your code’s output? Don’t despair! Two quick fixes can resurrect your results. First, unleash the Admin powers: launch Command Prompt in Administrator Mode. Still nothing? Time for some file surgery. Track down “Java.exe,” right-click for “Properties,” and venture into the “Compatibility” tab. See that box ticked for “Run this program as an administrator”? Untick it. Problem solved.

What is the difference between Java and Javascript?

Don’t confuse Java with Javascript, as they are two different entities:

Java, a titan born from Sun Microsystems between 1991 and 1995, predates its nimble cousin, JavaScript. Emerging later from Netscape, a now-vintage browser company, JavaScript provides a lightweight scripting experience. Though often confused, JavaScript maintains relevance as the go-to language for dynamic browser experiences.

  • Java is a compiled program, whereas Javascript is interpreted.
  • Java is a static typed program, whereas Javascript is dynamically typed.
  • Java uses classes, and Javascript uses prototypes.

Thanks for reading How to Run a Java Program from the Command Prompt

Getairo
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.