在命令行的类路径中包含 jars(javac 或 apt)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2096283/
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
Including jars in classpath on commandline (javac or apt)
提问by sixtyfootersdude
trying to run this program. I think that to setup all of the web service stuff I need to run apt. (Although using javac I am having the same issue). I think what I am getting is compile errors. (Shown at bottom).
试图运行这个程序。我认为要设置我需要运行 apt 的所有 Web 服务。(虽然使用 javac 我有同样的问题)。我认为我得到的是编译错误。(显示在底部)。
I think what I need to do is include this jar in my class path: jsr181-api.jar (source). Is there a simple temporary way to do this (on solaris)? I don't want to add it to my bash_rc file (it is there forever). I also know that there is some way to do it using a manifest text file but that seemed complicated so I didn't look into it yet. Can I just do something like:
我想我需要做的是在我的类路径中包含这个 jar: jsr181-api.jar (source)。有没有一种简单的临时方法来做到这一点(在solaris上)?我不想将它添加到我的 bash_rc 文件中(它永远存在)。我也知道有一些方法可以使用清单文本文件来做到这一点,但这看起来很复杂,所以我还没有研究它。我可以做这样的事情:
javac HelloImp <listOfJars>
or
或者
ant HelloImp <listOfJars>
Code:
代码:
package server;
import javax.jws.WebService;
@WebService
public class HelloImpl {
/**
* @param name
* @return Say hello to the person.
*/
public String sayHello(String name) {
return "Hello, " + name + "!";
}
}
Compile errors:
编译错误:
HelloImpl.java:3: package javax.jws does not exist
import javax.jws.WebService;
^
HelloImpl.java:5: cannot find symbol
symbol: class WebService
@WebService
^
2 errors
Update: Cool that is wrapped up but it is still not quite working. I have created a new question to keep things nice and organized:
更新:很酷,但它仍然没有完全工作。我创建了一个新问题以保持良好和有条理:
采纳答案by Kevin
Try the following:
请尝试以下操作:
java -cp jar1:jar2:jar3:dir1:. HelloWorld
The default classpath (unless there is a CLASSPATH environment variable) is the current directory so if you redefine it, make sure you're adding the current directory (.) to the classpath as I have done.
默认的类路径(除非有 CLASSPATH 环境变量)是当前目录,因此如果您重新定义它,请确保像我所做的那样将当前目录 (.) 添加到类路径中。
回答by matt b
Use the -cp
or -classpath
switch.
使用-cp
或-classpath
开关。
$ java -help
Usage: java [-options] class [args...]
(to execute a class)
or java [-options] -jar jarfile [args...]
(to execute a jar file)
where options include:
...
-cp <class search path of directories and zip/jar files>
-classpath <class search path of directories and zip/jar files>
A ; separated list of directories, JAR archives,
and ZIP archives to search for class files.
(Note that the separator used to separate entries on the classpath differs between OSes, on my Windows machine it is ;
, in *nix it is usually :
.)
(请注意,用于分隔类路径中条目的分隔符因操作系统而异,在我的 Windows 机器上是;
,在 *nix 中通常是:
。)
回答by sixtyfootersdude
Using:
使用:
apt HelloImpl.java -classpath /sac/tools/thirdparty/jaxws-ri/jaxws-ri-2.1.4/lib/jsr181-api.jar:.
works but it gives me another error, see new question
有效,但它给了我另一个错误,请参阅新问题
回答by Ben
In windows:
在窗口中:
java -cp C:/.../jardir1/*;C:/.../jardir2/* class_with_main_method
make sure that the class with the main function is in one of the included jars
确保具有 main 函数的类位于包含的 jar 之一中
回答by Stoica Mircea
javac HelloWorld.java -classpath ./javax.jar , assuming javax is in current folder, and compile target is "HelloWorld.java", and you can compile without a main method
javac HelloWorld.java -classpath ./javax.jar ,假设 javax 在当前文件夹中,编译目标是“HelloWorld.java”,你可以在没有 main 方法的情况下进行编译
回答by luisv
Note for Windows users, the jars should be separated by ;
and not :
.
请注意,对于 Windows 用户,jar 应该用;
和分隔,而不是:
。
for example:
javac -cp external_libs\lib1.jar;other\lib2.jar;
例如:
javac -cp external_libs\lib1.jar;other\lib2.jar;