How to run a class from Jar which is not the Main-Class in its Manifest file
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Running a class from a JAR file that is not specified as the Main-Class in the manifest file is a common task for developers who need to execute different functionalities packaged within the same JAR. Here, we delve into the technical aspects, explain the necessary command-line syntax, and provide guidance on handling various scenarios.
Understanding JAR and Manifest Files
A JAR (Java ARchive) file is a package file format used to combine multiple Java classes, metadata, and resources into a single file for easier distribution and deployment. The META-INF/MANIFEST.MF file inside the JAR specifies metadata about the archive.
The Main-Class in the manifest file designates the entry point of the application:
When you execute the JAR using the java -jar command, the Java Virtual Machine (JVM) launches the class specified as Main-Class.
Executing a Non-Main-Class from a JAR
To execute a class other than the one marked as Main-Class, use the -cp (or -classpath) option. Here’s the syntax to accomplish this:
Step-by-Step Execution
- Locate the Classpath: Determine the JAR file containing the desired class.
- Identify the Fully-Qualified Class Name: Use the package structure to compose the fully-qualified class name (e.g.,
com.example.YourClass). - Execute with Classpath:
- Use the
-cpoption followed by the path to the JAR. - Specify the class you want to run.
- Example:
Passing Arguments
When you need to pass arguments to the program, they follow the class name:
In your Java class, access these arguments via the args parameter in the main method:
Example Scenario
Suppose you have a JAR file utilities.jar with various utility classes compiled within different packages. If you want to run the FileProcessor class located in the package com.utils.process, the command would be:
Common Errors and Troubleshooting
- ClassNotFoundException: Ensure the correct path to the JAR is specified and that the class name is fully-qualified.
- NoClassDefFoundError: Could occur if the class relies on other JARs not included in the classpath. Add multiple JARs to the classpath by separating them with the OS-specific path separator (
:for Unix/Linux and;for Windows).
Troubleshooting Table
| Error Message | Possible Cause | Solution |
ClassNotFoundException | Incorrectly specified class name | Verify the fully-qualified name. |
NoClassDefFoundError | Missing dependencies | Include all required JARs in the classpath. Example: -cp dep1.jar:dep2.jar:yourfile.jar |
UnsupportedClassVersionError | Incompatible Java versions | Compile with a compatible JDK version for your runtime environment. |
Best Practices
- Organize your package structure to avoid conflicts and enhance clarity.
- Use Maven or Gradle for complex dependency management. These tools provide better control over classpath settings and version compatibility.
- Document your manifest file with comments to guide the user on executing different classes if they lack a straightforward Main entry point.
In conclusion, while the manifest's Main-Class offers a direct entry, the flexibility to invoke any class by manipulating the classpath provides robust control over application execution. By following the approach outlined above, developers can effectively manage and execute any class encapsulated within a JAR file.

