Java 编译时如何通配符包含JAR文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1064481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:00:46  来源:igfitidea点击:

How to wildcard include JAR files when compiling?

javajarcompilationpackages

提问by

I have the following in a java file (MyRtmpClient.java):

我在 java 文件 (MyRtmpClient.java) 中有以下内容:

import org.apache.mina.common.ByteBuffer;

and ByteBufferis inside a JAR file (with the proper directory structure of course). That jar file and others I need are in the same directory as the .java file.

并且ByteBuffer在一个 JAR 文件中(当然有正确的目录结构)。该 jar 文件和我需要的其他文件与 .java 文件位于同一目录中。

Then I compile with the line:

然后我用以下行编译:

javac -cp ".;*.jar" MyRtmpClient.java

But I get the error:

但我收到错误:

MyRtmpClient.java:3: package org.apache.mina.common does not exist
import org.apache.mina.common.ByteBuffer;

MyRtmpClient.java:3: package org.apache.mina.common does not exist
import org.apache.mina.common.ByteBuffer;

How can I include jar files in my project?

如何在我的项目中包含 jar 文件?

采纳答案by cd1

your command line is correct, but there are some considerations:

您的命令行是正确的,但有一些注意事项:

  • you must have javac >= 1.6, because only in that version the compiler parses the "*" as various JAR files.
  • you must be running Windows, because ";" is the path separator for that operating system only (it doesn't work on Unix, the path separator on Unix is ":").
  • 您必须有 javac >= 1.6,因为只有在该版本中,编译器才会将“*”解析为各种 JAR 文件。
  • 您必须运行 Windows,因为“;” 仅是该操作系统的路径分隔符(它在 Unix 上不起作用,Unix 上的路径分隔符是“:”)。

I'm assuming that the JAR file has the proper directory structure as you stated.

我假设 JAR 文件具有您所说的正确目录结构。

回答by blispr

try including the jar file in your command line so :

尝试在命令行中包含 jar 文件,以便:

javac MyRtmpClient.java ByteBuffer.jar

javac MyRtmpClient.java ByteBuffer.jar

回答by Mark

javac does not understand *.jar in the classpath argument. You need to explicitly specify each jar. e.g.

javac 不理解类路径参数中的 *.jar。您需要明确指定每个 jar。例如

javac -cp ".;mina.jar" MyRtmpClient.java

回答by thanhduyspkt

In your case, I think JAVAC can not found jars file.

在您的情况下,我认为 JAVAC 找不到 jars 文件。

Please try:

请尝试:

PROJECT_PATH
- lib\a.jar
- src\package\b.java

PROJECT_PATH
- lib\a.jar
- src\package\b.java

cd @PROJECT_PATH

javac -classpath lib\a.jar src\package\b.java

回答by Rewinna

You cannot use -cpwith Javac. You have to use -classpathinstead (assuming the other settings are correct).

您不能-cpJavac 一起使用。您必须-classpath改用(假设其他设置正确)。

回答by shashank

Probably below syntax will work on windows dos command

可能以下语法适用于 windows dos 命令

javac -cp ".;first.jar;second.jar;third.jar" MyRtmpClient.java

回答by plesiv

If you have utilities findand trat your disposal (e.g. you're working Linux), you could do:

如果您有实用程序find并且tr可以随意使用(例如,您使用的是 Linux),您可以执行以下操作:

javac -cp .:`find * -name "*.jar" | tr "\n" ":"` MyRtmpClient.java

All jarfiles in the current directory and all it's sub-directories will be added (shell command lists all jarfiles and puts colons as separators between them).

所有罐子在当前目录中的文件和它所有的子目录将被添加(shell命令列出所有的jar文件,并提出冒号它们之间的分隔符)。



解释:

  • pair of the backticks ( `) denote shell commands to be executed,
  • find * -name "*.jar"finds and lists all jarfiles in hierarchy whose root is current folder,
  • vertical bar ( |) is pipe; connects output of findto the input of next command,
  • tr "\n" ":"replaces all newlinecharacters with coloncharacters.
  • 一对反引号 ( `) 表示要执行的 shell 命令,
  • find * -name "*.jar"查找并列出层次结构中根为当前文件夹的所有jar文件,
  • 竖线 ( |) 是管道;将 的输出连接find到下一个命令的输入,
  • tr "\n" ":"冒号字符替换所有换行符

回答by Maciej Matys

In javac JDK 6 and above You could use (note lack of .jar):

在 javac JDK 6 及更高版本中,您可以使用(注意缺少 .jar):

javac -cp ".;*" MyRtmpClient.java

to quote javac - Java programming language compiler

引用javac - Java 编程语言编译器

As a special convenience, a class path element containing a basename of * is considered equivalent to specifying a list of all the files in the directory with the extension .jar or .JAR.

For example, if directory foo contains a.jar and b.JAR, then the class path element foo/* is expanded to A.jar;b.JAR, except that the order of jar files is unspecified. All jar files in the specified directory, even hidden ones, are included in the list. A classpath entry consisting simply of * expands to a list of all the jar files in the current directory.

为方便起见,包含基本名称 * 的类路径元素被视为等效于指定目录中所有文件的列表,扩展名为 .jar 或 .JAR。

例如,如果目录 foo 包含 a.jar 和 b.JAR,则类路径元素 foo/* 扩展为 A.jar;b.JAR,只是未指定 jar 文件的顺序。指定目录中的所有 jar 文件,甚至隐藏的,都包含在列表中。仅由 * 组成的类路径条目扩展为当前目录中所有 jar 文件的列表。

回答by Ankush

In Java 8, the option ".;*" etc. which are mentioned above did not seem to work. I tried and found that : javac -cp '<location of jars>/*' MyRtmpClient.java

在 Java 8 中,上面提到的选项“.;*”等似乎不起作用。我尝试并发现: javac -cp '<location of jars>/*' MyRtmpClient.java

works:

作品:

<location of jar>can be /usr/local/classes/* or /home/developer/MyProject/*

<location of jar>可以是 /usr/local/classes/* 或 /home/developer/MyProject/*