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
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
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:
java -classpath c:\path\to\dir_with_jars -jar myExecutableJar.jar p1.MainClass
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 :
java -cp:. -jar myJar.jar
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.
You can also use zip to extract the archive and take a look into the my.jar to see what's in there and if your MyMain.class is there.