有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java很简单。找不到类def

我正在用resteasywildfly构建rest客户端。 据stacktrace说,我可能有依赖的问题
已经在pom中多次尝试更改范围、删除或添加依赖项。xml不会给出任何结果,除了另一个stacktrace中未找到的内容
这是我的

stacktrace

    Exception in thread "main" java.lang.NoClassDefFoundError: javax/ws/rs/core/GenericType
    at groupId.Main.<init>(Main.java:33)
    at groupId.Main.main(Main.java:36)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: java.lang.ClassNotFoundException: javax.ws.rs.core.GenericType
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 7 more

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--
    JBoss, Home of Professional Open Source
    Copyright 2013, Red Hat, Inc. and/or its affiliates, and individual
    contributors by the @authors tag. See the copyright.txt in the
    distribution for a full listing of individual contributors.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at
    http://www.apache.org/licenses/LICENSE-2.0
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
-->
<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/maven-v4_0_0.xsd">
   <modelVersion>4.0.0</modelVersion>

   <parent>
      <artifactId>restClient</artifactId>
      <groupId>groupId</groupId>
      <version>1.0-SNAPSHOT</version>
   </parent>

   <artifactId>rest-client</artifactId>
   <packaging>war</packaging>

   <name>restClient: WAR Module</name>

   <url>http://wildfly.org</url>
   <licenses>
      <license>
         <name>Apache License, Version 2.0</name>
         <distribution>repo</distribution>
         <url>http://www.apache.org/licenses/LICENSE-2.0.html</url>
      </license>
   </licenses>

   <dependencies>

      <!-- Dependency on the EJB module so we can use it's services if needed -->

       <!--&lt;!&ndash; https://mvnrepository.com/artifact/org.jboss.resteasy/resteasy-jaxrs &ndash;&gt;-->

      <dependency>
         <groupId>org.jboss.resteasy</groupId>
         <artifactId>resteasy-client</artifactId>
         <version>3.1.2.Final</version>
          <scope>provided</scope>
      </dependency>
       <dependency>
           <groupId>org.jboss.resteasy</groupId>
           <artifactId>jaxrs-api</artifactId>

           <scope>provided</scope>
       </dependency>
       <!-- https://mvnrepository.com/artifact/org.jboss.resteasy/resteasy-jaxrs -->
       <dependency>
           <groupId>org.jboss.resteasy</groupId>
           <artifactId>resteasy-jaxrs</artifactId>
           <version>3.1.2.Final</version>
           <scope>provided</scope>
       </dependency>

       <!-- https://mvnrepository.com/artifact/org.jboss.resteasy/resteasy-multipart-provider -->
       <dependency>
           <groupId>org.jboss.resteasy</groupId>
           <artifactId>resteasy-multipart-provider</artifactId>
           <version>3.1.2.Final</version>
           <scope>provided</scope>
       </dependency>
       <!-- https://mvnrepository.com/artifact/org.jboss.resteasy/resteasy-jaxb-provider -->
       <!--<dependency>-->
       <!--<groupId>org.jboss.resteasy</groupId>-->
       <!--<artifactId>resteasy-jaxb-provider</artifactId>-->
       <!--<version>3.1.2.Final</version>-->
       <!--<scope>provided</scope>-->
       <!--</dependency>-->

       <!-- https://mvnrepository.com/artifact/org.jboss.resteasy/resteasy-jaxrs -->
       <!-- https://mvnrepository.com/artifact/javax.ejb/ejb-api -->



       <!-- Import the CDI API, we use provided scope as the API is included in JBoss WildFly -->
       <dependency>
           <groupId>javax.enterprise</groupId>
           <artifactId>cdi-api</artifactId>
           <scope>provided</scope>
       </dependency>

       <!-- Import the JSF API, we use provided scope as the API is included in JBoss WildFly -->
       <dependency>
           <groupId>org.jboss.spec.javax.faces</groupId>
           <artifactId>jboss-jsf-api_2.2_spec</artifactId>
           <scope>provided</scope>
       </dependency>
       <!-- https://mvnrepository.com/artifact/javax.ejb/ejb-api -->
       <dependency>
           <groupId>javax.ejb</groupId>
           <artifactId>ejb-api</artifactId>
           <version>3.0</version>
       </dependency>

       <!-- Import the JPA API, we use provided scope as the API is included in JBoss WildFly -->
       <!-- https://mvnrepository.com/artifact/org.hibernate.javax.persistence/hibernate-jpa-2.1-api -->
       <dependency>
           <groupId>org.hibernate.javax.persistence</groupId>
           <artifactId>hibernate-jpa-2.1-api</artifactId>

           <version>1.0.0.Final</version>

       </dependency>
       <!-- https://mvnrepository.com/artifact/javax.persistence/persistence-api -->
       <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
       <dependency>
           <groupId>org.hibernate</groupId>
           <artifactId>hibernate-core</artifactId>
           <scope>provided</scope>
           <exclusions>
               <exclusion>
                   <groupId>dom4j</groupId>
                   <artifactId>dom4j</artifactId>
               </exclusion>
           </exclusions>
       </dependency>
       <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-entitymanager -->
       <dependency>
           <groupId>org.hibernate</groupId>
           <artifactId>hibernate-entitymanager</artifactId>
           <scope>provided</scope>
           <exclusions>
               <exclusion>
                   <groupId>dom4j</groupId>
                   <artifactId>dom4j</artifactId>
               </exclusion>
           </exclusions>
       </dependency>
       <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-validator -->
       <dependency>
           <groupId>org.hibernate</groupId>
           <artifactId>hibernate-validator</artifactId>
           <scope>provided</scope>
       </dependency>

       <!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
       <!--<dependency>-->
       <!--<groupId>org.postgresql</groupId>-->
       <!--<artifactId>postgresql</artifactId>-->
       <!--<version>42.1.0</version>-->
       <!--</dependency>-->

       <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
       <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.jaxrs/jackson-jaxrs-json-provider -->
       <dependency>
           <groupId>com.fasterxml.jackson.jaxrs</groupId>
           <artifactId>jackson-jaxrs-json-provider</artifactId>
           <version>2.9.0.pr3</version>
       </dependency>
       <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.jaxrs/jackson-jaxrs-xml-provider -->
       <dependency>
           <groupId>com.fasterxml.jackson.jaxrs</groupId>
           <artifactId>jackson-jaxrs-xml-provider</artifactId>
           <version>2.9.0.pr3</version>
       </dependency>

       <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
       <dependency>
           <groupId>commons-io</groupId>
           <artifactId>commons-io</artifactId>
           <version>2.5</version>
       </dependency>

   </dependencies>

   <build>
      <finalName>${project.artifactId}</finalName>
      <plugins>
         <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>${version.war.plugin}</version>
            <configuration>
               <!-- Java EE 7 doesn't require web.xml, Maven needs to catch up! -->
               <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
         </plugin>
      </plugins>
   </build>

</project>

共 (1) 个答案

  1. # 1 楼答案

    根据stacktrace,您正在IntelliJ中运行一个main方法

    所有RESTEasy依赖项都标记为“已提供”。这意味着应用程序需要运行时容器(例如应用程序服务器)提供它们。IntelliJ在运行时本身不会提供这些依赖项,因此会出现ClassNotFoundException错误

    https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html

    provided

    This is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime. For example, when building a web application for the Java Enterprise Edition, you would set the dependency on the Servlet API and related Java EE APIs to scope provided because the web container provides those classes. This scope is only available on the compilation and test classpath, and is not transitive.

    要在IntelliJ中测试应用程序,只需删除提供的作用域