类路径错误 - 无法找到主类和 log4j
我正在尝试在 shell 脚本中使用此命令运行 jar -
java -Dlog4j.configuration=path/to/log4j.properties -classpath path/to/log4j.jar:path/to/another.jar -cp my/jarfile/to/run/myjar.jar com.xyz.TestSuiteRunner CREATE_4_SL
但是当我运行此命令时,我收到如图所示的错误 -
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger
at com.xyz.TestSuiteRunner.<clinit>(TestSuiteRunner.java:27)
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger
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:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
... 1 more
Could not find the main class: com.xyz.TestSuiteRunner. Program will exit.
在参考其他类似的帖子后,我确实知道我可以使用清单文件创建可运行的 jar 文件但我不想那样做。谁能告诉我我哪里出了问题吗?
I am trying to run a jar using this command in my shell script -
java -Dlog4j.configuration=path/to/log4j.properties -classpath path/to/log4j.jar:path/to/another.jar -cp my/jarfile/to/run/myjar.jar com.xyz.TestSuiteRunner CREATE_4_SL
But when I run this, I get the error as shown -
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger
at com.xyz.TestSuiteRunner.<clinit>(TestSuiteRunner.java:27)
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger
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:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
... 1 more
Could not find the main class: com.xyz.TestSuiteRunner. Program will exit.
After referring to other similar posts, I do know that I can create a runnable jar file using a manifest file but I don't want to do it that way. Can anyone please let me know where am I going wrong over here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你有
-classpath
和-cp
开关;-cp
只是-classpath
的缩写,并且不能有多个。最后一个是“粘住”的,因此第一个-classpath
开关中命名的任何类都不会被发现。你需要将这些论点组合成一条长路径;即,关于无法找到主类的错误有点转移注意力;该类显然已找到,只是无法初始化,因为未找到其依赖项。
You've got both
-classpath
and-cp
switches;-cp
is just an abbreviation for-classpath
, and you can't have more than one of these. The last one is the one that "sticks", so any classes named in the first-classpath
switch won't be found. You need to combine those arguments into one long path; i.e.,The error about not being able to find the main class is a bit of a red herring; the class is clearly found, it just can't be initialized because its dependencies aren't found.