While many programming environments will allow you to compile and run a program within the environment, you can also compile and run using Command Prompt. Both Windows and Mac have their own versions of Command Prompt, although it is technically called Terminal on the Mac OS. The process is essentially identical for Windows and Mac.

Method 1
Method 1 of 2:

Compiling and Running

  1. 1
    Save the program. After using a text editor, such as NotePad, to create your Java program, save the program with a .java extension. Of course, the file name can be anything. For the purposes of this tutorial, "filename" will be used as a placeholder for your file name.
    • To ensure that your file is saved as a .java file, be sure to write .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 of how to write a program in Java, check out our tutorial on how to program in Java. For the purposes of learning how to compile and run a program, though, you can use any sort of Java program.
  2. 2
    Open Command Prompt/Terminal. Accessing the command line differs slightly for Mac and Windows.
    • Windows: Press Home, then type cmd. Press Enter to open the Command Prompt.
    • Mac: In Finder, press the Go tab, select Applications, select Utilities, then select Terminal.
    Advertisement
  3. 3
    Check that Java is installed. 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. 4
    Navigate to the correct folder. Use the command cd followed by the directory name to change your working directory.
    • For example, if you were operating in C:\Users\Bob\Project and wanted to get to C:\Users\Bob\Project\TitanProject , enter in cd TitanProject and press Enter.
    • You can see a list of what is in your current directory by typing dir and pressing Enter.
  5. 5
    Compile the program. Once you are in the correct directory, you can compile the program by typing javac filename.java into the command line and pressing enter.
    • If you have any errors in your program, or if there is difficultly compiling, the command prompt will notify you.
    • Check out our article about how to fix compiler errors in Java for more help.
  6. 6
    Run the program. Enter in java filename and press Enter. Of course, replace "filename" with whatever the name of your file actually is.
    • After pressing Enter, your program should run. If you receive an error, or if your program fails to run in any way, refer to the troubleshooting method.
  7. Advertisement
Method 2
Method 2 of 2:

Troubleshooting

  1. 1
    Set your path. If you are running a simple program with all of 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 off the version of Java displayed on the first line, type in set path=%path%;C:\Program Files\Java\jdk1.5.0_09\bin to the command prompt and press Enter. Substitute jdk1.5.0_09 with whatever version of Java you have installed.
      • Be sure you are entering in this command while in the directory containing your Java project.
    • Mac: Type /usr/libexec/java_home -v 1.7 into the terminal and press Enter to ensure Java is installed on your system. Then, type echo export "JAVA_HOME=\$(/usr/libexec/java_home)" >> ~/.bash_profile into the terminal and press Enter. Restart the terminal afterwards.

Community Q&A

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

About This Article

Yaffet Meshesha
Co-authored by:
Computer Specialist
This article was co-authored by Yaffet Meshesha. 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 943,597 times.
How helpful is this?
Co-authors: 25
Updated: January 29, 2023
Views: 943,597
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?
Advertisement