Compiler doesn't find a class I have as maven dependency

强颜欢笑 提交于 2019-12-24 07:25:39

问题


I have a code that has a dependency on the RESTEasy JAX RS Client

I added it as a dependency to my project like this:

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

I can write the code normally, but when I try to compile, I get these errors:

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

Although I can find these classes normally, I know they are in my maven repo, and if I look them up using my IDE, I can access them, so I know they exist.

I tried changing the dependency scope to provided and compile just for the sake of testing it out, but with no luck.

Any idea what I might be missing?

EDIT

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

class failing to compile

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());
        }

    }

}

回答1:


You are not including a dependency to what it is complaining about:

This is where the API classes are:

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

search.maven.org is your friend!

The following isn't correct as well.

You have marked the resteasy-client.jar dependency, as well as the others, that you need as provided which means it will not be included on the classpath when executed or packaged.

Remove the <scope> element on all those dependencies, it is most likely not correct.

Review the <scope> element documentation and make sure this is what you intend.



来源:https://stackoverflow.com/questions/24827820/compiler-doesnt-find-a-class-i-have-as-maven-dependency

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!