- How to run Java Program in cmd (Command Prompt)
- How to run a Java Program using CMD
- Create a folder
- Create a Simple Class
- Open Command Prompt / CMD
- Run the Java Program in CMD
- Additional Points
- Conclusion
- How to Compile & Run Java Program Using Command Prompt
- Compiling and Running
- Troubleshooting
- Community Q&A
- You Might Also Like
- About This Article
- Reader Success Stories
- Is this article up to date?
- Quizzes
- You Might Also Like
- How to run Java programs using the command prompt ( cmd )
- Coding java in cmd
- Learn Latest Tutorials
- Preparation
- Trending Technologies
- B.Tech / MCA
- Javatpoint Services
- Training For College Campus
How to run Java Program in cmd (Command Prompt)
In this article, we will learn how to run the Java Program in cmd / using command prompt.
We are using Java compiler javac for compiling the Java program and Java interpreter java for running the Java program.
Let’s get started :
How to run a Java Program using CMD
We need to follow below mentioned steps in order to run the Java in CMD :
- Create a folder in your machine.
- Create one Java class with some logic.
- Open the command prompt or cmd in your machine.
- Run the Program in CMD.
Create a folder
let’s create a folder named as Sample in your C drive of your machine.
Create a Simple Class
Let’s save this class with name – Test.java in the Sample folder.
Open Command Prompt / CMD
Open command prompt in your machine. You can also open it by typing cmd in run window.
Run the Java Program in CMD
Below are the required steps in cmd :
Now the current directory is Sample.
C:\Sample> set path=%path%;C:\Program Files\Java\jdk1.8.0_101\bin
Replace the above JDK version with your installed java version.
This will Compile the program in cmd.
This is how to run the program in cmd using interpreter.
It will print the following output :
Output:Sample Class to test
Additional Points
You need to ensure that your Windows is able to find the Java compiler and interpreter or not.
Follow below steps :
- Go to Start -> Computer -> System Properties -> Advanced System Settings -> Environment Variables -> System Variables -> PATH
- Click on the “Edit” button.
- Append the semicolon and then jdk path at the end.
- Click on OK button.
Conclusion
We have learnt how to run java program in cmd. It is a very basic question yet very important. To install java in your machine, you can visit url.
You can check your java version with following commands in cmd :
How to Compile & Run Java Program Using Command Prompt
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 952,416 times.
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.
Compiling and Running
- 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.
- 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.
- 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.
- 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 .
- 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.
- 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.
Troubleshooting
- 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.
Community Q&A
What would I do if I type in the Java filename.java and it is not running? The code written in Notepad is correct.
Thanks! We’re glad this was helpful.
Thank you for your feedback.
As a small thank you, we’d like to offer you a $30 gift card (valid at GoNift.com). Use it to try out great new products and services nationwide without paying full price—wine, food delivery, clothing and more. Enjoy! Claim Your Gift If wikiHow has helped you, please consider a small contribution to support us in helping more readers like you. We’re committed to providing the world with free how-to resources, and even $1 helps us in our mission. Support wikiHowOnce you are in the correct directory, you can compile the program by typing «javac filename.java» into the command line and pressing enter.
Thanks! We’re glad this was helpful.
Thank you for your feedback.
As a small thank you, we’d like to offer you a $30 gift card (valid at GoNift.com). Use it to try out great new products and services nationwide without paying full price—wine, food delivery, clothing and more. Enjoy! Claim Your Gift If wikiHow has helped you, please consider a small contribution to support us in helping more readers like you. We’re committed to providing the world with free how-to resources, and even $1 helps us in our mission. Support wikiHowThanks! We’re glad this was helpful.
Thank you for your feedback.
As a small thank you, we’d like to offer you a $30 gift card (valid at GoNift.com). Use it to try out great new products and services nationwide without paying full price—wine, food delivery, clothing and more. Enjoy! Claim Your Gift If wikiHow has helped you, please consider a small contribution to support us in helping more readers like you. We’re committed to providing the world with free how-to resources, and even $1 helps us in our mission. Support wikiHowAgain, 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.
You Might Also Like
How to Set JAVA_HOME for JDK & JRE: A Step-by-Step Guide
Use Easy Windows CMD Commands to Check Your Java Version
How to Do Division in Java (Integer and Floating Point)
How to Compile and Run Java Programs Using Notepad++
Simple Steps to Type a Bunny with Your Keyboard
About This Article
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 952,416 times.
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.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
Is this article up to date?
«There is not anything specifically good in any wikiHow Article. ‘Cause all wikiHow articles are awesome. We all love this site. Excellent Work.» . » more
«I’m working on a Boy Scout Programming merit badge and I needed help with the Java part. I couldn’t compile it.» . » more
Quizzes
You Might Also Like
How to Set JAVA_HOME for JDK & JRE: A Step-by-Step Guide
Use Easy Windows CMD Commands to Check Your Java Version
How to Do Division in Java (Integer and Floating Point)
How to Compile and Run Java Programs Using Notepad++
How to run Java programs using the command prompt ( cmd )
In this, we will discuss, how to run Java programs using the command prompt ( cmd ) while writing the code in text editors like notepad or sublime and about the errors that we encounter while running it, and how to resolve those.
1. First, let’s run a simple hello world program using a command prompt or cmd
You can use any text editor to save the below program, eg. notepad, sublime, etc.
Now, save this file under the same name as that of class, HelloWorld.java and then run the javac HelloWorld.java command from your command prompt or cmd where you saved the file, this command will create a .class file of your java program in the same folder itself.
I have placed my file in the demo folder as shown below –As you can see, before running the javac command, there was only one file, whereas after running the command, a .class file is also generated.
Then we will just run the java Helloword command from our cmd, and it will print Hello World to the output screen.2. Now we will look at various issues that we can get while running programs using command prompt (cmd)
In the above program, we can see that both the filename(HelloWorld.java) and classname(HelloWorld) are the same, but there are cases when both are different. So, let’s change the classname in the Helloword.java file
Now, let’s run the same command( javac Helloworld.java ) and see what happens. We have got the below error, stating that the Test class should be kept in a Test.java file. So, we have to keep both classname and filename the same.
HelloWorld.java:1: error: class Test is public, should be declared in a file named Test.java public class Test < ^ 1 error
What if our program is in some package? what will happen if we run the same commands? Let’s take a sneak peek into this
In our above programs, our program was not in any package, rather our file was in a default package. Let’s look at the below code where we added a package line
package demo; public class HelloWorld < public static void main(String[] args) < System.out.println("Hello World"); >>
Now, again run the same commands,
- javac Helloworld.java
- java Helloworld
Now after running the same commands, we got the error saying, Error: Could not find or load main class HelloWorld .
So, What went wrong here? When we compile a file within some package, then it will be compiled as packageName.className, which means we have to run
java packageName.className hereAgain, we are getting the same error because now we are using demo.HelloWorld. So, we have to move into our parent folder and then run the commands again on cmd as shown below
Hurrrrayyy, it works now. Let us know if you faced any other problems while running java programs in the command prompt or cmd.
Hope you liked this article. If you have any doubts or concerns, please feel free to write us in comments or mail us at [email protected]
Coding java in cmd
Learn Latest Tutorials
Preparation
Trending Technologies
B.Tech / MCA
Javatpoint Services
JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.
- Website Designing
- Website Development
- Java Development
- PHP Development
- WordPress
- Graphic Designing
- Logo
- Digital Marketing
- On Page and Off Page SEO
- PPC
- Content Development
- Corporate Training
- Classroom and Online Training
- Data Entry
Training For College Campus
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 weekLike/Subscribe us for latest updates or newsletter