在命令行(javac 或 apt)上的类路径中包含 jars

问题描述 投票:0回答:5

尝试运行这个程序。我认为要设置所有网络服务,我需要运行 apt。 (虽然使用 javac 我遇到了同样的问题)。我认为我得到的是编译错误。 (显示在底部)。

我认为我需要做的就是将此 jar 包含在我的类路径中:jsr181-api.jar (源代码)。有没有一种简单的临时方法可以做到这一点(在Solaris上)?我不想将它添加到我的 bash_rc 文件中(它永远存在)。我还知道有一些方法可以使用清单文本文件来完成此操作,但这看起来很复杂,所以我还没有研究它。我可以做这样的事情吗:

javac HelloImp <listOfJars>

ant HelloImp <listOfJars>

代码:

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 + "!";
   }
}

编译错误:

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

更新: 很酷,已经完成,但仍然无法正常工作。我创建了一个 新问题 以使事情保持良好和有序:

java jar
5个回答
186
投票

尝试以下操作:

java -cp jar1:jar2:jar3:dir1:. HelloWorld

在 Windows 中使用;

java -cp jar1;jar2;jar3;dir1;. HelloWorld

默认的类路径(除非有 CLASSPATH 环境变量)是当前目录,因此如果您重新定义它,请确保将当前目录 (.) 添加到类路径,就像我所做的那样。


35
投票

在窗口中:

java -cp C:/.../jardir1/*;C:/.../jardir2/* class_with_main_method

确保具有主要功能的类位于包含的 jar 之一中


32
投票

Windows 用户请注意,罐子应该用

;
分隔,而不是
:

例如:

javac -cp external_libs\lib1.jar;other\lib2.jar;


17
投票

使用

-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.  

(请注意,用于分隔类路径上条目的分隔符在不同操作系统中有所不同,在我的 Windows 机器上它是

;
,在 *nix 中通常是
:
。)


2
投票

javac HelloWorld.java -classpath ./javax.jar ,假设javax在当前文件夹中,编译目标是“HelloWorld.java”,无需main方法即可编译

© www.soinside.com 2019 - 2024. All rights reserved.