Source: javaindos.

Let's say your file is in C:\mywork\

Run Command Prompt

CopyC:\> cd \mywork

This makes C:\mywork the current directory.

CopyC:\mywork> dir

This displays the directory contents. You should see filenamehere.java among the files.

CopyC:\mywork> set path=%path%;C:\Program Files\Java\jdk1.5.0_09\bin

This tells the system where to find JDK programs.

CopyC:\mywork> javac filenamehere.java

This runs javac.exe, the compiler. You should see nothing but the next system prompt...

CopyC:\mywork> dir

javac has created the filenamehere.class file. You should see filenamehere.java and filenamehere.class among the files.

CopyC:\mywork> java filenamehere

This runs the Java interpreter. You should then see your program output.

If the system cannot find javac, check the set path command. If javac runs but you get errors, check your Java text. If the program compiles but you get an exception, check the spelling and capitalization in the file name and the class name and the java HelloWorld command. Java is case-sensitive!

Answer from Nicholas Kadaeux on Stack Overflow
Top answer
1 of 13
274

Source: javaindos.

Let's say your file is in C:\mywork\

Run Command Prompt

CopyC:\> cd \mywork

This makes C:\mywork the current directory.

CopyC:\mywork> dir

This displays the directory contents. You should see filenamehere.java among the files.

CopyC:\mywork> set path=%path%;C:\Program Files\Java\jdk1.5.0_09\bin

This tells the system where to find JDK programs.

CopyC:\mywork> javac filenamehere.java

This runs javac.exe, the compiler. You should see nothing but the next system prompt...

CopyC:\mywork> dir

javac has created the filenamehere.class file. You should see filenamehere.java and filenamehere.class among the files.

CopyC:\mywork> java filenamehere

This runs the Java interpreter. You should then see your program output.

If the system cannot find javac, check the set path command. If javac runs but you get errors, check your Java text. If the program compiles but you get an exception, check the spelling and capitalization in the file name and the class name and the java HelloWorld command. Java is case-sensitive!

2 of 13
121

To complete the answer :

  1. The Java File

    CopyTheJavaFile.java
    
  2. Compile the Java File to a *.class file

    Copyjavac TheJavaFile.java
    
    • This will create a TheJavaFile.class file
  3. Execution of the Java File

    Copyjava TheJavaFile
    
  4. Creation of an executable *.jar file

    • You've got two options here -

      1. With an external manifest file :

        • Create the manifest file say - MANIFEST.mf

        • The MANIFEST file is nothing but an explicit entry of the Main Class

        • jar -cvfm TheJavaFile.jar MANIFEST.mf TheJavaFile.class

      2. Executable by Entry Point:

        • jar -cvfe TheJavaFile.jar <MainClass> TheJavaFile.class
  5. To run the Jar File

    Copyjava -jar TheJavaFile.jar
    
🌐
freeCodeCamp
freecodecamp.org › news › how-to-execute-and-run-java-code
How to Execute and Run Java Code from the Terminal
March 10, 2022 - Inside the Source folder, it creates our .class file. We need this file to run the Java program. Now if we want to run the .class file, then we need to change the command a little, as we need to provide the directory of the .class file in the terminal window.
Discussions

