Home >Java >javaTutorial >How Can I Execute a JAR File with External Dependencies?

How Can I Execute a JAR File with External Dependencies?

Linda Hamilton
Linda HamiltonOriginal
2024-12-13 14:29:13552browse

How Can I Execute a JAR File with External Dependencies?

Executing JAR Files with Custom Classpath

Due to limitations with the "-jar" option, specifying "-cp" will have no effect. As per the Java documentation, using "-jar" makes the JAR file the sole source of user classes, overriding other classpath settings.

Additionally, JAR files cannot encompass other JAR files. To include necessary dependencies, you can either:

  • Include the JAR files from the "lib" directory in the manifest using relative paths.
  • Specify all JAR files on the command line using "-cp":
java -cp MyJar.jar:lib/* com.somepackage.subpackage.Main

This command will execute the main class from the specified JAR while loading dependencies from the "lib" directory.

The above is the detailed content of How Can I Execute a JAR File with External Dependencies?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn