将多个 .jar 与 javac 一起使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2143115/
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
Using multiple .jar with javac
提问by niko
pardon my terminology. I'm trying to use three jar files with a java program for my CS class. The first is funjava, a simplified java language, and the others are class definitions color and geometry. Here is my code and what happens when I try to run it.
请原谅我的术语。我正在尝试将三个 jar 文件与一个 java 程序一起用于我的 CS 类。第一个是funjava,一种简化的java语言,其他的是类定义颜色和几何。这是我的代码以及当我尝试运行它时会发生什么。
import colors.*;
class Canvas{
public static void main(String [] args){
System.out.println("test123");
Circle cr1 = new Circle( new Posn(1,2), 5, "blue");
Circle cr2 = new Circle( new Posn(5,4), 3, "red");
}
}
class Circle{
Posn center;
int rad;
String color;
Circle(Posn p, int r, String c){
this.center = p;
this.rad = r;
this.color = c;
}
}
class Posn{
int x;
int y;
Posn(int x, int y){
this.x = x;
this.y = y;
}
}
The last argument of Circle should be a color from the colors.jar, not a string.
Circle 的最后一个参数应该是来自 colors.jar 的颜色,而不是字符串。
niko@niko-laptop:~/Classes/Fundies2$ javac -cp *.jar Canvas.java
error: Class names, 'funjava.jar,geometry.jar', are only accepted if annotation processing is explicitly requested
1 error
niko@niko-laptop:~/Classes/Fundies2$ ls
1-20-10.java 1-21-10.java Book.class Canvas.class Circle.java Examples.class funjava.jar hw1~ Main.java OceanWorld.java
1-21-10 Author.class book.java Canvas.java colors.jar Examples.java geometry.jar Ishape OceanWorld Posn.class
1-21-10~ Author.java Book.java Circle.class Combo.java Fundies2.txt hw1 Main.class OceanWorld~ Rect.java
So how do I explicitly request annotation processing? Thank you.
那么如何显式请求注释处理呢?谢谢你。
采纳答案by ZoogieZork
In addition to Romain Muller's answer:
除了罗曼穆勒的回答:
If you want to quickly use all of the *.jar files in the current directory, and you're using JDK 6 or later, you can use a single-asterisk. In a unix shell (like in Linux), you'll need to escape the asterisk:
如果您想快速使用当前目录中的所有 *.jar 文件,并且您使用的是 JDK 6 或更高版本,则可以使用单星号。在 unix shell 中(如在 Linux 中),您需要转义星号:
javac -cp \* Canvas.java
This works when running the Java application as well:
这也适用于运行 Java 应用程序:
java -cp .:\* Canvas
Note the .:
to tell Java to look in the current directory as well as the *.jar files to find Canvas.class
.
请注意.:
告诉 Java 在当前目录中查找以及要查找的 *.jar 文件Canvas.class
。
On Windows, use a semicolon (;
) instead of a colon as a separator.
在 Windows 上,使用分号 ( ;
) 而不是冒号作为分隔符。
回答by Romain
As far as I know, the -cp option requires classpath to be specified as a colon or semi-colon-separated list of places in most situations, and not a comma-separated list as your OS seems to derivate when expanding *.jar
.
据我所知, -cp 选项要求在大多数情况下将类路径指定为冒号或分号分隔的位置列表,而不是逗号分隔的列表,因为您的操作系统似乎在扩展*.jar
.