Explain Codes LogoExplain Codes Logo

Run a JAR file from the command line and specify classpath

java
classpath-management
manifest-file
java-execution
Anton ShumikhinbyAnton Shumikhin·Mar 5, 2025
TLDR

To execute a JAR file and specify an external classpath, use the following:

java -cp "libs/*;MyApp.jar" com.myapp.Main
  • Substitute libs/* with the location of your dependencies, replace : with ; on Windows.
  • Change MyApp.jar and com.myapp.Main to your JAR file and the entrance class respectively.

Understanding the interplay between -jar and -cp

When you use -jar flag, it fires up the JAR file's Main-Class as declared in the manifest, while disregarding any classpath established with -cp. Hence if you are looking to define a bespoke classpath, you should either:

  • Include the classpath in the manifest
  • Use -cp without -jar and provide the fully qualified name of the main class.

Interacting with classpath and manifest

Manifest classpath declaration

If you package your app with all dependencies, it's more efficient to "bundle" all required JAR files within your JAR's manifest. Just remember:

  • Include the Class-Path attribute in your JAR's META-INF/MANIFEST.MF
  • Use relative paths when referring to needed JAR files.
  • Keep lines within 72 characters to avoid the wrath of the manifest's line length Overlord.

Including classpath dynamically

Alternatively, you can specify the classpath dynamically on the command line:

java -cp "MyJar.jar:lib/*" com.somepackage.subpackage.Main

Never forget that Windows prefers semicolons (;), while Unix/Linux take pleasure in colons (:). With a wildcard (*), all JAR files in a directory can be included with ease.

Escaping the labyrinth of spaces and special characters

When handling paths that showcase spaces or special characters, be sure to escape, especially on Windows PowerShell:

java -cp 'C:\Program Files\Java\libs\*;MyApp.jar' com.myapp.Main // "Because spaces in paths are fun, right? 😯"

Leveraging scripts and build tools

Make use of scripts or build tools like ANT, Maven, or Gradle to manage your classpath entries and manifest files like a pro. It boosts maintainability and prevents manual errors. Also, automation: it's the future! 💾 => 🚀

Testing across environments

Ensure to test your JAR files and classpath configuration across different environments. It can smoke out any discrepancies between Unix/Linux and Windows cameos due to path and script differences. Test, test then test again! 🧪

NoClassDefFoundError

This isn't a secret message from Hogwarts, it's a sign that there's a classpath problem. Check that:

  • The classpath includes all necessary JARs.
  • The paths are forcibly accurate and relative to your command run location.
  • Your JAR files have necessary read-run permissions, or else they will just sit there, like lazy teenagers. 🛋️

Could not find or load main class

This means your Main-Class could be playing hide-and-seek with the JVM. Check that:

  • Your fully qualified class name is correct and in its Sunday best. 👔
  • If using -jar, your manifest file's Main-Class attribute declares the class with main method.

Going beyond exec

As tempting as it can be, try to avoid Runtime.getRuntime().exec(command) when running JAR commands or dealing with complex classpaths. It's a common cause of comedy in coding, however, results are less fun in production.