编译错误,包javax.ws.rs不存在,依赖关系无法从maven解析

时间:2013-01-07 03:16:46

标签: maven

我之前已经提到了pom文件中的依赖库,也存在库系统路径,但是在使用maven clean install -e -X编译期间,它会抛出错误,说包不存在。

**[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /C:/Users/gopc/workspace/RestfulService/src/restfu/Hello.java:[4,19] C:\Users\gopc\workspace\RestfulService\src\restfu\Hello.java:4: package javax.ws.rs does not exist
[ERROR] /C:/Users/gopc/workspace/RestfulService/src/restfu/Hello.java:[5,19] C:\Users\gopc\workspace\RestfulService\src\restfu\Hello.java:5: package javax.ws.rs does not exist
[ERROR] /C:/Users/gopc/workspace/RestfulService/src/restfu/Hello.java:[6,19] C:\Users\gopc\workspace\RestfulService\src\restfu\Hello.java:6: package javax.ws.rs does not exist
[ERROR] /C:/Users/gopc/workspace/RestfulService/src/restfu/Hello.java:[8,1] C:\Users\gopc\workspace\RestfulService\src\restfu\Hello.java:8: package javax.ws.rs.core does not exist
[ERROR] /C:/Users/gopc/workspace/RestfulService/src/restfu/Hello.java:[21,2] C:\Users\gopc\workspace\RestfulService\src\restfu\Hello.java:21: cannot find symbol
symbol: class Path**

POM文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>RestfulService</groupId>
  <artifactId>RestfulService</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <build>
    <sourceDirectory>${basedir}/src</sourceDirectory>
    <outputDirectory>${basedir}/build/classes</outputDirectory>
  <pluginManagement>

    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.0</version>
        <configuration>
          <source>1.5</source>
          <target>1.5</target>
        </configuration>
      </plugin>
    </plugins>
</pluginManagement>

</build>

<dependencyManagement>
<dependencies>

  <dependency>
    <groupId>jersey-server</groupId>
    <artifactId>jersey-server</artifactId>
    <version>1.4</version>
    <scope>compile</scope>
    <systemPath>${basedir}/lib/jersey-server-1.4.jar</systemPath>
  </dependency>

<dependency>
        <groupId>javax.ws.rs</groupId>
        <artifactId>javax.ws.rs</artifactId>
        <version>1.4</version>
        <scope>system</scope>
        <systemPath>${basedir}/lib/javax.ws.rs.jar</systemPath>
</dependency>
</dependencies>
</dependencyManagement>
</project>

7 个答案:

答案 0 :(得分:27)

您需要在POM中包含Java EE依赖项,并使用provided范围(也就是说,应用程序服务器将提供最终的文件,但在此期间我需要它们用于编译)。

<dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-web-api</artifactId>
    <version>6.0</version>
    <scope>provided</scope>
</dependency>

答案 1 :(得分:6)

在系统更新后,当NetBeans更改字体和GUI时,我遇到了更多类似的问题。我通过执行

在NetBeans IDE中添加Java EE 6 API Library来解决此问题

myProject->Properties->Libraries->Add Library

答案 2 :(得分:4)

我认为这种依赖性更好

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

答案 3 :(得分:2)

Jersey在java.net存储库中发布;只需使用它(或更好地编辑您的用户settings.xml):

<repositories>
    <repository>
        <id>maven2-repository.java.net</id>
        <name>Java.net Repository for Maven</name>
        <url>http://download.java.net/maven/2/</url>
        <layout>default</layout>
    </repository>
</repositories>

<dependencies>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-server</artifactId>
        <version>1.8</version>
    </dependency>
</dependencies>

JSR311(javax.ws.rs)将由maven自动下载为jersey-core依赖。

答案 4 :(得分:1)

@Perception回复的依赖也是必需的,但对我来说还不够。还需要以下依赖项:

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

https://mvnrepository.com/artifact/javax.ws.rs/javax.ws.rs-api/2.0

这为我解决了这个问题!

答案 5 :(得分:0)

就我而言,它是IntelliJ&#39; make&#39;这是行不通的,虽然mvn clean install工作正常。解决它的方法是删除两个错误的&#34;模块&#34;从&#34;项目结构&#34; - &gt;模块选项卡,模块是:main和test。不知何故,他们从现有的来源&#39;创建项目进入那里。

答案 6 :(得分:0)

我遇到了这个错误,对我来说解决方案是同时拥有这两个依赖项:

<dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>6.0</version>
            <scope>provided</scope>
</dependency>

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