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.
  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 .
    • 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. 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.
    • 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
    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
    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
    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
      Advertisement

      Video

      Tips

      Submit a Tip
      All tip submissions are carefully reviewed before being published
      Thanks for submitting a tip for review!
      Advertisement

      About This Article

      Article Summary X

      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,035,984 times.

      Reader Success Stories

      • 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