Source: javaindos.

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

Run Command Prompt

Use with AIC:\> cd \mywork

This makes C:\mywork the current directory.

Use with AIC:\mywork> dir

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

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

This tells the system where to find JDK programs.

Use with AIC:\mywork> javac filenamehere.java

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

Use with AIC:\mywork> dir

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

Use with AIC:\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
275

Source: javaindos.

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

Run Command Prompt

Use with AIC:\> cd \mywork

This makes C:\mywork the current directory.

Use with AIC:\mywork> dir

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

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

This tells the system where to find JDK programs.

Use with AIC:\mywork> javac filenamehere.java

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

Use with AIC:\mywork> dir

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

Use with AIC:\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

    Use with AITheJavaFile.java
    
  2. Compile the Java File to a *.class file

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

    Use with AIjava 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

    Use with AIjava -jar TheJavaFile.jar
    
🌐
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 - 1. Type ″cmd″ into Windows search. 2. Right-click Command prompt. 3. Click Run as administrator. 4. Type ″cd \path\to\java\program″. 5. Press Enter. 6. Type ″javac -filename.java″. 7. Press Enter. 8. Type ″java filename.java″. 9.
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
compilation - How to run Java program in command prompt - Stack Overflow
I created a Java project to call a Web service. It has one Main java file and another class file. I have used some jar files for HTTP client. In Eclipse it runs fine. I need to run the Java program... More on stackoverflow.com
🌐 stackoverflow.com
How to run java program on Command Prompt/Terminal with Intellij (WINDOWS)?
Why do you need to run the program through the command prompt/Terminal if you are already using an IDE??? More on reddit.com
🌐 r/javahelp
3
1
June 6, 2020
how do i run java from the command line?
type - "java HelloWorld", to execute your file. More on reddit.com
🌐 r/learnjava
10
5
October 1, 2018
🌐
W3Schools
w3schools.com › java › java_getstarted.asp
Java Getting Started
Open Command Prompt (cmd.exe), navigate to the directory where you saved your file, and type "javac Main.java": ... This will compile your code. If there are no errors in the code, the command prompt will take you to the next line.
🌐
Medium
medium.com › @AlexanderObregon › executing-java-code-from-the-command-line-d6a3c09bb565
Executing Java Code from the Command Line | Medium
March 9, 2025 - It’s important to omit the .class extension in the command. The java command looks for a class file with the same name as the one specified and attempts to run its main method. If the class contains a valid main method, the program will execute, and you will see the output in the terminal:
🌐
CodingNomads
codingnomads.com › how-to-run-java-from-command-line
Executing Java applications from the CLI
Please follow the steps below to run your first Java app from the CLI. First, open your terminal and ensure you're in the right working directory.
🌐
Vbspu
vbspu.ac.in › e-content › How-to-Compile-and-Run-your-First-Java-Program.pdf pdf
How to Compile and Run your First Java Program
is FirstJavaProgram, that’s why our file name should be FirstJavaProgram.java. Step 3: In this step, we will compile the program. For this, open command prompt · (cmd) on Windows, if you are Mac OS then open Terminal.
🌐
javaspring
javaspring.net › blog › how-to-run-java-program-in-cmd
How to Run a Java Program in the Command Prompt — javaspring.net
The java command is used to run the compiled Java bytecode. It loads the bytecode into the JVM and executes the main method of the specified class. To compile a Java program in the CMD, follow these steps:
Find elsewhere
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-run-java-program
How to Run Java Program? - GeeksforGeeks
July 23, 2025 - Step 5: Compile your Java code by typing "javac [filename].java" in the command prompt/terminal. This will create a .class file in the same directory. Step 6: Run your Java program by typing "java [filename]" in the command prompt/terminal.
🌐
Richard Johnsonbaugh
condor.depaul.edu › ~alarrain › csc211 › dos.html
Running a Java Program from the Command Prompt in Windows
You must follow that standard. Since the name of the class begins with uppercase your file name must begin with uppercase. The filename name must be identical (in case) to the class name. ... Enter cmd or command (depending on your OS) in the combo box labeled open.
🌐
Codecademy
codecademy.com › article › java-for-programmers-java-and-the-command-line
Java and the Command Line | Codecademy
There are several types, each with a specific purpose and you can explore these use cases on Oracle’s website, the vast majority of Java development though, occurs in the SE or Standard Edition of the JDK. You can find the most recent version of the JDK here, and older versions of Java, such as Java 9 like we are using here. Before you can run the program from the command line, you must compile it. Open your terminal or command prompt (depending on OS), and navigate to the directory where the file you want to run is located.
🌐
Coderanch
coderanch.com › t › 562022 › java › Running-Java-Programs-command-prompt
Running Java Programs from the command prompt (Beginning Java forum at Coderanch)
December 17, 2011 - ... You can't run the java source code (.java). You have to compile the source to get a class file (.class). You do this with the javac command. I assume you have the Java SDK, if not, you're going to need it. On the command prompt, type in javac and press Enter.
🌐
KaaShiv InfoTech
kaashivinfotech.com › home › how to run a java program in cmd: step-by-step guide for beginners
How to Run a Java Program in CMD – Easy Step-by-Step
August 9, 2025 - So go ahead and open your CMD or Terminal, and get to coding! ... A: Go to the src folder and enter javac -d . package/FileName.java then java package.FileName. ... A: No, you need the JDK to compile and run Java programs.
🌐
Scaler
scaler.com › home › topics › how to run java program?
How to Run Java Program? - Scaler Topics
March 22, 2024 - Step 7: Now again, open the cmd and type the command java Scaler as you can see in the below image. Step 8: At last, you will see that the content of the file has been displayed. ... 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.
🌐
The Londoner
dev.thelondoner.com › home › how to run a java program in cmd?
Carry Out how to run a java program in cmd? With Care | The Londoner
Integrated development environments like IntelliJ IDEA or Eclipse handle compilation and execution automatically, but using CMD gives you full control and mirrors how Java runs on servers. Install JDK. Add JDK bin to PATH. Verify with java -version. Write a .java file. Compile with javac. Run with java. You now have a repeatable process for running any Java program from the command line.
🌐
Skylit
skylit.com › javamethods › faqs › javaindos.html
Running a Java program from Command Prompt
Save your file as HelloWorld.java in C:\mywork. To make sure your file name is HeloWorld.java, (not HelloWorld.java.txt), first choose "Save as file type:" All files, then type in the file name HelloWorld.java · If the system cannot find javac, check the set path command.
🌐
JanBask Training
janbasktraining.com › home › what is the process to run a java program in cmd?
What Is The Process To Run A Java Program In CMD?
July 5, 2019 - For the reasons for figuring out how to assemble and run a program, however, you can utilize any kind of Java program. Open Command Prompt/Terminal. Getting to the order line varies somewhat for Mac and Windows. Windows:Press ⇱ Home, at that point, type cmd.
🌐
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 - (use the JDK folder for the version installed on your system). This tells the system where to find JDK programs. C:\SoftwareTestingMaterial> javac HelloTesters.java · This runs javac.exe, the compiler.