【问题标题】:Compiler doesn't find a class I have as maven dependency编译器找不到我作为 Maven 依赖项的类
【发布时间】:2014-07-18 14:47:16
【问题描述】:

我的代码依赖于RESTEasy JAX RS Client

我将它作为依赖项添加到我的项目中,如下所示:

<dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-client</artifactId>
    <version>3.0.8.Final</version>
</dependency>

我可以正常编写代码,但是当我尝试编译时,我得到了这些错误:

java: cannot access javax.ws.rs.client.ClientBuilder
  class file for javax.ws.rs.client.ClientBuilder not found

java: cannot access javax.ws.rs.core.Link
  class file for javax.ws.rs.core.Link not found

java: cannot access javax.ws.rs.client.WebTarget
  class file for javax.ws.rs.client.WebTarget not found

虽然我可以正常找到这些类,但我知道它们在我的 maven 存储库中,如果我使用我的 IDE 查找它们,我可以访问它们,所以我知道它们存在。

我尝试将依赖范围更改为 providedcompile 只是为了测试它,但没有运气。

知道我可能缺少什么吗?

编辑

相关的 pom.xml

<modelVersion>4.0.0</modelVersion>

<artifactId>my-project-id</artifactId>
<name>MyProject Name</name>

<dependencies>
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.2.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-client</artifactId>
        <version>3.0.8.Final</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
    </plugins>
</build>

类编译失败

import org.jboss.resteasy.client.jaxrs.ResteasyClient;
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;
import my.project.representations.App;

public class Main {

    public static void main(String[] args) {

        ResteasyClient client = new ResteasyClientBuilder().build();
        ResteasyWebTarget target = client.target(SERVER_URL);

        String token = "access_token";

        target.register(new BearerTokenFilter(token));

        Admin admin = target.proxy(Admin.class);

        Realm realm = admin.realm("realm_name");

        for (App app : realm.apps().findAll()) {
            System.out.println(app.getName());
        }

    }

}

【问题讨论】:

  • 你是怎么编译的?
  • 我用 IDE (IntelliJ IDEA) 编译它,当我看到这个错误时,我尝试通过 maven mvn clean install 编译它显示了同样的问题
  • 请提供.pom 和一个失败的源文件,如果可能的话,请提供最小失败示例。
  • 对不起,我不确定你想让我在这里发布什么,你能详细说明一下吗?
  • 我想查看“pom.xml”文件,请删除不相关的内容和一个无法编译的“.java”文件。

标签: maven resteasy dependency-management


【解决方案1】:

你没有包含对它所抱怨的内容的依赖:

这是 API 类所在的位置:

<dependency>
    <groupId>javax.ws.rs</groupId>
    <artifactId>javax.ws.rs-api</artifactId>
    <version>2.0</version>
</dependency>

search.maven.org是你的朋友!

以下也不正确。

您已将 resteasy-client.jar 以及其他您需要的依赖项标记为 provided,这意味着在执行或打包时它将包含在类路径中。

删除所有这些依赖项上的 &lt;scope&gt; 元素,这很可能是不正确的。

查看&lt;scope&gt; 元素文档并确保这是您想要的。

【讨论】:

  • 我尝试添加 jaxrs-apiresteasy-jaxrs 并且它起作用了,这很奇怪,因为我认为我不必明确地这样做
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-03-22
  • 2018-11-03
  • 2016-10-01
  • 2020-04-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多