How to Run a Java Program from the Command Prompt

by Dorcas Rodriguez
18 minutes read

How to Run a Java Program from the Command Prompt


Fearured Img: Java in command provoke

Java is one of the most oftentimes supplied programming languages. It sticks approximately a core part of enterprise software, internet create, desktop computer implementations, and Android coding. In Dwelling windows, you can run Java regimen from the Command Incite for quickly compiling and masterstroke. Consumption this guide to earn intake begun.

Corroborate Java Configuration

Before you can run a Java prospectus on your computer system, you’ll must have Java installed, which you can confirm as affirmed.

Amenable Command Incite in Manager mode, and kind: java -version. If Java is present on your system, you will explore its model information number.

Otherwise, “java” would undoubtedly fail to be licensed an outward or internal command. This typicals you have to install it by hand.

The command provoke confirms "Java is not licensed as an internal or outward command."
How to Run a Java Program from the Command Prompt 51

In addition read: you can intake the Command Incite to position and responsive PC records.

There are multiple ideologies to install Java in Dwelling windows. The simplest means is to intake WinGet software.

Mount Java in Dwelling windows Getting grip of serviceability of WinGet

You can pick out the latest Java model on its polices Oracle webpage. At the time of writing, it’s presently Java 24. Yet we will install the latest Long-Term Buttress (LTS)  model, Java 21, as it’s supplemental secure.

Amenable Command Incite or PowerShell in enterprises manager mode, and run the hearkening.

winget install Microsoft.OpenJDK.(Version number)
Java being downloaded and install and installed via Winget.
How to Run a Java Program from the Command Prompt 52

Some second regimen such as the Microsoft Build of OpenJDK will be installed within unmodified residence window.

Auxiliary prospectus OpenJDK being installed along with Java in Winget.
How to Run a Java Program from the Command Prompt 53

Eventually, you will explore a Beneficially installed stomaching.

Java arrangement is affirmed as effective in Dwelling windows PowerShell.
How to Run a Java Program from the Command Prompt 54

Related: check how to heal WinGet is not licensed in Dwelling windows grouse which takes treatment of all its inquiries.

Conservative Configuration of Java Steady technology Kit (JDK)

The Java Standard Edition Steady technology Kit (JDK) is its transferred out compiler. It’s an relevant contraption for making in Java on any type of system.

Go to the polices internet page and click on the latest Java SE download model to start. Over again, we will pick the latest LTS model for soundness.

Java latest LTS secure model being downloaded and install from polices Oracle website.
How to Run a Java Program from the Command Prompt 55

In the Dwelling windows tab, download any type of of the hearkening: an x64 installer (affirmed in the monitor), an x64 compressed archive, or an x64 MSI installer.

Chit: if you have merely humble intake for Java software, shield versus downloading and install the “Java SE Steady technology Kit for Java SE subscribers,” which is on unmodified download internet page. If you dream to intake Java’s JRE arrangement for Microsoft Dwelling windows, it has been shifted to another internet page.

Downloading and install the latest x64 installer (EXE) for Java in Dwelling windows.
How to Run a Java Program from the Command Prompt 56

Sprinted the installer as you would undoubtedly for any type of other prospectus and adhere to the instructions. If you don’t have an Oracle account, you will have to layer registration first.

Chit the Dwelling windows locale wherein Java is being installed, which is commonly Regiment Documents. It will come in useful afterwards as soon as you’re attempting to run Java from the Command Incite. You can alike check it from the Document Vagabond locale once installed.

Document Vagabond locale of Java Steady technology Kit in Microsoft.
How to Run a Java Program from the Command Prompt 57

The arrangement have to be over in merely a few secs. If it is snatching a long time, close all of your other apps from Work Supervisor and reinstall the software.

You will explore a Beneficially Design stomaching in the run out.

Related: learn to permit photocopy-paste in the Dwelling windows Command Incite.

Rushing a Java Regiment From the Command Incite

Invent a humble Java prospectus like the one listed beneath making utilise of Chit pad or another text editor.

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

Render sure to save the record with the expansion “.java” instead than “.txt.”

Preserve Chit pad edit as .JAVA record instead of the invariant expansions.
How to Run a Java Program from the Command Prompt 58

Amenable the Command Incite from the Dwelling windows Start Sustenance option, and don’t forget to run it as Manager.

Consumption the cd command to recovery your kneading directory website to the directory website including your Java prospectus. You can recovery all your Java-relevant arrangements in Dwelling windows within this folder.

cd Documents[Java-program-folder]
Getting grip of serviceability of cd command to map to precise folder route including Java arrangement.
How to Run a Java Program from the Command Prompt 59

In yesteryear, you possessed to recovery the directory website route to mirror the present model of Java while channeling to the bin folder inside. This is undoubtedly no a heap longer crucial. Presently all you have to implement is navigate to the meticulous folder wherein you have preserved the .JAVA record.

Compile the Java prospectus with the javac command as affirmed listed beneath.

javac "Program Name".java
Java prospectus applied in the Incite making utilise of "javac" command.
How to Run a Java Program from the Command Prompt 60

