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
    
🌐
Medium
medium.com › @AlexanderObregon › executing-java-code-from-the-command-line-d6a3c09bb565
Executing Java Code from the Command Line | Medium
March 9, 2025 - Learn how to compile and run Java programs from the command line using javac and java, manage classpaths, and understand how the JVM processes bytecode.
Discussions

How to run a Java program via CMD on Windows? - Ask a Question - TestMu AI (formerly LambdaTest) Community
How can I run a Java program from the command line on Windows? I’m trying to execute the following Java program from the command line in Windows: import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import ... More on community.testmuai.com
🌐 community.testmuai.com
0
March 12, 2025
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
Running A Java File Using Command Prompt

Double check the exact name and extension of your file on your desktop. Double check these steps:

  1. cd to desktop.

  2. touch helloWorld.java

  3. nano helloWorld.java

  4. enter code, control X to exit, Y to save. Enter.

  5. javac helloWorld.java

  6. java MyHelloWorldDemo (or whatever your class is named)

More on reddit.com
🌐 r/learnjava
8
8
September 13, 2019
Compiling a java file using command prompt
i guess i'm asking how to make my command prompt focus on desktop. Use the "cd" (change directory) command. E.g. cd C:\Users\SBS\Desktop More on reddit.com
🌐 r/learnprogramming
3
2
December 15, 2018
🌐
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.
🌐
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 - 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. The above command doesn’t set your Java compiler PATH permanently. It sets the environment variable for that session, but that change will be removed. Setting your Java compiler PATH permanently is handy if you want your compiled Java programs to run ...
🌐
Princeton CS
introcs.cs.princeton.edu › java › 15inout › windows-cmd.html
Java and the Windows Command Prompt
Now try to execute with "java -cp . HelloWorld". If this works, you need to edit your classpath. (iTunes has a proclivity for changing the classpath, so if you recently upgraded iTunes, this is likely the source of the problem.) Where can I learn more about the Windows command line? Here is a short tutorial on the Windows command prompt. Microsoft maintains a complete command line reference. How do I change my directory to the H: drive from the Windows Command Prompt?
🌐
freeCodeCamp
freecodecamp.org › news › how-to-execute-and-run-java-code
How to Execute and Run Java Code from the Terminal
March 10, 2022 - It will run the Java code like below. If you're wondering why we are changing the command now, it's because earlier, we did not declare any packages. So the Java compiler created the .class file within the directory where our source code was. So, we could get the .class file directly from there and execute the class file as well.
🌐
Princeton CS
introcs.cs.princeton.edu › java › windows › manual.php
Hello World in Java on Windows (manual instructions)
In order to run Java from the Command Prompt, you must add Java to your PATH environment variable:
Find elsewhere
🌐
Richard Johnsonbaugh
condor.depaul.edu › ~alarrain › csc211 › dos.html
Running a Java Program from the Command Prompt in Windows
bad command or javac is not recognized You will have to set your PATH variable. You cannot continue until your path variable is set properly. See Java Setup at the resource page. If you're program compiled now you have to run it.
🌐
W3Schools
w3schools.com › java › java_getstarted.asp
Java Getting Started
For now, focus on how to run the code above. Save the code in Notepad as "Main.java". Open Command Prompt (cmd.exe), navigate to the directory where you saved your file, and type "javac Main.java":
🌐
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.
🌐
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 - Step iii: Open command prompt · Open Command Prompt (Open Run (Windows+R) and type cmd) Step iv: Run the created Java program using command prompt · Follow the below steps: C:\Users\Admin> cd\ C:\> cd SoftwareTestingMaterial ·
🌐
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.
🌐
Javatpoint
javatpoint.com › how-to-run-java-program-in-cmd-using-notepad
How to Run Java Program in CMD Using Notepad - javatpoint
How to Run Java Program in CMD Using Notepad with java tutorial, features, history, variables, object, class, programs, operators, for-loop, oops concept, array, string, map, math, methods, examples etc.
🌐
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 - Learn how to run a Java program in CMD with simple steps. Compile, execute, and troubleshoot Java code directly from the command line.
🌐
Codecademy
codecademy.com › article › java-for-programmers-java-and-the-command-line
Java and the Command Line | Codecademy
While there are many IDEs with built-in execution capabilities (Eclipse and IntelliJ for example), Java can also be run directly from the command line.
🌐
Florida State University
cs.fsu.edu › ~myers › cop3252 › howto › cmdline.html
Compiling on the Command line with Java SDK
The primary Java SDK tools, which ... in any of the following command-line prompts, as long as the SDK is installed: ... To run a java program, use the java command....
🌐
iO Flood
ioflood.com › blog › java-run-programs-command-line
How-To Run Java Programs From Command Line
February 29, 2024 - We’ll cover everything from compiling and executing Java programs in the command line, handling Java programs with arguments, to dealing with packages and using alternative approaches like Integrated Development Environments (IDEs) or build tools like Maven or Gradle. Let’s lace up and start running with Java!
🌐
Scaler
scaler.com › home › topics › how to run java program?
How to Run Java Program? - Scaler Topics
March 22, 2024 - Step 2: You can see that inside the Art folder, there is a Java file named test.java. Step 3: Now press Alt+D, and you will enter the above search bar. Step 4: Type CMD and press enter key.
🌐
TestMu AI
community.testmuai.com › ask a question
How to run a Java program via CMD on Windows? - Ask a Question - TestMu AI (formerly LambdaTest) Community
March 12, 2025 - How can I run a Java program from the command line on Windows? I’m trying to execute the following Java program from the command line in Windows: import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class CopyFile { public static void main(String[] args) { InputStream inStream = null; OutputStream outStream = null; try { File afi...