PDF download Download Article
An easy-to-follow guide to compiling and running your Java code with CMD
PDF download Download Article

While many programming environments will allow you to compile and run a program within that environment, you can also compile and run in Command Prompt on Windows or the Terminal on Mac. The process is nearly the same. This wikiHow article teaches you how to compile and run a Java program using the Command Prompt or Terminal.

Things You Should Know

  • At the command prompt, type "cd" followed by the path your Java program is saved to, then press "Enter."
  • Type "javac [filename] and press "Enter" to compile the program.
  • Type "java [filename]" and press "Enter" to run the Java program after it is compiled.
Method 1
Method 1 of 2:

Compiling and Running

PDF download Download Article
  1. You can create a Java program using a simple text editing program like Notepad or an independent development environment (IDE) such as Eclipse. It's recommended you save the filename as the name of the Java class. Be sure to add the ".java" file extension to the file.
    • To ensure that your file is saved as a .java file, be sure to type .java after the file name and select All Files from the dropdown extension menu.
    • Make note of where you saved the file on your system.
    • If you are unsure how to write a program in Java, check out our tutorial on how to program in Java. You can use any Java program to learn how to compile and run a program.
  2. Advertisement
  3. Type java -version into your command line. If Java is installed, you will see a message stating what version of Java is currently installed.[2]
  4. Use the command cd followed by the directory name to change folders in the Command Prompt.
    • For example, if your program is saved to "C:\Users\Bob\Project\TitanProject," type cd C:\Users\Bob\Project\TitanProject and press Enter to navigate to that folder.
    • You can see a list of what is in your current directory by typing dir and pressing Enter.
  5. Once you are in the correct directory, you can compile the program. To do so, type javac [filename].java into the command line and press Enter.[3]
    • If you have any errors in your program or difficulty compiling, the command prompt will notify you. You'll need to fix the errors before the program can be compiled.
  6. Type java [filename] and press Enter. Of course, replace "filename" with whatever the name of your file is.
    • After pressing Enter, your program should run. If you receive an error or your program fails to run, you must do some troubleshooting.
  7. Advertisement
Method 2
Method 2 of 2:

Troubleshooting

PDF download Download Article
  1. Set your path. If you are running a simple program with all the files in the same directory, you will probably not need to do this. However, if you are running a more complex program with files in multiple directories, you will need to tell the computer where to look for these files.[4]
    • Windows: Type java -version into the command prompt and press Enter. Based on the version of Java displayed on the first line, type in {{{1}}} to the command prompt and press Enter. Substitute "jdkX.X.X_XX" with whatever version of Java you have installed.
      • Be sure you are entering this command while in the directory containing your Java project.
    • Mac: Type /usr/libexec/java_home -v X.X into the terminal and press Enter to ensure Java is installed on your system. Then, type {{{1}}} into the terminal and press Enter. Restart the terminal afterward.

Community Q&A

Search
Add New Question
  • Question
    What would I do if I type in the Java filename.java and it is not running? The code written in Notepad is correct.
    Community Answer
    Community Answer
    It is okay. You have to write for compiling : javac filename.java and for running: just filename.
  • Question
    How can I compile using CMD?
    Community Answer
    Community Answer
    Once you are in the correct directory, you can compile the program by typing "javac filename.java" into the command line and pressing enter.
  • Question
    How can I hack a computer using the command prompt?
    Community Answer
    Community Answer
    You can't without installing other things. CMD is NOT for hacking and should never be used for it.
See more answers
Ask a Question
200 characters left
Include your email address to get a message when this question is answered.
Submit
Advertisement

Video

Tips

Submit a Tip
All tip submissions are carefully reviewed before being published
Name
Please provide your name and last initial
Thanks for submitting a tip for review!
Advertisement

You Might Also Like

Compile a Java Program Compile a Java Program Using an SDK or Online Compiler
Check Your Java Version in the Windows Command LineUse Easy Windows CMD Commands to Check Your Java Version
Set Java Home Set JAVA_HOME for JDK & JRE: A Step-by-Step Guide
Install the Java Software Development KitInstall the Java Software Development Kit
Create Your First Java Program on Ubuntu LinuxCreate Your First Java Program on Ubuntu Linux
Run C Program in Command Prompt Run a C Program Using Developer Command Prompt
Program in JavaProgram in Java
Run a .Jar Java FileRun a .Jar Java File
Compile and Run Java Program by Notepad Compile and Run Java Programs Using Notepad++
Install Java in Ubuntu Using TerminalInstall Java in Ubuntu Using Terminal
Run Ruby CodeRun Ruby Code
Run a Batch File from the Command Line on Windows Run a Windows Batch File from the Command Line (CMD)
Compile a C ProgramCompile a C Program
Install Oracle Java JRE on Ubuntu LinuxInstall Oracle Java JRE on Ubuntu Linux
Advertisement

About This Article

Yaffet Meshesha
Co-authored by:
Computer Specialist
This article was co-authored by Yaffet Meshesha and by wikiHow staff writer, Travis Boylls. Yaffet Meshesha is a Computer Specialist and the Founder of Techy, a full-service computer pickup, repair, and delivery service. With over eight years of experience, Yaffet specializes in computer repairs and technical support. Techy has been featured on TechCrunch and Time. This article has been viewed 1,152,690 times.
How helpful is this?
Co-authors: 29
Updated: September 28, 2025
Views: 1,152,690
Categories: Java
Article SummaryX

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. Press Enter.

Did this summary help you?

Thanks to all authors for creating a page that has been read 1,152,690 times.

Reader Success Stories

  • Vaibhav Agrawal

    Vaibhav Agrawal

    May 16, 2017

    "There is not anything specifically good in any wikiHow Article. 'Cause all wikiHow articles are awesome. We..." more
Share your story

Is this article up to date?

Advertisement