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.

public class HelloWorld {
	public static void main(String[] args) {
		System.out.println("Hello World");
	}
}

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 –

Running javac command in cmd

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.

Running java command to produce output

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

public class Test {
	public static void main(String[] args) {
		System.out.println("Hello World");
	}
}

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
Runing javac Command without using package name

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 here

Running java command inside parent folder

Again, 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

Running java command outside parent folder

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 admin@codekru.com

Liked the article? Share this on

Leave a Comment

Your email address will not be published. Required fields are marked *