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

时间:2021-05-10 19:23:19

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

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

import org.apache.mina.common.ByteBuffer;

and ByteBuffer is 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:包org.apache.mina.common不存在import org.apache.mina.common.ByteBuffer;

How can I include jar files in my project?

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

9 个解决方案

#1


26  

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.
  • 你必须有javac> = 1.6,因为只有在那个版本中,编译器才会将“*”解析为各种JAR文件。
  • 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 ":").
  • 你必须运行Windows,因为“;”是仅适用于该操作系统的路径分隔符(它在Unix上不起作用,Unix上的路径分隔符是“:”)。

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

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

#2


16  

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

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

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

#3


8  

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

#4


8  

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.

作为一种特殊的方便,包含基本名称*的类路径元素被认为等同于指定扩展名为.jar或.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.

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

#5


6  

If you have utilities find and tr at your disposal (e.g. you're working Linux), you could do:

如果你有自己的实用程序find和tr(例如你正在使用Linux),你可以这样做:

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

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

将添加当前目录中的所有jar文件及其所有子目录(shell命令列出所有jar文件,并将冒号作为它们之间的分隔符放置)。


Explanation:

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

#6


4  

Probably below syntax will work on windows dos command

可能下面的语法将适用于Windows dos命令

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

#7


1  

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' / *'MyRtmpClient.java 的位置>

works:

作品:

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

可以是/ usr / local / classes / *或/ home / developer / MyProject / * 的位置>

#8


-2  

try including the jar file in your command line so :

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

javac MyRtmpClient.java ByteBuffer.jar

javac MyRtmpClient.java ByteBuffer.jar

#9


-2  

You cannot use -cp with Javac. You have to use -classpath instead (assuming the other settings are correct).

您不能将-cp与Javac一起使用。您必须使用-classpath(假设其他设置正确)。

#1


26  

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.
  • 你必须有javac> = 1.6,因为只有在那个版本中,编译器才会将“*”解析为各种JAR文件。
  • 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 ":").
  • 你必须运行Windows,因为“;”是仅适用于该操作系统的路径分隔符(它在Unix上不起作用,Unix上的路径分隔符是“:”)。

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

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

#2


16  

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

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

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

#3


8  

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

#4


8  

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.

作为一种特殊的方便,包含基本名称*的类路径元素被认为等同于指定扩展名为.jar或.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.

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

#5


6  

If you have utilities find and tr at your disposal (e.g. you're working Linux), you could do:

如果你有自己的实用程序find和tr(例如你正在使用Linux),你可以这样做:

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

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

将添加当前目录中的所有jar文件及其所有子目录(shell命令列出所有jar文件,并将冒号作为它们之间的分隔符放置)。


Explanation:

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

#6


4  

Probably below syntax will work on windows dos command

可能下面的语法将适用于Windows dos命令

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

#7


1  

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' / *'MyRtmpClient.java 的位置>

works:

作品:

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

可以是/ usr / local / classes / *或/ home / developer / MyProject / * 的位置>

#8


-2  

try including the jar file in your command line so :

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

javac MyRtmpClient.java ByteBuffer.jar

javac MyRtmpClient.java ByteBuffer.jar

#9


-2  

You cannot use -cp with Javac. You have to use -classpath instead (assuming the other settings are correct).

您不能将-cp与Javac一起使用。您必须使用-classpath(假设其他设置正确)。