类路径错误 - 无法找到主类和 log4j

发布于 2025-01-07 17:13:39 字数 1056 浏览 1 评论 0原文

我正在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

筱武穆 2025-01-14 17:13:39

你有 -classpath-cp 开关; -cp 只是 -classpath 的缩写,并且不能有多个。最后一个是“粘住”的,因此第一个 -classpath 开关中命名的任何类都不会被发现。你需要将这些论点组合成一条长路径;即,

-classpath path/to/log4j.jar:path/to/another.jar:my/jarfile/to/run/myjar.jar

关于无法找到主类的错误有点转移注意力;该类显然已找到,只是无法初始化,因为未找到其依赖项。

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.,

-classpath path/to/log4j.jar:path/to/another.jar:my/jarfile/to/run/myjar.jar

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文