[英]Can not find load class in the lib folder of jar
I package all my dependency jars in the lib folder of my final jar. 我将所有依赖项jar打包在最终jar的lib文件夹中。 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 ?
但是,当我使用命令像java -cp my.jar MyMainClass这样执行时,它说无法在lib文件夹中加载该类,这是怎么回事?
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: 如果您的程序打包为可执行jar,则应该有一个放置其他jar依赖项的单独文件夹,然后在运行程序时将该文件夹指定为类路径:
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). 另外:如果要使用非常规方法将jar依赖项打包在jar文件中,则应将这些依赖项放在可执行jar的ROOT中,而不要放在任何文件夹(lib或其他文件夹)中。 This way they'll effectively be at the root of your classpath, so if you run your jar as :
这样,它们将有效地位于类路径的根目录,因此,如果您将jar运行为:
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.
这个想法是,实际上有一些工具可以让您将jar依赖关系打包到jar中(作为内部jar),但是它将不再作为可以使用默认JDK类加载器运行的常规可执行jar起作用。 Instead you need a special classloaded that will take into account the internal jars.
相反,您需要加载一个特殊的类,该类将考虑内部jar。 The tools which offer this functionality usually automatically add the necessary special classloaded and update the jar manifest to use it.
提供此功能的工具通常会自动添加必要的特殊类,并更新jar清单以使用它。 Sorry for the mistake.
对不起,这个错误。
您还可以使用zip解压缩存档,并查看my.jar以查看其中的内容以及MyMain.class是否存在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.