简体   繁体   English

Java从命令行运行jar:找不到或加载主类com.test.Main时出错

[英]Java run jar from command line: Error could not find or load main class com.test.Main

Windows console command: Windows控制台命令:

c:\..path..>java -jar TestApp.jar

results in: Error could not find or load main class com.test.Main 结果: Error could not find or load main class com.test.Main

I'm using IntelliJ 14 and my artifact layout looks like this: 我正在使用IntelliJ 14,并且我的工件布局如下所示:

TestApp.jar/
--client.jar <----- com.test.Main is located here

--jogl/
----gluegen.jar
----jogl-all.jar

--META-INF/
----manifest.mf

Manifest file info: 清单文件信息:

Main-Class: com.test.Main
Class-Path: client.jar;jogl/gluegen-rt.jar;jogl/jogl-all.jar;

but when I run code in IntelliJ, it is succesfull. 但是当我在IntelliJ中运行代码时,它就成功了。

I am afraid you can't, because default Classloader cannot load from a jar-within-a-jar. 恐怕您不能这样做,因为默认的Classloader无法从jar-in-a-jar加载。 I suppose IntelliJ will expand those jars and thus working. 我想IntelliJ会扩展这些jar并使其工作。

See here : Reference jars inside a jar 看到这里: 罐子里的参考罐子

But there is a solution. 但是有一个解决方案。 I use to create autorun-jars with dependencies using maven-assembly-plugin . 我使用maven-assembly-plugin创建具有依赖项的autorun-jars。 If I expand this jar I found that all jar dependencies are expanded inside the jar. 如果我扩展此jar,我发现所有jar依赖项都在jar内扩展。 This way, Classloader can load all classes. 这样,Classloader可以加载所有类。

If you don't use maven, IntelliJ must have an option to export project as autorun-jar or something similar. 如果您不使用maven,则IntelliJ必须具有将项目导出为autorun-jar或类似名称的选项。

Edit 编辑

If maven or exporting your project as autorun-jar is not ok for you, maybe One-JAR would serve your porpouse, but I have no experience with this project. 如果行不通或将您的项目导出为autorun-jar不合适,也许One-JAR会为您服务,但是我没有这个项目的经验。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 命令行Java错误:找不到或加载主类 - Command line java error: Could not find or load main class Java命令行错误(无法找到或加载主类) - Java command line error (could not find or load main class) 无法运行jar文件,出现“错误:找不到或加载主类com.trident.SchedulerMain” - Unable to run jar file getting “Error: Could not find or load main class com.trident.SchedulerMain” 从 IntelliJ 创建可运行的 Jar,尝试在 cmd 中运行会给出“错误:无法找到或加载主类 com.....” - Creating runable Jar from IntelliJ, trying to run in cmd gives 'Error: Could not find or load main class com.....' 在Windows上尝试使用classpath(用于其他lib)从命令行运行jar文件会引发“找不到或加载主类” - Trying to run jar file from command line using classpath(for other libs) raises “Could not find or load main class”, on Windows Java JAR:无法找到或加载主 class - Java JAR: Could not find or load main class 运行带有“错误:无法找到或加载主类”的 jar - run jar with `Error: Could not find or load main class` 错误:从Linux命令行运行时找不到或加载主类Main- - Error: Could not find or load main class Main - when running from Linux command line Java -jar错误:找不到或加载主类 - Java -jar ERROR: could not find or load main class 创建 JAR 文件后,命令行返回“错误:无法找到或加载主 class 示例程序”? - After creating JAR file, command line returns 'Error: Could not find or load main class ExampleProgram`?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM