在jar的lib文件夹中找不到加载类
我将所有依赖项 jar 打包到最终 jar 的 lib 文件夹中。但是当我使用 java -cp my.jar MyMainClass 这样的命令执行时,它说无法加载 lib 文件夹中的类,这里出了什么问题?
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的程序打包为可执行 jar,则应该有一个单独的文件夹,其中放置其他 jar 依赖项,然后在运行程序时将该文件夹指定为类路径中:
另外:如果您想使用非正统方法将 jar 依赖项打包到 jar 文件中时,您应该将这些依赖项放在可执行 jar 的根目录中,而不是任何文件夹(lib 或其他文件夹)中。这样它们将有效地位于类路径的根目录中,因此如果您将 jar 运行为:
它应该可以工作
注意:此答案包含错误,请参阅下面附加的评论。这个想法是,实际上有一些工具可以让您将 jar 依赖项打包到 jar 中(作为内部 jar),但这将不再像可以使用默认 JDK 类加载器运行的常规可执行 jar 一样工作。相反,您需要一个特殊的类加载来考虑内部罐子。提供此功能的工具通常会自动添加必要的特殊类加载并更新 jar 清单以使用它。很抱歉犯了这个错误。
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.
您还可以使用 zip 提取存档并查看 my.jar 以查看其中的内容以及您的 MyMain.class 是否在那里。
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.