How To Run A Java Program From Command Line On Windows?

Java is a widely-used programming language for building software. It is a high-level object-oriented language. It was first released by Sun Microsystems company in the year 1995. Being that old, it has evolved over the years. Still, there are a few websites and software that won’t function if you haven’t installed Java on your system.

java

That was a nice intro to this beautiful programming language. But, did you know you could use a command line to run your java program? In this article, we’ll learn how to run a java program from the command line. Well, yes, you can do that. And that’s quite simple and quick to implement.

After all, the command prompt is our buddy that does a lot that we don’t even know yet. To run a java file on Windows, you should have java installed. A JDK (Java Development Kit) file should be present in your system. 

Run A Java Program Using The Command Line?

This is the step-by-step guide to run your program in cmd. Let’s quickly walk through the simple steps to run java code in the command prompt.

  1. Right-click the Windows icon or press Windows + R to open the Run window.
  2. Type cmd in the text field.
  3. Now type the commands as follows and press enter key after each of them:

cd\

cd folder_name

  1. You will see the below directory path on the screen:

c:\folder_name>

  1. Just after the angular bracket, type this command to compile the program and press enter:

javac file_name.java

  1. Again you see command same as step 4. To run a java program from the command line, we use the following command and press enter:

java file_name

It was easy to run the program through the command line. Further in the article, you’ll learn more about java.

Handling Java Errors In The Command Line?

Programming is not that easy. One tiny little mistake and your code will throw an error. You learned the simplest way how to run a java program from the command line. While compiling the code, the command line looks for errors as well. If there is an error present in your code, it will not run the program further till you rectify it. 

command

In the compiling process, the command line will tell you about errors. You might understand some and figure out the exact location in the code where the error occurred. Or you might get confused about what it is saying. 

Errors are not that easy to figure out. However, with experience, you get to know where you did something and how to resolve it. Let’s help you to understand some errors you might get when you run java from the command line while compiling your code.

See also: How To Hire Java Developer For Startup: Top Things To Remember

Error 1

Error: Class names, ‘ExampleClass’, are only accepted if annotation processing is explicitly requested

This error occurs due to the following reasons:

  1. You tried to compile the file without adding the .java extension with the file name.
  2.  You misspelled or wrote an incorrect file extension.

The solution is also very simple. Don’t forget to add a file extension while compiling the java file. And you need not add the file extension when you run java from cmd.

Error 2

Error: Could not find or load main class ExampleApplication.

Caused by: java.lang.ClassNotFoundException: ExampleApplication.Class

This error will occur when you want to run the program. If you try to run the main method using extensions such as .java or .class, you will receive this exception or error. Run it without the extensions, and you will see the output.

Error 3

Error: Command not found

You might receive this for the following possible reasons:

  1. There might be a typo in the command you entered.
  2. Command syntax might be incorrect.
  3. The file path has been altered or inaccurate.

You can solve these by checking the above reasons. Correct the spelling if there is a typo. Make sure or confirm that the syntax you are using is not missing anything. Also, check the file path, if it is incorrect or not. Whenever in doubt, revise the steps from how to run a java program from the command line.

One more and most important solution is to check if the Java is correctly installed on your system or not.

Checking If Java Is Installed On Your System?

If you face too many errors while running a program then you should check if your system has Java installed correctly or not. You have the correct steps with you on how to run a java program from the command line. However, if all the steps are correct, your code is also valid, and still, you are facing issues while running your program, then follow the below steps.

There are two ways to check if Java is installed or not. 

Check Using GUI

  1. In the search bar, type Control Panel. Find and open it.control panel
  2. Now open the Programs section.
  3. Find and open Java from the list of categories.programs and java
  4. A new window will open up; click on the About button in the Java Control Panel.
  5. About Java window will open up with java details about the version and more.

Check Using Command Prompt

  1. Right-click the Windows icon or press Windows + R to open the Run window.
  2. Type cmd in the text field.run
  3. Command Prompt window will open up; type the command as it is: java -version.
  4. You will see all the information about java on your screen if it is installed.

Now you can clearly see the difference that why the command prompt is preferable for how to run a java code. A simple command can save many clicks. And also, you need not remember the whole navigation always and only the command.

FAQs

Difference between javac and java commands?

The command javac compiles the program; that is, it produces bytecode. It takes the file with the .java extension as input. The command java runs the produced bytecode. Here the bytecode is the input to produce the final output you see on the screen.

Can I use VS Code for Java?

Yes, you can use VS Code to write your java programs. As VS Code is free, open-source, fast, and lightweight to use, developers find it suitable for Java. It is an excellent tool for most programming languages.

Where is the installed Java file located in Windows?

The location of the installed Java file is by default C:Program FilesJavajdk1. You will see the file path when you install Java on your computer. You should move it anywhere else as it should be in that file path only. Tweaking anything in there can disturb the program.

Conclusion

Command prompt is relatively easy to use if you know the correct commands. Most developers use the command line not only to run programs but also to achieve various tasks in programming like creating folders, creating files, saving files, installing third-party packages, or deleting files and folders.

In this article, we learned how to run a java program from the command line and handle a few errors if you face any. Java programming language is tough, but practicing it daily and challenging yourself with new programs can make you understand the logic used in the programs.

See Also: 4 Ways To Fix Discord Won’t Open Error In An Instant