Solved Problem loading libraries with Maven... "java.lang.NoClassDefFoundError: org/json/JSONObject"

Discussion in 'Plugin Development' started by FowD8, Aug 9, 2016.

Thread Status:
Not open for further replies.
  1. Offline

    FowD8

    Hey everybody. So I'm having a bit of trouble just using an external library, in this case, org.json (though it's the same problem for any maven library I've used)... when I run my code I get a NoClassDefFoundError.

    I made a very simple clean example with nothing more than an onEnable and my attempt at using org.json.JSONObject:

    Code:
    package com.testbukkit;
    
    import org.bukkit.plugin.java.JavaPlugin;
    import org.json.JSONArray;
    import org.json.JSONObject;
    
    public class Main extends JavaPlugin {
      public void onEnable() {
        System.out.println("==========================");
    
        String testString = "{var: \"some var\"}";
    
        JSONObject json = new JSONObject(testString);
        JSONArray jsonArray = json.getJSONArray("object");
    
        System.out.println(jsonArray);
    
        System.out.println("==========================");
      }
    
      public void onDisable() {
    
      }
    }
    this is my pom.xml:

    Code:
    <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>com.testbukkit</groupId>
        <artifactId>test-bukkit</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    
        <repositories>
            <repository>
                <id>bukkit-repo</id>
                <url>http://repo.bukkit.org/content/groups/public/</url>
            </repository>
        </repositories>
    
        <dependencies>
            <dependency>
                <groupId>org.json</groupId>
                <artifactId>json</artifactId>
                <version>20160212</version>
            </dependency>
    
            <dependency>
                <groupId>org.bukkit</groupId>
                <artifactId>bukkit</artifactId>
                <version>LATEST</version>
            </dependency>
    
        </dependencies>
    </project>
    this is the full error stack:

    Code:
    [11:06:34 ERROR]: Error occurred while enabling Test-Bukkit-Plugin v1.0 (Is it up to date?)
    java.lang.NoClassDefFoundError: org/json/JSONObject
        at com.testbukkit.Main.onEnable(Main.java:13) ~[?:?]
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:292) ~[craftbukkit-1.10.2.jar:git-Bukkit-0ebb9c7]
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:332) [craftbukkit-1.10.2.jar:git-Bukkit-0ebb9c7]
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:404) [craftbukkit-1.10.2.jar:git-Bukkit-0ebb9c7]
        at org.bukkit.craftbukkit.v1_10_R1.CraftServer.loadPlugin(CraftServer.java:347) [craftbukkit-1.10.2.jar:git-Bukkit-0ebb9c7]
        at org.bukkit.craftbukkit.v1_10_R1.CraftServer.enablePlugins(CraftServer.java:319) [craftbukkit-1.10.2.jar:git-Bukkit-0ebb9c7]
        at org.bukkit.craftbukkit.v1_10_R1.CraftServer.reload(CraftServer.java:725) [craftbukkit-1.10.2.jar:git-Bukkit-0ebb9c7]
        at org.bukkit.Bukkit.reload(Bukkit.java:548) [craftbukkit-1.10.2.jar:git-Bukkit-0ebb9c7]
        at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:25) [craftbukkit-1.10.2.jar:git-Bukkit-0ebb9c7]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140) [craftbukkit-1.10.2.jar:git-Bukkit-0ebb9c7]
        at org.bukkit.craftbukkit.v1_10_R1.CraftServer.dispatchCommand(CraftServer.java:625) [craftbukkit-1.10.2.jar:git-Bukkit-0ebb9c7]
        at org.bukkit.craftbukkit.v1_10_R1.CraftServer.dispatchServerCommand(CraftServer.java:611) [craftbukkit-1.10.2.jar:git-Bukkit-0ebb9c7]
        at net.minecraft.server.v1_10_R1.DedicatedServer.aL(DedicatedServer.java:397) [craftbukkit-1.10.2.jar:git-Bukkit-0ebb9c7]
        at net.minecraft.server.v1_10_R1.DedicatedServer.D(DedicatedServer.java:361) [craftbukkit-1.10.2.jar:git-Bukkit-0ebb9c7]
        at net.minecraft.server.v1_10_R1.MinecraftServer.C(MinecraftServer.java:646) [craftbukkit-1.10.2.jar:git-Bukkit-0ebb9c7]
        at net.minecraft.server.v1_10_R1.MinecraftServer.run(MinecraftServer.java:550) [craftbukkit-1.10.2.jar:git-Bukkit-0ebb9c7]
        at java.lang.Thread.run(Thread.java:745) [?:1.8.0_91]
    Caused by: java.lang.ClassNotFoundException: org.json.JSONObject
        at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[?:1.8.0_91]
        at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:77) ~[craftbukkit-1.10.2.jar:git-Bukkit-0ebb9c7]
        at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:62) ~[craftbukkit-1.10.2.jar:git-Bukkit-0ebb9c7]
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[?:1.8.0_91]
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[?:1.8.0_91]
        ... 17 more

    looks to me like the plugin is having problems loading the org.json libraries, any help in fixing this would be much appreciated :)

    after a bit more reading around, it looks like I need to use maven-shade-plugin... i added this to my pom.xml with still no luck:

    Code:
      <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-shade-plugin</artifactId>
                    <version>2.4.3</version>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>shade</goal>
                            </goals>
                            <configuration>
                                <artifactSet>
                                    <includes>
                                        <include>org.json:json</include>
                                    </includes>
                                </artifactSet>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
     
    Last edited: Aug 9, 2016
  2. If you aren't exporting the library within the jar, it must be on the server.
     
  3. Offline

    FowD8

    that's kind of my question though I guess, how do i export the library within the jar? Because unless I'm mistaken, I am...

    [​IMG]

    "Export generated class files and resources"
     
  4. @FowD8
    Resources aren't external libraries, they are things that aren't classes, such as text files, images etc. What you want to do is shade the json libraries into your jar.
     
  5. Offline

    ArsenArsen

    Here's how, add the maven assembly plugin and set it to do jar with dependencies. Then use mvn clean install instead if eclipse export.

    Also use GSON or json simple. They're allready in the bukkit jars, and org json is generally terrible.
     
  6. Offline

    FowD8

    @ArsenArsen

    I figured it out "using eclipse"... can just right click project > run as > Maven Install

    then just copy the jar file from the /target folder

    thanks, that got me going in the right direction =)

    btw for anybody that wants to know about shading... this is what I added:

    Code:
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-shade-plugin</artifactId>
                    <version>2.4.3</version>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>shade</goal>
                            </goals>
                            <configuration>
                                <artifactSet>
                                    <includes>
                                        <include>org.json:json</include>
                                    </includes>
                                </artifactSet>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    main part being <include>org.json:json</include> which is <include>groupId:artifactId</include>

    ----------------

    I went ahead and updated the thread as "Solved"
     
    Last edited: Aug 9, 2016
Thread Status:
Not open for further replies.

Share This Page