Run jar file in command prompt
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Running a JAR file via the command prompt in Windows or a terminal in Unix-based systems (like Linux or macOS) is a common task for Java developers and system administrators. A JAR (Java ARchive) file encapsulates many files into one, allowing for easy distribution and deployment of Java applications or libraries.
What is a JAR File?
A JAR file is built on the ZIP file format and typically contains Java class files and associated metadata and resources (text, images, etc.) that constitute a program. It allows Java Runtime Environment (JRE) to efficiently deploy Java applications. It can be executable or non-executable. An executable JAR file contains a specific manifest file that specifies the main class to run.
Prerequisites for Running a JAR File
To run a JAR file, you'll need:
- The Java Runtime Environment (JRE) or Java Development Kit (JDK) installed on your machine. JDK includes JRE, so if you have JDK, that’s adequate.
- The JAR file you intend to run.
Checking Java Installation
Before attempting to run a JAR file, you should first check if Java is installed. Open your command prompt (Windows) or terminal (macOS or Linux) and type:
This command will show the version of Java installed if it is present; if not, it will prompt an error, and you'll need to install Java.
Running a JAR File
Assuming Java is installed, you can run a JAR file by using the java command followed by the -jar switch and the path to the JAR file. Syntax:
Example
If you have a JAR file named example.jar in the folder C:\Programs, you would navigate to the folder (using cd command) and run:
This command will execute the JAR file using the main class defined in its manifest.
Common Issues and Troubleshooting
- JAR Cannot be Opened: If you receive an error saying the JAR cannot be opened, ensure that you have the correct permissions to access the file and that the JAR is not corrupted.
- Main Class not Found: If the error states that the main class could not be found, it's possible that the JAR’s manifest does not specify a main class, or the specified main class does not exist in the JAR. This is specific to executable JAR files.
- Java Version Mismatch: This occurs when the JAR was compiled using a higher version of Java than what is installed on your system. Update your Java installation or recompile the JAR with a compatible Java version.
Additional Information
- Viewing Content: You can view the contents of a JAR file without running it by using the
jartool included in JDK. - Creating Executable JAR: To create an executable JAR, ensure your manifest file includes the main class. This is typically done through your IDE or build tool (e.g., Maven, Gradle).
Summary Table
| Command | Description |
java -version | Check the installed Java version. |
java -jar path_to_jar_file.jar | Execute a JAR file. |
cd path_to_directory | Change the directory in command prompt or terminal. |
jar tf jar-file | View the contents of a JAR file. |
Conclusion
Running a JAR file from the command prompt or terminal is a straightforward process once Java is installed and properly configured on your system. This ability is crucial for deploying and testing Java applications, especially in production environments or on servers where graphical user interfaces are absent. Moreover, understanding the basics of JAR operations and Java command-line tools enhances your effectiveness in managing Java applications.
Related reading
- run main class of Maven project
- Run mvn spring-bootrun from parent module?
- Run Spring-boot's main using IDE
- run spring batch job from the controller
- Runnable with a parameter?
- Running a MVC app using Spring Boot Hibernate MySql
- Running chron job on a single instance of spring service deployed on aws
- Running code after Spring Boot starts

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.