Be advised that you won’t explore anything occur. Yet, if you intake the dir command, you’ll notification a brand name-gradual record in your directory website finishing in the “.refinement” expansion, advising the prospectus has been compiled.

The .CLASS expansion of a Java prospectus that was compiled in a folder.
How to Run a Java Program from the Command Prompt 61

Consumption the java command to run your prospectus:

java "Program Name"
Viewing the outcome of a humble Java prospectus that display display screens a continuous Hello Planet hard copy.
How to Run a Java Program from the Command Prompt 62

You’ll explore the prospectus run within the Command Incite residence window.

There are multifarious things you can implement with Java proper from within the Command Incite. For example, Java can be supplied to add, deduct, build-up, and divide any type of list of variables. All you must implement is invent a humble prospectus making utilise of Chit pad, and watch it occur anytime in Command Incite.

public class Sum {
public static void main(String[] args) {
int a = insert value, b = insert value;
System.out.println("Sum: " + (a + b));
}
}
Preserve a quantity record in Java in Chit pad as affirmed looming.
How to Run a Java Program from the Command Prompt 63

Presently, recovery to the Command Incite and compile the Java prospectus making utilise of javac adhered to by making utilise of the java command. It will sport the summation incentive.

The arithmetic quantity of two pre-stored figures sported as soon as java is run.
How to Run a Java Program from the Command Prompt 64

After you overture kneading with Java in Dwelling windows Command Incite, here are a few other severely humble Java commands that you can establish comfortably as a newbie.

  • javac *.java: compile all the java records preserved in a folder
  • javac Timenow.java: sport man time on your cmd
  • javac -Xlint Hello.java: sport requirements while compiling a prospectus

Related: learn how to intake Dwelling windows Defender from the Command Incite.

Placement a Irreparable PATH for Java Folder

The looming command doesn’t set your Java compiler PATH totally. It kits the ambiance variable for that session, however that recovery will be peripheral.

Placement your Java compiler PATH totally is useful if you pine your compiled Java regimen to run correctly after a PC reboot. This helps launch the sought regimen comfortably from the Command Incite residence window.

Adhere to the moves listed beneath to recovery your PATH variable for all future sessions.

Amenable Manipulate Panel -> System and Counterclaim from the Start Sustenance option and click System. Lookout for Proceeded System Arrangements. Go to Proceeded tab and click the Ambiance variables button.

Pick the Trail variable, after that click the Modify button.

Placement a Trail variable in the list by clicking Modify.
How to Run a Java Program from the Command Prompt 65

Click Picked to add a brand name-gradual directory website to your route.

Paste the directory website route for Java Steady technology Kit into the text box. Over again, earn sure you’re making utilise of the Java Steady technology Kit (JDK) directory website and not the Java Runtime Ambiance (JRE) directory website next off to it.

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

The finalized route with folder locale of Java preserved in Environnent variables.
How to Run a Java Program from the Command Prompt 66

Last however not least, click OK to commit to the recovery.

Oftentimes Rendered apprehensions Misgivings

How can I heal “You don’t have consent to save in this locale” while keeping Java records?

Some users may earn intake a “You don’t have consent to save in this locale” pitfall while keeping Java records in Dwelling windows. Even though you are the enterprises manager of your PC, this pitfall arrives out of undoubtedly no unit. To heal the grouse, proper-click the Java folder’s Freeholds and navigate to the Counterclaim -> Proceeded -> Pick User or Group.

In the Proceeded Counterclaim arrangements for Java, recovery the Storekeeper from System to every little thing user account you have supplied to log in to the machine. You can recognize the straighten name from Contemplate monikers. Just enter the text, such as Desktop computer, Manager, or Clients, to permit full sanctions to the folder. Click OK and save the equalizes.

How can I heal “Java is not licensed as an internal or outward command” in Dwelling windows?

The most guarded means to heal “Java is not licensed as an internal or outward command” is to add Java’s bin directory website to your computer system’s route, as sheathed looming.

Dwelling windows Command Incite doesn’t confirm the results of Java command. How can I heal it?

If your Dwelling windows Command Incite doesn’t confirm the results of a Java command you’ve keyed in, there are two solutions: run the Command Incite in Manager Placement or position your “Java.exe” record in the folder locale and responsive its Freeholds. Then, navigate to the Compatibility tab wherein you will have to uncheck the Sprinted this prospectus as an enterprises manager selection.

What is the debate in between Java and Javascript?

Wear’t dumbfound Java with Javascript, as they are two dissimilar entities:

  • Java came in yesteryear Javascript. It was started by Sun Microsystems in 1991-1995. Javascript was started afterwards by Netscape, an ratty web browser issuer. Basically, Javascript is a severely lightweight model of Java and still oftentimes supplied in web browsers.
  • Java is a compiled prospectus, whereas Javascript is swiped.
  • Java is a static keyed in prospectus, whereas Javascript is dynamically keyed in.
  • Java puts on classes, and Javascript puts on prototypes.

Related Posts