类路径中的多个 jar 不起作用

发布于 2024-12-18 23:00:16 字数 546 浏览 3 评论 0原文

我在 stackoverflow 上看到了几个非常相似的问题,但没有遇到任何与我的问题完全匹配的问题。我有一个包含几个 .java 文件的文件夹,另一个包含两个 .jar 文件的文件夹。我需要在使用 javac 时包含两个 jar 文件,以便一次性编译整个项目:

$: javac -classpath .:~/myjardir/*.jar ~/myprojectdir/*.java

但是如果我这样做,则只有第一个 jar 被识别,并且依赖于第二个 jar 的所有内容都会抛出错误。令人惊讶的是,如果我单独编译每个程序,

$: javac -classpath .:~/myjardir/oneofthejars.jar ~/myprojectdir/file1.java

那么一切都会正常工作。我还在 Eclipse 中单独编译了该项目,只是为了测试代码和 jar。只有当我尝试在命令行中使用两个带有 -classpath 的 jar 时,才会出现错误。通配符条目应该在 JDK6 中工作,所以我在这里不知所措。

I have seen several very similar questions on stackoverflow, but haven't come across anything that exactly matches my problem. I have a folder with several .java files, and another folder with two .jar files. I need to include both the jar files while using javac so that the entire project gets compiled at one go:

$: javac -classpath .:~/myjardir/*.jar ~/myprojectdir/*.java

But if I do this, only the first jar is recognized, and everything that depends on the second jar throws an error. Surprisingly, if I compile each program separately,

$: javac -classpath .:~/myjardir/oneofthejars.jar ~/myprojectdir/file1.java

then everything works fine. I have also compiled the project separately in Eclipse just to test the code and the jars. It is only when I try to use both the jars with -classpath in command line that I get the errors. Wildcard entries are supposed to work in JDK6, so I am at a loss here.

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

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

发布评论

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

评论(3

雪花飘飘的天空 2024-12-25 23:00:16

类路径通配符的工作方式与 Unix shell 中不同。 * 表示目录中名为 *.jar 的所有内容。因此,您不需要执行 *.jar,而只需执行 *。以下内容应该满足您的要求:

$: javac -classpath .:~/myjardir/* ~/myprojectdir/*.java

请参阅 了解类路径通配符Java SE 6文档

The class path wildcards don't work like they do in the Unix shells. The * means everything named *.jar in the directory. So you don't need to do *.jar but just *. The following should do what you want:

$: javac -classpath .:~/myjardir/* ~/myprojectdir/*.java

See Understanding class path wildcards in the Java SE 6 documentation.

笔芯 2024-12-25 23:00:16

请参阅这里的答案,但这里是Java文档中的相关段落:

类路径条目可以包含基本名称通配符 *,这被认为相当于指定目录中扩展名为 .jar 或 .JAR 的所有文件的列表。例如,类路径条目 foo/** 指定名为 foo 的目录中的所有 JAR 文件。仅由 * 组成的类路径条目将扩展为当前目录中所有 jar 文件的列表。

see the SO answer here but here's the relevant paragraph from the Java documentation:

Class path entries can contain the basename wildcard character *, which is considered equivalent to specifying a list of all the files in the directory with the extension .jar or .JAR. For example, the class path entry foo/** specifies all JAR files in the directory named foo. A classpath entry consisting simply of * expands to a list of all the jar files in the current directory.

肤浅与狂妄 2024-12-25 23:00:16

如果您想要在类路径中包含多个内容,据我所知,您必须通过类路径分隔符将它们分开。所以 ./lib:/lib/mylib 在 unix 系统上是一个有效的类路径,我认为在 Windows 系统上等效的是 .\lib;\lib\mylib 。您不必指定每个文件,只需指定目录即可。

if you want multiple things in a classpath, you have to separate them by the classpath separator as far as I know. So ./lib:/lib/mylib would be a valid classpath on a unix system, I think the equivalent would be .\lib;\lib\mylib on a windows system. You don't have to specify every file, just the directories.

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