I package all my dependency jars in the lib folder of my final jar. But when I use command to execute like java -cp my.jar MyMainClass, it said that can not load the class in the lib folder, what’s wrong here ?
Thanks
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If your program is packaged as an executable jar, you should have a separate folder where the other jar dependencies are placed, and then specify that folder as being in the classpath when you run your program:
Also: if you want to use the unorthodox method of packaging your jar dependencies inside the jar file you shuld place those dependencies at the ROOT of your executable jar, not in any folder (lib or otherwise). This way they’ll effectively be at the root of your classpath, so if you run your jar as :
it should work
Note: this answer contains errors, please see the attached comments below. The idea is that there are in fact tools that let you package your jar dependencies inside the jar (as internal jars), but this will no longer work as a regular executable jar that you can run with the default JDK classloader. Instead you need a special classloaded that will take into account the internal jars. The tools which offer this functionality usually automatically add the necessary special classloaded and update the jar manifest to use it. Sorry for the mistake.