简体   繁体   English

Maven的java.lang.noclassdeffounderror

[英]java.lang.noclassdeffounderror for Maven

I am new at Maven and I recently created this program that requires multivaluedmap. 我是Maven的新手,我最近创建了需要多值映射的程序。 Since I did not have jsr311-api.jar, I downloaded it from online, but I have no clue where to place them. 由于我没有jsr311-api.jar,因此我从网上下载了它,但是我不知道将它们放在哪里。 I decided to upload it onto the apache-maven/lib folder and tried to run the program, but it did not work. 我决定将其上传到apache-maven / lib文件夹中,并尝试运行该程序,但是它不起作用。 This is the error that I get. 这是我得到的错误。

Exception in thread "main" java.lang.NoClassDefFoundError: javax/ws/rs/core/Mul
ivaluedMap
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
    at java.lang.Class.privateGetMethodRecursive(Unknown Source)
    at java.lang.Class.getMethod0(Unknown Source)
    at java.lang.Class.getMethod(Unknown Source)
    at sun.launcher.LauncherHelper.validateMainClass(Unknown Source)
    at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: javax.ws.rs.core.MultivaluedMap
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 7 more

I assumed that I'm getting errors from misplacing the .jar file. 我认为放错.jar文件会出错。 Thank you in advance for your help. 预先感谢您的帮助。

This is my pom file that I currently have: http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 这是我目前拥有的pom文件:http://maven.apache.org/xsd/maven-4.0.0.xsd“> 4.0.0

<groupId>com.test.api</groupId>
<artifactId>testing</artifactId>
<packaging>jar</packaging>
<version>0.1.0</version>
<name>testPlatform</name>

<properties>
    <source.version>1.7</source.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
    <plugins>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>

    </plugins>
</build>

<dependencies>
    <dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>3.8.1</version>
  <scope>test</scope>
</dependency>
    <!-- REST client -->
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-client</artifactId>
        <version>2.9.1</version>
    </dependency>

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

    <dependency>
        <groupId>joda-time</groupId>
        <artifactId>joda-time</artifactId>
        <version>2.3</version>
    </dependency>

    <!-- for deserialization -->
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.2.4</version>
    </dependency>



</dependencies>

I'm fairly sure that I know your problem here. 我相当确定我在这里知道您的问题。

You are using Maven to compile your code and make a jar files. 您正在使用Maven编译代码并制作一个jar文件。

Then you are trying to execute that jar file from outside maven with some java command line or another. 然后,您尝试使用一些Java命令行或其他命令从maven外部执行该jar文件。

When Maven calls Java to compile your code, it's adding all your dependencies to the classpath. 当Maven调用Java来编译代码时,它会将所有依赖项添加到类路径中。

When you are running your code, you aren't. 当您运行代码时,事实并非如此。

You can configure the maven-dependency-plugin to write out a text file with the pathnames of all your dependencies, and then you can use that to build a command line. 您可以配置maven-dependency-plugin以写出具有所有依赖项的路径名的文本文件,然后可以使用它来构建命令行。 or you can use the maven-surefire-plugin to run tests, or the exec-maven-plugin to just launch a class, or the appassembler plugin to build a package with the dependencies and a command-line wrapper, or ... 或者您可以使用maven-surefire-plugin运行测试,或使用exec-maven-plugin仅启动一个类,或使用appassembler插件来构建具有依赖项和命令行包装的程序包,或者...

Here's how to make the dependency plugin write out your class path. 这是使依赖性插件写出您的类路径的方法。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
      <execution>
        <id>build-classpath</id>
        <phase>generate-sources</phase>
        <goals>
           <goal>build-classpath</goal>
        </goals>
        <configuration>
         <outputFile>${project.build.directory}/classpath.txt</outputFile>
        </configuration>
      </execution>
    </executions>
  </plugin>

Seems like you have not included the javax.ws.rs.jar file in your class path. 好像您没有在类路径中包含javax.ws.rs.jar文件。

Download Link 下载链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM