批处理文件执行jar错误-找不到主类
我的目的是为不懂 java 的功能分析师团队提供一个执行 java 应用程序/工具的 jar 的批处理文件。当我执行批处理文件时,出现以下错误。 非常感谢任何帮助。
如何调试以查明问题是否出在负责加载 log4j 和配置文件的 main() 中?
Exception in thread "main" java.lang.NoClassDefFoundError: x.y.XYZClass Caused by: java.lang.ClassNotFoundException: x.y.XYZClass at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: x.y.XYZClass
. Program will exit.
这是我到目前为止所做的: 我使用 Maven 打包了一个应用程序,目录结构如下:
bin/run.bat
lib/application.jar and other jars that it depends on
config.properties
log4j.xml
jar 清单文件内容:
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By:
Build-Jdk: 1.6.0_20
Main-Class: x.y.XYZClass
Class-Path: vn.jar pn.jar
run.bat 内容:
java -cp .;../lib/application.jar x.y.XYZClass -Dconfig.properties.name=../config.properties -Dlog4j.configuration=../log4j.xml
我从 bin 文件夹中的命令行执行 run.bat。
XYZClass main() 执行以下操作: 加载 log4j.xml 和 config.properties 并执行其他操作。
My intent is to give a batch file that executes a jar of a java application/tool to functional analysts team who do not know java. When I execute the batch file, I get the below error.
Any help is highly appreciated.
How to debug to find out if the issues is in main() which takes care of loading log4j and config files?
Exception in thread "main" java.lang.NoClassDefFoundError: x.y.XYZClass Caused by: java.lang.ClassNotFoundException: x.y.XYZClass at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: x.y.XYZClass
. Program will exit.
This what I did so far:
I packaged an application using Maven with the following directory structure:
bin/run.bat
lib/application.jar and other jars that it depends on
config.properties
log4j.xml
jar manifest file content:
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By:
Build-Jdk: 1.6.0_20
Main-Class: x.y.XYZClass
Class-Path: vn.jar pn.jar
run.bat content:
java -cp .;../lib/application.jar x.y.XYZClass -Dconfig.properties.name=../config.properties -Dlog4j.configuration=../log4j.xml
I execute run.bat from command line from bin folder.
XYZClass main() does the following:
loads log4j.xml and config.properties and does other stuff.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
验证 XYZClass 是否存在于 application.jar 内的正确目录中。从 bin 目录运行命令:
jar tvf ../lib/application.jar
。输出应类似于:
另外,由于您已经创建了一个可执行 jar(通过在清单中指定 Main-Class),您应该能够使用以下命令运行它:
java -jar ..\lib\application.jar< /代码>
Verify that the XYZClass exists inside application.jar, in the correct directory. From the bin directory, run the command:
jar tvf ../lib/application.jar
.The output should resemble:
Also, since you've created an executable jar (by specifying Main-Class in your manifest), you should be able to run it with:
java -jar ..\lib\application.jar