类路径问题:.jar 文件可以从清单中的类路径访问文件,但不能从 -cp 命令行参数访问文件

发布于 2024-09-09 00:16:38 字数 1422 浏览 1 评论 0原文

我在这个目录结构中有文件 foo.jar:

+--+- /foo_lib
|  |
|  +- Ice.jar ...
|
+-- /swt-linux-gtk -- swt.jar
|
+-- foo.jar

其中清单部分表示:

Class-Path: . foo_lib/Ice.jar swt-linux-gtk/swt.jar foo_lib/o
 rg.eclipse.core.commands_3.5.0.I20090525-2000.jar...

它可以通过命令毫无问题地启动

java -jar foo.jar

到目前为止,一切都很好。问题是我想从命令行设置 swt.jar(但不是其他)的路径(因为不同的平台需要不同版本的 swt.jar)。因此,我从清单中删除了 swt-linux-gtk/swt.jar 并运行此命令:

java -cp swt-linux-gtk/swt.jar -jar foo.jar

不幸的是,这不起作用,我收到此异常:

Exception in thread "main" java.lang.NoClassDefFoundError: org/eclipse/swt/widgets/Display
    at scsadmin.BootStrapper$.main(BootStrapper.scala:90)
    at scsadmin.BootStrapper.main(BootStrapper.scala)
    at scsadmin.JavaMain.main(JavaMain.java:9)
Caused by: java.lang.ClassNotFoundException: org.eclipse.swt.widgets.Display
    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)
    ... 3 more

Is -cp当清单中有 Class-Path 部分时被忽略?或者我还缺少其他东西吗?

I have the file foo.jar in this directory structure:

+--+- /foo_lib
|  |
|  +- Ice.jar ...
|
+-- /swt-linux-gtk -- swt.jar
|
+-- foo.jar

where manifest says, in part:

Class-Path: . foo_lib/Ice.jar swt-linux-gtk/swt.jar foo_lib/o
 rg.eclipse.core.commands_3.5.0.I20090525-2000.jar...

It can be lauched without problem by the command

java -jar foo.jar

So far, so good. The problem is that I want to set the path to swt.jar (but not the others) from the command line (since different platforms need different versions of swt.jar). So I removed swt-linux-gtk/swt.jar from the manifest and run this command:

java -cp swt-linux-gtk/swt.jar -jar foo.jar

Unfortunately, this doesn't work and I get this exception:

Exception in thread "main" java.lang.NoClassDefFoundError: org/eclipse/swt/widgets/Display
    at scsadmin.BootStrapper$.main(BootStrapper.scala:90)
    at scsadmin.BootStrapper.main(BootStrapper.scala)
    at scsadmin.JavaMain.main(JavaMain.java:9)
Caused by: java.lang.ClassNotFoundException: org.eclipse.swt.widgets.Display
    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)
    ... 3 more

Is -cp ignored when there is a Class-Path section in the manifest? Or am I missing something else?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

巡山小妖精 2024-09-16 00:16:38

来自 java -jar 的联机帮助页:

当您使用此选项时,JAR 文件是所有用户的源
类和其他用户类路径设置将被忽略。

jar 似乎还使用自定义类加载器来加载捆绑的 jar。

From the manpage for java -jar:

When you use this option, the JAR file is the source of all user
classes, and other user class path settings are ignored.

It also seems that jar is using a custom classloader to be able to load bundled jars.

醉生梦死 2024-09-16 00:16:38

当您使用“java -jar”时,-classpath 选项将被忽略。

请参阅 http://download .oracle.com/docs/cd/E17476_01/javase/1.4.2/docs/tooldocs/linux/java.html 了解更多详细信息。

“当您使用此选项时,JAR 文件是所有用户类的源,其他用户类路径设置将被忽略。”

干杯,
唐纳德.

When you use "java -jar", the -classpath option is ignored.

See http://download.oracle.com/docs/cd/E17476_01/javase/1.4.2/docs/tooldocs/linux/java.html for more details.

"When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored."

Cheers,
Donald.

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