Running Java in Command Prompt?
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/javahelp
11
1
February 2, 2021
How to run java program on Command Prompt/Terminal with Intellij (WINDOWS)?
For my java courses all of our programs had to be run from command prompt so I understand your need. A lot of prof's want you to be comfortable with the command line. First, you have to navigate to the folder where the java file is located. You can do this using the "cd" command to move around to where you keep your files. Then you compile the program from the java file with the main method. Make sure any class files you need are in same folder. compile: javac MainMethodFile.java If no exceptions, then you launch: java MainMethodFile Make sure to just use the file name not the whole file extension when you launch. Don't use the .java or .class extension. More on reddit.com
🌐 r/javahelp
3
1
June 6, 2020
How can I get JAVA to run in VS code??
Did you follow the instructions step by step ?you need to download and install the Java SE Development Kit (JDK), configure your environment for Java development. and install J ava Extension Pack,you could refer to Java with VS Code More on reddit.com
🌐 r/vscode
6
0
July 18, 2019
How to "Clear screen" in Java?
Your console is closely tied to your operating system, so there isn't a platform-agnostic way to clear the screen that is always guaranteed to work every time. You can try using escape codes (which might not always work), like this: public static void clearScreen() { System.out.print("\033[H\033[2J"); System.out.flush(); } Or you can figure out what your platform is and execute the appropriate command, like this: public final static void clearConsole() { try { final String os = System.getProperty("os.name"); if (os.contains("Windows")) { Runtime.getRuntime().exec("cls"); } else { Runtime.getRuntime().exec("clear"); } } catch (final Exception e) { // Handle any exceptions. } } Full disclosure: All of this is from a StackOverflow post . More on reddit.com
🌐 r/learnprogramming
1
1
February 2, 2018
🌐
Medium
medium.com › @burakkocakeu › how-to-run-your-java-program-on-terminal-b4956e7102a8
How to Run your Java Program on Terminal | by Burak KOCAK | Medium
January 29, 2023 - The general syntax is java [class_name] The program will now execute and display any output in the terminal. Note that you should use the fully qualified name of the class containing the main method.
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-run-java-program
How to Run Java Program? - GeeksforGeeks
July 23, 2025 - To run a Java program, you need to follow these steps: Step 1: Install Java Development Kit (JDK) on your computer. You can download the JDK from the Oracle website or any other reliable source. Step 2: Write your Java code in a text editor such as Notepad or any other code editor. Save the file with a .java extension and file type as 'All files' in the desired location. Step 3: Open the command prompt (Windows) or terminal (Mac or Linux).
🌐
Princeton CS
introcs.cs.princeton.edu › java › 15inout › windows-cmd.html
Java and the Windows Command Prompt
Prepend C:\Program Files\Java\jdk1.6.0_27\bin; to the beginning of the PATH variable. Click OK three times. You will type commands in an application called the Command Prompt. Launch the command prompt via All Programs -> Accessories -> Command Prompt. (If you already had a command prompt window ...
🌐
Wikitechy
wikitechy.com › technology › how-to-run-the-java-program-in-command-prompt
How to Execute and Run Java Code from the Terminal
August 8, 2024 - In this article, we will show you how to run Java programs in Command Prompt, a command-line interface in Windows.
Find elsewhere
🌐
Make Tech Easier
maketecheasier.com › home › computing › windows › how to run a java program from the command prompt
How to Run a Java Program from the Command Prompt - Make Tech Easier
July 16, 2025 - Compile the Java program with the javac command as shown below. ... Be warned that you won’t see anything happen. However, if you use the dir command, you’ll notice a new file in your directory ending in the “.class” extension, indicating the program has been compiled. ... You’ll see the program run within the Command Prompt window.
🌐
Codemia
codemia.io › home › knowledge hub › how do i run a java program from the command line on windows?
How do I run a Java program from the command line on Windows? | Codemia
January 27, 2025 - This article will take you through detailed steps from writing a simple Java program, compiling it, and finally running it on a Windows command prompt. Additionally, we’ll also look into managing Java versions and setting environmental variables. Before running a Java program, ensure the Java Development Kit (JDK) is installed on your machine. To check if it's already installed, open the command prompt (type cmd in the Windows search bar) and type:
🌐
Wikihow
wikihow.com › computers and electronics › software › programming › java › how to compile and run a java program using command prompt
How to Compile and Run a Java Program Using Command Prompt
September 28, 2025 - You can use any Java program to learn how to compile and run a program. ... Windows: Click the Windows Start menu and type CMD. Click the Command Prompt icon. Mac: In Finder, press the Go tab, select Applications, select Utilities, then select ...
🌐
YouTube
youtube.com › watch
Run Java in Command Prompt / Terminal | Compile and Run Java using Commands - YouTube
In this video I have shown how you run your java program using command prompt or terminal in windows 11. I have create file using notepad and compiled and ru...
Published   March 12, 2024
🌐
Code With Arjun
codewitharjun.com › home › blogs › run java using command prompt / terminal
Run Java Using Command Prompt / Terminal - Code With Arjun
March 26, 2025 - Compile and Run Java Program using Command Prompt or Terminal, Notepad using Java commands: javac and java.
🌐
Quora
quora.com › How-do-I-run-a-Java-program-in-terminal-Windows
How to run a Java program in terminal Windows - Quora
Answer (1 of 3): Install Java. * Visit the oracle website and then click on download. * Click on Accept License Agreement and download the .exe file of JDK for windows. * Download the file and start the installation process by clicking on the file. * Keep clicking on next and don't alter any...
🌐
Scaler
scaler.com › home › topics › how to run java program?
How to Run Java Program? - Scaler Topics
March 22, 2024 - 1: Java programs are compiled using the javac command. It takes .java files as input and generates bytecode. 2: The java command executes Java bytecodes. It processes bytecode as input, executes it, and produces the output. This guide highlights how to run Java program, starting from creating the Java file in Notepad to compiling and executing it.
🌐
LabEx
labex.io › tutorials › java-how-to-compile-and-run-a-java-program-in-the-terminal-413960
How to compile and run a Java program in the terminal | LabEx
Additionally, the javac command supports various options and flags that can be used to customize the compilation process, such as setting the source and target Java versions, enabling or disabling certain compiler warnings, and more. You can explore these options by running javac -help in the terminal. Now that you've learned how to compile Java source code, let's move on to the next step: running Java programs in the terminal.
🌐
Software Testing Material
softwaretestingmaterial.com › home › selenium › how to run a java program using command prompt
How To Run A Java Program Using Command Prompt
June 11, 2025 - Run a Java Program using Command Prompt - We use Java compiler javac to compile and the Java interpreter java to run the Java program
🌐
YouTube
youtube.com › watch
How to Run Java Programs With Command Prompt (cmd) and Notepad in Windows - YouTube
How do you compile and execute your Java programs from the command prompt (cmd) in Windows? Also, how you can you write your programs just using Notepad?We'l...
Published   November 15, 2021
🌐
Florida State University
cs.fsu.edu › ~myers › cop3252 › howto › cmdline.html
Compiling on the Command line with Java SDK
javac // the compiler java // loader/interpreter Windows versions of these filenames would be: javac.exe java.exe These commands (discussed below) will work in any of the following command-line prompts, as long as the SDK is installed: ... To run a java program, use the java command.
🌐
Princeton CS
introcs.cs.princeton.edu › java › windows › manual.php
Hello World in Java on Windows (manual instructions)
This document instructs you on how to manually setup a Java programming environment for your Windows computer and provides a step-by-step guide for creating, compiling, and executing a Java program using either DrJava or the Command Prompt.
🌐
Reddit
reddit.com › r/javahelp › running java in command prompt?
r/javahelp on Reddit: Running Java in Command Prompt?
February 2, 2021 -

Second Edit: May have fixed the Environment Variables to work with JAVA_HOME. Minor change to what was printed but was able to run the file like so:

C:\Users\Test>cd Documents\MyFirstProgramFolder

C:\Users\Test\Documents\MyFirstProgramFolder>java MyFirstProgram
We did it! Again! Number 3!

Again, making sure I was in the right directory was the first issue, but now I may have the Environment Variables fixed. I hope at least. Thank you again everyone.

Edit: Have my issue mostly solved. I am able to compile and run the code. cd-ing to the correct directory seemed to be the issue in the command prompt and that is all I really need for now. Thank you to everyone who took the time try to help me!

Hello! I am new to learning Java but have been stuck on a single issue for the past day and a half. I have recently started reading Head First Java to try and teach myself Java. I took a single programming class that was taught in Java 3-4 years ago. All of the programming experience I have has always been in some sort of IDE. In Head First Java they want you to work through the command line after writing code. I have been entirely unsuccessful at setting up Java to work through the command prompt. I have now tried to download 2-3 different JDK's, followed 2-3 tutorials on how to just get "Hello World" to print, and followed a guide on how to set up the Java Environment. None have been successful and I am frequently met with the below errors/issues when I try to parse together my understanding trying to fix the issue. I would really like to learn how to actually do things without the need of an IDE. Any help at all would be greatly appreciated! So the question summed up, is more or less where am I going wrong for setting up the Java environment, or JVM?

In the Command Prompt:

C:\Users\Test>"C:\Program Files\Java\jdk1.8.0_161\bin"\Javac MyFirstProgram.java

javac: file not found: MyFirstProgram.java

Usage: javac <options> <source files>

use -help for a list of possible options

In Windows Powershell:

PS C:\Users\Test\Documents\MyFirstProgramFolder> "C:\Program Files\Java\jdk1.8.0_161\bin"\javac MyFirstProgram.java

At line:1 char:41

+ "C:\Program Files\Java\jdk1.8.0_161\bin"\javac MyFirstProgram.java

+ ~~~~~~

Unexpected token '\javac' in expression or statement.

+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException

+ FullyQualifiedErrorId : UnexpectedToken

Below are some of the websites I have tried:

https://www.instructables.com/How-to-Make-Your-First-Computer-Program/ - This instructables link is what I just used to receive the above responses.

https://www.tutorialspoint.com/java/java_environment_setup.htm

https://www.geeksforgeeks.org/setting-environment-java/

The Code:

class MyFirstProgram {

    public static void main(String[] args) {
            System.out.println("Hello, World");
    }
}
Top answer
1 of 6
2
move the quote to the end of the args[0] "C:\Program Files\Java\jdk1.8.0_161\bin\javac.exe" MyFirstProgram.java Java is a compiled language and not interpreted, so there is no actual IDE - but maybe a Java-pro knows one. There are some websites, where you can play around with classes...
2 of 6
2
First, I would add C:\Program Files\Java\jdk1.8.0_161\bin to your system PATH environment variable - this will allow you to use the Java commands without including the entire path: Right click Computer Click the properties On the left pane select Advanced System Settings Select Environment Variables Under the System Variables, Select PATH and click edit,and then click new and add path as C:\ProgramFiles\Java\jdk1.8.0_161\bin (depending on your installation path)and finally click ok (credit to https://stackoverflow.com/questions/32241179/setting-up-enviromental-variables-in-windows-10-to-use-java-and-javac for putting in the steps so I didn't have to) Once done, you should be able to use javac MyFirstProgram.java instead of putting the entire path in for javac and mucking about with quotes :) Note: you will need to restart the command prompt for the path update to take effect :) Second, in your examples above your error is file not found for the command prompt, and your folder is different than the one showing for powershell ( C:\Users\Test for command prompt and C:\Users\Test\Documents\MyFirstProgramFolder for powershell) so I'm guessing the error was due to MyFirstProgram.java not being in the folder the command prompt was in. So if you are in C:\Users\Test\Documents\MyFirstProgramFolder (assuming this is the location of your java file) and did the Path fix above, you can just use javac MyFirstProgram.java or "C:\Program Files\Java\jdk1.8.0_161\bin\javac.exe" MyFirstProgram.java if you didn't modify the path. Edit to add: both of those commands should work in powershell as well depending on whether you set the PATH variable or not assuming you are in the folder containing MyFirstProgram.java.