java.lang.NoClassDefFoundError: javax/json/Json

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25420167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 08:00:42  来源:igfitidea点击:

java.lang.NoClassDefFoundError: javax/json/Json

javajsonmaven

提问by Luke Skywalker

I have a simple Java project where I would like to build a Json string. For this, I use javax.json:

我有一个简单的 Java 项目,我想在其中构建一个 Json 字符串。为此,我使用 javax.json:

import javax.json.Json;
import javax.json.JsonObjectBuilder;

public class MyClass {
    public void MyFunc() {
        JsonObjectBuilder myBuilder = Json.createObjectBuilder(); # this line will trigger the exception
        // And some other code
    }
}

The project is build with maven, and in the pom I have added these dependencies:

该项目是用 maven 构建的,在 pom 中我添加了这些依赖项:

<dependencies>
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>3.8.1</version>
    <scope>test</scope>
  </dependency>
  <dependency>
    <groupId>javax.json</groupId>
    <artifactId>javax.json-api</artifactId>
    <version>1.0</version>
  </dependency>
  <dependency>
      <groupId>org.glassfish</groupId>
      <artifactId>javax.json</artifactId>
      <version>1.0.4</version>
  </dependency>    
</dependencies>

Compilation is ok. When I execute the code using that command:

编译没问题。当我使用该命令执行代码时:

java -cp target/classes MyClass

The main is in MyClass. I get:

主要在 MyClass 中。我得到:

Exception in thread "main" java.lang.NoClassDefFoundError: javax/json/Json
[...]
Caused by: java.lang.ClassNotFoundException: javax.json.Json
        at java.net.URLClassLoader.run(URLClassLoader.java:366)
        at java.net.URLClassLoader.run(URLClassLoader.java:355)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
        ... 6 more

on the call to Json.createObjectBuilder().

在调用 Json.createObjectBuilder() 时。

Symptom is the same if you run it in the targer folder like that:

如果您在 targer 文件夹中运行它,则症状是相同的:

java MyClass

I have checked in my ~/.m2/repository folder and i have javax/json/javax.json-api/1.0/javax.json-api-1.0.jar. When I unzip it, I can see the Json.class file.

我已经检查了我的 ~/.m2/repository 文件夹,我有 javax/json/javax.json-api/1.0/javax.json-api-1.0.jar。当我解压它时,我可以看到 Json.class 文件。

What's wrong ???

怎么了 ???

采纳答案by Elad Tabak

The classpath does not have the javax.json-api-1.0.jar. Run it like this:

类路径没有 javax.json-api-1.0.jar。像这样运行它:

java -cp target/classes:~/.m2/repository/javax/json/javax.json-api/1.0/javax.json-api-1.0.jar MyClass

java -cp 目标/类:~/.m2/repository/javax/json/javax.json-api/1.0/javax.json-api-1.0.jar MyClass

回答by Marco Luzzara

Something like this just happened to me using Tomcat as container. However I am on IntelliJ and I solved it following this procedure:

像这样的事情发生在我使用 Tomcat 作为容器的时候。但是我在 IntelliJ 上,我按照以下程序解决了它:

Project StructureArtifacts→ (select your artifact)

项目结构工件→(选择您的工件)

On Output Layoutyou will see Maven's Project Libraries, select those for which you get a ClassNotFoundException, open the context menu (right click) and click on Put into /WEB-INF/lib.

输出布局上,您将看到 Maven 的项目库,选择您获得 的项目库,ClassNotFoundException打开上下文菜单(右键单击)并单击放入 /WEB-INF/lib

Automatically Tomcat will check on WEB-INF/libafter its own libfolder if it does not find here a reference to a class.

如果 Tomcat 在这里没有找到对类的引用,它会自动检查WEB-INF/lib它自己的lib文件夹。

Sorry for the off-topic on IntelliJ but it could have helped me too.

很抱歉在 IntelliJ 上跑题了,但它也可以帮助我。