{"id":10088,"date":"2025-08-09T12:28:17","date_gmt":"2025-08-09T12:28:17","guid":{"rendered":"https:\/\/www.kaashivinfotech.com\/blog\/?p=10088"},"modified":"2025-08-09T12:28:17","modified_gmt":"2025-08-09T12:28:17","slug":"how-to-run-a-java-program-in-cmd","status":"publish","type":"post","link":"https:\/\/www.kaashivinfotech.com\/blog\/how-to-run-a-java-program-in-cmd\/","title":{"rendered":"How to Run a Java Program in CMD: Step-by-Step Guide for Beginners"},"content":{"rendered":"<h2><strong>Introduction<\/strong><\/h2>\n<p>If you&#8217;re brand new to learning Java, and you&#8217;re possibly thinking about how to run a java program in CMD, you&#8217;ve come to the right resource. Running Java from the command line isn&#8217;t just for hardcore developers; it&#8217;s a neat, and simple, but powerful skill that helps you better understand how Java runs behind the scenes.<\/p>\n<p>This guide will show you how to run a java program on CMD, how to compile Java code and more importantly, how to run a Java project in CMD when you have multiple files. I&#8217;ll be covering everything you need to know step-by-step, and I will include some tips and tricks, some common errors and their solutions, and more.<\/p>\n<figure id=\"attachment_10099\" aria-describedby=\"caption-attachment-10099\" style=\"width: 663px\" class=\"wp-caption aligncenter\"><img fetchpriority=\"high\" decoding=\"async\" class=\"wp-image-10099\" src=\"https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/How-to-run-java-project-in-cmd.webp\" alt=\"How to run java project in cmd\" width=\"663\" height=\"442\" srcset=\"https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/How-to-run-java-project-in-cmd.webp 1536w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/How-to-run-java-project-in-cmd-300x200.webp 300w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/How-to-run-java-project-in-cmd-1024x683.webp 1024w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/How-to-run-java-project-in-cmd-768x512.webp 768w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/How-to-run-java-project-in-cmd-380x253.webp 380w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/How-to-run-java-project-in-cmd-800x533.webp 800w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/How-to-run-java-project-in-cmd-1160x773.webp 1160w\" sizes=\"(max-width: 663px) 100vw, 663px\" \/><figcaption id=\"caption-attachment-10099\" class=\"wp-caption-text\">How to run java project in CMD<\/figcaption><\/figure>\n<h2>Why Learn How to Run Java Program in CMD?<\/h2>\n<p>With many beginners beginning in an IDE like Eclipse or IntelliJ, they almost always skip the command line!<\/p>\n<p>That said:<\/p>\n<ul>\n<li>Using the CMD (or Terminal) will give you greater control over your Java programs.<\/li>\n<li>CMD is quicker when compiling small test files and to run them!<\/li>\n<li>CMD teaches you how Java performs its compilation process without the IDE magic.<\/li>\n<\/ul>\n<p>There are also other reasons for using CMD, like if you have a job interview it would be more impressive to your prospective employers if you can run java code in CMD.<\/p>\n<h2><strong>Prerequisites Before Running Java in CMD<\/strong><\/h2>\n<figure id=\"attachment_10101\" aria-describedby=\"caption-attachment-10101\" style=\"width: 587px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" class=\" wp-image-10101\" src=\"https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/Prerequisites-Before-Running-Java-in-CMD.webp\" alt=\"how to run a java program in cmd\" width=\"587\" height=\"391\" srcset=\"https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/Prerequisites-Before-Running-Java-in-CMD.webp 1536w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/Prerequisites-Before-Running-Java-in-CMD-300x200.webp 300w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/Prerequisites-Before-Running-Java-in-CMD-1024x683.webp 1024w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/Prerequisites-Before-Running-Java-in-CMD-768x512.webp 768w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/Prerequisites-Before-Running-Java-in-CMD-380x253.webp 380w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/Prerequisites-Before-Running-Java-in-CMD-800x533.webp 800w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/Prerequisites-Before-Running-Java-in-CMD-1160x773.webp 1160w\" sizes=\"(max-width: 587px) 100vw, 587px\" \/><figcaption id=\"caption-attachment-10101\" class=\"wp-caption-text\">Prerequisites Before Running Java in CMD<\/figcaption><\/figure>\n<p>Before you start typing commands, make sure you have:<\/p>\n<ul>\n<li><strong>Java Development Kit (JDK)<\/strong> installed<\/li>\n<li><strong>Environment Variables<\/strong> set (for Windows)<\/li>\n<li>A <strong>text editor<\/strong> (Notepad, VS Code, Sublime Text, etc.)<\/li>\n<li>Basic knowledge of Java syntax<\/li>\n<\/ul>\n<h3><strong> Step 1 \u2013 Install Java (JDK)<\/strong><\/h3>\n<p><strong>Windows:<\/strong><\/p>\n<ol>\n<li>Download JDK from <a href=\"https:\/\/www.oracle.com\/java\/technologies\/javase-downloads.html\" target=\"_blank\" rel=\"noopener\">Oracle<\/a>.<\/li>\n<li>Install it and note the installation path (e.g., C:\\Program Files\\Java\\jdk-21).<\/li>\n<\/ol>\n<p><strong>macOS:<\/strong><\/p>\n<p>brew install openjdk<\/p>\n<p><strong>Linux (Ubuntu\/Debian):<\/strong><\/p>\n<p>sudo apt install openjdk-21-jdk<\/p>\n<h3><strong> Step 2 \u2013 Set Java Environment Variables<\/strong><\/h3>\n<p>If you don\u2019t do this, you might get the error:<\/p>\n<p>&#8216;javac&#8217; is not recognized as an internal or external command<\/p>\n<p><strong>Windows:<\/strong><\/p>\n<ol>\n<li>Search for &#8220;Environment Variables&#8221; in the Start Menu.<\/li>\n<li>Add JAVA_HOME with your JDK path.<\/li>\n<li>Add %JAVA_HOME%\\bin to PATH.<\/li>\n<\/ol>\n<h3><strong> Step 3 \u2013 Write Your Java Program<\/strong><\/h3>\n<p>Let\u2019s create a simple HelloWorld.java file.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">public class HelloWorld {\r\n\r\n\u00a0\u00a0\u00a0 public static void main(String[] args) {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 System.out.println(\"Hello, CMD!\");\r\n\r\n\u00a0\u00a0\u00a0 }\r\n\r\n}<\/pre>\n<p>Save it in a folder (e.g., C:\\JavaProjects).<\/p>\n<h3><strong> Step 4 \u2013 Compile the Java Program in CMD<\/strong><\/h3>\n<p>Open <strong>Command Prompt<\/strong>, navigate to the folder:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\">cd C:\\JavaProjects\r\n\r\nCompile the file:\r\n\r\njavac HelloWorld.java<\/pre>\n<p>If successful, this creates a HelloWorld.class file.<\/p>\n<h3><strong> Step 5 \u2013 Run the Java Program in CMD<\/strong><\/h3>\n<p>Now run the program:<\/p>\n<p>java HelloWorld<\/p>\n<p>Output:<\/p>\n<p>Hello, CMD!<\/p>\n<h2><strong> How to Run Java Project in CMD (Multiple Files)<\/strong><\/h2>\n<p>If you have a Java project with multiple .java files:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\">javac *.java\r\n\r\njava MainClass<\/pre>\n<p>Replace MainClass with the file containing public static void main.<\/p>\n<h2><strong> Running Java from CMD on macOS\/Linux<\/strong><\/h2>\n<p>On macOS\/Linux, the steps are the same, but you open <strong>Terminal<\/strong> instead of CMD:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\">cd ~\/JavaProjects\r\n\r\njavac HelloWorld.java\r\n\r\njava HelloWorld<\/pre>\n<h2><strong>Common Errors and Fixes<\/strong><\/h2>\n<ul>\n<li>&#8216;javac&#8217; is not recognized \u2192 Set your environment variables.<\/li>\n<li>Could not find or load main class \u2192 Check if file\/class names match.<\/li>\n<li>Compilation failed \u2192 Fix syntax errors before running.<\/li>\n<\/ul>\n<h2><strong>Tips for Running Java in CMD<\/strong><\/h2>\n<ul>\n<li>Remember to always match the file name with the class name.<\/li>\n<li>Use the UP arrow key in CMD to quickly go back to a command.<\/li>\n<li>Organize your Java files into folders.<\/li>\n<\/ul>\n<h2>Final Thoughts \u2013 How to Run a Java Program in CMD<\/h2>\n<p>Knowing how to run a java program in CMD is not just for exam purposes, it&#8217;s one of the most important things for any Java developer to understand. Once you know how to run a java program in cmd and run a java project in cmd without using an IDE, you have real power over your code.<\/p>\n<p>It doesn&#8217;t matter if you are a beginner or a professional, using java terminal commands will speed up debugging, improve understanding of Java, and enable you to work quickly on any OS. So go ahead and open your CMD or Terminal, and get to coding!<\/p>\n<h2>FAQ \u2013 How to Run Java Program in CMD<\/h2>\n<p>Q1: How to compile and run Java file in cmd?<\/p>\n<p>A: Compile: javac FileName.java Run: java FileName<\/p>\n<p>Q2: How to run Java project in cmd with packages?<\/p>\n<p>A: Go to the src folder and enter javac -d . package\/FileName.java then java package.FileName.<\/p>\n<p>Q3: Can I run a Java without JDK?<\/p>\n<p>A: No, you need the JDK to compile and run Java programs.<\/p>\n<h2>Related Reads<\/h2>\n<ul>\n<li><a href=\"https:\/\/www.kaashivinfotech.com\/blog\/basic-programs-on-java-beginners\/\">Basic Programs on Java: Your First Step to Java Mastery \ud83d\udcbb<\/a><\/li>\n<li><a href=\"https:\/\/www.kaashivinfotech.com\/blog\/backend-java-developer-skills-2025-top-7\/\">Backend Java Developer Skills: 7 Must-Haves in 2025 \ud83d\ude80<\/a><\/li>\n<li><a href=\"https:\/\/www.wikitechy.com\/object-oriented-programming-oop-in-java\/\" target=\"_blank\" rel=\"noopener\">Object-Oriented Programming (OOP) In Java &#8211; Explained<\/a><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction If you&#8217;re brand new to learning Java, and you&#8217;re possibly thinking about how to run a java program in CMD, you&#8217;ve come to the right resource. Running Java from the command line isn&#8217;t just for hardcore developers; it&#8217;s a neat, and simple, but powerful skill that helps you better understand how Java runs behind [&hellip;]<\/p>\n","protected":false},"author":9,"featured_media":10097,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2499],"tags":[8243,8245,8240,8242,8244,8241],"class_list":["post-10088","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-how-to","tag-compile-java-in-cmd","tag-execute-java-program","tag-how-to-run-a-java-program-in-cmd","tag-java-project-in-cmd","tag-java-terminal-commands","tag-run-java-program-on-cmd"],"_links":{"self":[{"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/posts\/10088","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/comments?post=10088"}],"version-history":[{"count":0,"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/posts\/10088\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/media\/10097"}],"wp:attachment":[{"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/media?parent=10088"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/categories?post=10088"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/tags?post=10088"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}