Download Article
An easy-to-follow guide to compiling and running your Java code with CMD
Download Article
- Compiling and Running |
- Troubleshooting |
- Video |
- Q&A |
- Tips
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.
Steps
-
Save the program. 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.
-
Open Command Prompt or Terminal . Accessing the command line differs slightly for Mac and Windows.
- Windows: Click the Windows Start menu and type CMD . Click the Command Prompt icon.
- Mac: In Finder, press the Go tab, select Applications, select Utilities, then select Terminal.
Advertisement -
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.
- If not, you may need to install the Java Development Kit from their website. It is free and can be found at this link: http://www.oracle.com/technetwork/java/javase/downloads/index.html .
- If you are not running the latest version of Java, you can update Java in the Java control panel.
-
Navigate to the folder your program is saved to. 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 .
-
Compile the program. 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.
-
Run the program. 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 .
Advertisement
Community Q&A
Search
-
QuestionWhat would I do if I type in the Java filename.java and it is not running? The code written in Notepad is correct.Community AnswerIt is okay. You have to write for compiling : javac filename.java and for running: just filename.
-
QuestionHow can I compile using CMD?Community AnswerOnce you are in the correct directory, you can compile the program by typing "javac filename.java" into the command line and pressing enter.
-
QuestionHow can I hack a computer using the command prompt?Community AnswerYou can't without installing other things. CMD is NOT for hacking and should never be used for it.
Ask a Question
200 characters left
Include your email address to get a message when this question is answered.
Submit
Advertisement
Video
Tips
- Again, it is important that you have the Java JDK downloaded and installed on your computer. The link is here: http://www.oracle.com/technetwork/java/javase/downloads/index.html .Thanks
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
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,109,973 times.
Reader Success Stories
- "There is not anything specifically good in any wikiHow Article. 'Cause all wikiHow articles are awesome. We all love this site. Excellent Work." ..." more
Advertisement