Calling "java -jar MyFile.jar" with Extended Classpath Options
When attempting to run a compiled JAR file incorporating external libraries and facing a "java.lang.ClassNotFoundException", it's crucial to understand the fundamentals behind the Java execution process.
Java provides two options for specifying classpaths:
Incorrect Usage
Attempting to combine both -jar and -cp concurrently is incorrect and will result in the aforementioned error.
Solution
There are two valid approaches to resolve this issue:
1. Manifest-Defined Classpath:
2. Explicit Classpath:
For Example:
java -cp 'MyProgram.jar:libs/*' main.Main
Note: The dir/* syntax adds all JAR files from the specified directory to the classpath.
Additional Tips:
The above is the detailed content of How to Run a JAR File with External Libraries and Avoid \'java.lang.ClassNotFoundException\'?. For more information, please follow other related articles on the PHP Chinese website!