简体   繁体   English

Intellij Java:java.lang.NoClassDefFoundError:org/json/JSONException

[英]Intellij Java: java.lang.NoClassDefFoundError: org/json/JSONException

I need help.我需要帮助。 I spent several hours trying to solve this problem but I can't.我花了几个小时试图解决这个问题,但我做不到。 I code on Intellij Community Edition 2018.2 OS Windows 10, here is my java project structure.我在 Intellij Community Edition 2018.2 OS Windows 10 上编码,这是我的 java 项目结构。

在此处输入图像描述

my Main.java source:我的 Main.java 来源:

package id.simko;

public class Main {

    public static void main(String[] args) {
        System.out.println("Hello World!");

        SqlServerConn sqlServerConn = new SqlServerConn();
        sqlServerConn.connectDbSqlServer();
        sqlServerConn.selectSqlServer();
    }
}

SqlServerConn.java: SqlServerConn.java:

package id.simko;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.sql.*;

class SqlServerConn {
    private Connection conn;

    void connectDbSqlServer() {

        String db_connect_string = "jdbc:sqlserver://localhost";
        String db_name = "db1";
        String db_userid = "sa";
        String db_password = "sa";

        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            conn = DriverManager.getConnection(db_connect_string+";databaseName="+db_name,
                    db_userid, db_password);
            System.out.println("connected to sql server");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    void selectSqlServer(){
        Statement statement;
        try {
            // ... skipped for clarity
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

my MANIFEST.MF:我的清单.MF:

Manifest-Version: 1.0
Main-Class: id.simko.Main

My pom.xml:我的 pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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>id.simko</groupId>
    <artifactId>replicator</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId>
            <version>7.0.0.jre8</version>
        </dependency>
    </dependencies>

</project>

I configured File -> Project Structure -> Artifacts, and added libs/java-json.jar.我配置了文件 -> 项目结构 -> 工件,并添加了 libs/java-json.jar。

在此处输入图像描述

Then lastly, on Intellij I clicked Build -> Build Artifacts -> replicator.jar -> Build.最后,在 Intellij 上,我点击了 Build -> Build Artifacts ->replicator.jar -> Build。 And my executable jar was created successfully.我的可执行文件 jar 已成功创建。

But when I tried to run it, error happens.但是当我尝试运行它时,会发生错误。 here is the screenshot.这是屏幕截图。

在此处输入图像描述

please help.. I know this question already answered here: Java: NoClassDefFoundError: org/json/JSONException but the solution cannot solve my problem.请帮忙..我知道这个问题已经在这里回答了: Java: NoClassDefFoundError: org/json/JSONException但该解决方案无法解决我的问题。 maybe this is a bug from intellij?也许这是来自 intellij 的错误?

==================================================================== ==================================================== ===================

LAST UPDATE FROM ME: I tried for hours until I give up.我的最新消息:我尝试了几个小时直到我放弃。 For now, the only possible way to create an executable application out of java project in Windows 10 is by NOT using Intellij CE 2018.2.目前,从 Windows 10 中的 java 项目创建可执行应用程序的唯一可能方法是不使用 Intellij CE 2018.2。 I tried NetBeans 8.2 IDE and I successfully created executable bat with gradle plugin & cygwin.我尝试了 NetBeans 8.2 IDE 并使用 gradle 插件和 cygwin 成功创建了可执行 bat。

I will leave this question unanswered, because I know someday someone will have a way, but for now I give up and tried a different IDE, at least for creating executable.我不会回答这个问题,因为我知道有一天有人会有办法,但现在我放弃并尝试了不同的 IDE,至少用于创建可执行文件。

I was facing same issue.我面临同样的问题。 After spending hours, I found a solution to resolve this issue.花了几个小时后,我找到了解决此问题的解决方案。 What I need to do was:我需要做的是:

Go to : File -> Project Structure -> Liabraries Add "org.springframework.boot:spring-boot-configuration-processor:2.3.4.RELEASE" in liabraries.转到:文件-> 项目结构-> 库在库中添加“org.springframework.boot:spring-boot-configuration-processor:2.3.4.RELEASE”。

Now clean, build and run the project现在清理,构建并运行项目

Note: You can change the version of above package注意:您可以更改上述包的版本

将 jar 文件放在 lib 文件夹下,因为在运行时它无法识别您的 jar 我认为是这样..

Open your replicator.jar (can use WinRAR) and see whether the class JSONException is available inside the jar.打开您的replicator.jar (可以使用WinRAR)并查看类JSONException在jar 中是否可用。 If not you need to include that inside your jar.如果不是,则需要将其包含在 jar 中。 For that you can use maven shade( https://maven.apache.org/plugins/maven-shade-plugin/ ) plugin or assemble plugin( http://maven.apache.org/plugins/maven-assembly-plugin/ ) to include all dependent classes into your executable jar.为此,您可以使用 maven shade( https://maven.apache.org/plugins/maven-shade-plugin/ )插件或组装插件( http://maven.apache.org/plugins/maven-assembly-plugin/ ) 将所有依赖类包含到您的可执行 jar 中。

This post would help you Including dependencies in a jar with Maven这篇文章将帮助您使用 Maven 在 jar 中包含依赖项

In order to resolve the issue, here what I've done :为了解决这个问题,我在这里做了什么:

Remove Some IDE files/folders删除一些 IDE 文件/文件夹

I deleted the .idea folder and the .iml file containing the project name.我删除了.idea文件夹和包含项目名称的.iml文件。 Files and folder to delete要删除的文件和文件夹

Reset Intellij Cash and restart Intellij重置 Intellij Cash 并重新启动 Intellij

Reload Maven project重新加载Maven项目

Reload maven project重新加载 Maven 项目

That how I solve most of my Intellij IDEA Issues那我是如何解决大部分 Intellij IDEA 问题的

My solution to this issue using Intellij was to create a library folder/directory "lib" and I included the JSON jar file.我使用 Intellij 解决此问题的方法是创建一个库文件夹/目录“lib”,并包含 JSON jar 文件。

File > Project Structure > Artifacts文件 > 项目结构 > 工件

enter image description here在此处输入图像描述

暂无
暂无

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

相关问题 JApplet java.lang.NoClassDefFoundError:org / json / JSONException - JApplet java.lang.NoClassDefFoundError: org/json/JSONException 处女座服务器抛出java.lang.NoClassDefFoundError:org / json / JSONException部署Java包时出错 - Virgo Server Throwing java.lang.NoClassDefFoundError: org/json/JSONException Error while deploying a Java package Java:NoClassDefFoundError:org / json / JSONException - Java: NoClassDefFoundError: org/json/JSONException 运行 java 类时,Tomcat 服务器不会在 Eclipse 中启动。 引起:java.lang.NoClassDefFoundError:org/json/JSONException - Tomcat server won't start in Eclipse when running java class. Caused by: java.lang.NoClassDefFoundError: org/json/JSONException java.lang.NoClassDefFoundError:Intellij中的org / hamcrest / SelfDescribing - java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing in Intellij IntelliJ java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing - IntelliJ java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing jython java.lang.NoClassDefFoundError:java.lang.NoClassDefFoundError:org / json / simple / JSONObject - jython java.lang.NoClassDefFoundError: java.lang.NoClassDefFoundError: org/json/simple/ JSONObject Java servlet java.lang.NoClassDefFoundError:org / json / JSONObject - Java servlet java.lang.NoClassDefFoundError: org/json/JSONObject 线程“main”中的异常java.lang.NoClassDefFoundError:org / json / JSONObject - Exception in thread “main” java.lang.NoClassDefFoundError: org/json/JSONObject Intellij 依赖问题 - java.lang.NoClassDefFoundError - Intellij Dependency Issue - java.lang.NoClassDefFoundError
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM