Dika
Dika

Reputation: 2442

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.

enter image description here

my Main.java source:

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:

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:

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

My 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.

enter image description here

Then lastly, on Intellij I clicked Build -> Build Artifacts -> replicator.jar -> Build. And my executable jar was created successfully.

But when I tried to run it, error happens. here is the screenshot.

enter image description here

please help.. I know this question already answered here: Java: NoClassDefFoundError: org/json/JSONException but the solution cannot solve my problem. maybe this is a bug from 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. I tried NetBeans 8.2 IDE and I successfully created executable bat with gradle plugin & cygwin.

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.

Upvotes: 4

Views: 6876

Answers (5)

Enzo
Enzo

Reputation: 41

My solution to this issue using Intellij was to create a library folder/directory "lib" and I included the JSON jar file.

File > Project Structure > Artifacts

enter image description here

Upvotes: 0

Faouzi Mohamed
Faouzi Mohamed

Reputation: 431

In order to resolve the issue, here what I've done :

Remove Some IDE files/folders

I deleted the .idea folder and the .iml file containing the project name. Files and folder to delete

Reset Intellij Cash and restart Intellij

Reload Maven project

Reload maven project

That how I solve most of my Intellij IDEA Issues

Upvotes: 0

shubham gorde
shubham gorde

Reputation: 175

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.

Now clean, build and run the project

Note: You can change the version of above package

Upvotes: 1

Dushmantha
Dushmantha

Reputation: 3041

Open your replicator.jar (can use WinRAR) and see whether the class JSONException is available inside the jar. If not you need to include that inside your 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.

This post would help you Including dependencies in a jar with Maven

Upvotes: 0

Santhosh
Santhosh

Reputation: 23

Place the jar file under lib folder because while runtime it is not able to identify your jar I think so..

Upvotes: 0

Related Questions