简体   繁体   English

在jar的lib文件夹中找不到加载类

[英]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.

相关问题 从命令行调用jar的lib文件夹内的jar的类? - Calling class of a jar inside the lib folder of a jar from command line? MEKA >>错误:找不到或加载主类。\\ lib \\ Jama.jar - MEKA >> Error: Could not find or load main class .\lib\Jama.jar Java mysql错误:找不到或加载主类lib \\ mysql-connector-java-5.1.25-bin.jar - Java mysql Error: Could not find or load main class lib\mysql-connector-java-5.1.25-bin.jar .jar文件中找不到或加载主类 - Can't find or load main class, in a .jar file 我得到的错误检查 Apache Pig 版本“错误:无法找到或加载主类 C:\\java\\lib\\tools.jar” - The error I get Which checking Apache Pig Version "Error: Could not find or load main class C:\java\lib\tools.jar" 斯坦福核心 NLP ner 4.0.0 错误:无法找到或加载主 class stanford-ner.jar;lib.* - Stanford Core NLP ner 4.0.0 Error: Could not find or load main class stanford-ner.jar;lib.* 斯卡拉 JAR 文件; 无法在 JAR 文件中找到或加载主类 - Scala JAR file; can't find or load main class in a JAR file 将新的jar文件添加到lib文件夹时,在工作室中找不到类以前模块lib的异常 - When adding new jar files to lib folder getting class not found exception of previous module lib in studio Eclipse jar 导出和lib文件夹 - Eclipse jar export and lib folder 将bean传递到lib文件夹中的jar中 - Pass the bean to a jar in lib folder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM