Solved Class Not Found Exception On Export

Discussion in 'Plugin Development' started by kmecpp, Sep 1, 2014.

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

    kmecpp

    Hi, ive been trying to fix this for the past like 8 hours and have gotten no luck whatsoever. I'm trying to use the Apache NET Commons library but it keeps giving me a class not found exception for the library whenever i export it (When i run the code in Eclipse it works fine) The full error is as follows:

    Code:
    org.bukkit.command.CommandException: Unhandled exception executing command 'stats' in plugin RubberCubes Analytics v0.1
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:192)
        at org.bukkit.craftbukkit.v1_6_R3.CraftServer.dispatchCommand(CraftServer.java:528)
        at org.bukkit.craftbukkit.v1_6_R3.CraftServer.dispatchServerCommand(CraftServer.java:515)
        at net.minecraft.server.v1_6_R3.DedicatedServer.as(DedicatedServer.java:263)
        at net.minecraft.server.v1_6_R3.DedicatedServer.t(DedicatedServer.java:228)
        at net.minecraft.server.v1_6_R3.MinecraftServer.s(MinecraftServer.java:488)
        at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java:421)
        at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:583)
    Caused by: java.lang.NoClassDefFoundError: org/apache/commons/net/ftp/FTPClient
        at me.kmecpp.util.FTPUtil.upload(FTPUtil.java:35)
        at me.kmecpp.util.FileUtil.writeStats(FileUtil.java:195)
        at me.kmecpp.analytics.Stats.collect(Stats.java:45)
        at me.kmecpp.analytics.Commands.onCommand(Commands.java:36)
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
        ... 8 more
    Currently my project looks like this but ive been screwing around with it for a long time
    [​IMG]
    and if you need anymore information just ask.

    If anyone can tell me what i'm doing wrong that would be greatly appreciated, Thanks.
     
  2. Offline

    Deleted user

    kmecpp
    So, let's take a look.

    java.lang.NoClassDefFoundError

    So the JVM couldn't find a class.

    Now, if we examine the JAR file itself, with a program like 7-Zip or such, we'll probably notice that the package org.apache.commons doesn't exist.

    So, because of that, you'll need to shade that jar into your plugin, most likely using Maven, or some alternative.
     
  3. Offline

    fireblast709

    zombiekiller753 Shading libraries is not always a proper solution.
    kmecpp CraftBukkit doesn't know where to find the class files, since it never loaded the jar.
    • Copy the jar in <server root>/lib/<jar name>.jar
    • Add '../lib/<jar name>.jar' to the Class-Path in your manifest file (found in /META-INF/ in the jar)
     
  4. Offline

    Deleted user

    kmecpp
    [​IMG]
    [​IMG]

    Seriously?
    It's imported as a library in Eclipse, but not in your JAR.

    In lolmen's terms, eclipse haz le lib but server no haz.

    fireblast709
    Sure.. but requiring libs to be in a folder called 'libs' in the server folder is a better solution?
    Besides, if he were making a plugin, he'd need it to be dynamic. Will he tell everyone "Your server root MUST be here"

    (Also, why doesn't Bukkit just say "Here are all the libs. Go put them in the libs folder"? They shaded too)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 10, 2016
  5. It appears that you are not using maven. That means Eclipse won't compile dependencies inside the jar. Get maven, setup a project and add dependencies. Put them to scope: compile, and done.
     
  6. Offline

    kmecpp

    errrrr..... so who is right?
     
  7. Offline

    Deleted user

    fireblast709
    I think you can trust Vilsol if he says it's an issue

    Also, if two people shade the same library, how will there be referencing issues? The classes they reference will be the same

    kmecpp
    We're both right. However, personal preference dictates that one of us is more right. We just both think it's ourselves.
     
  8. Offline

    kmecpp

    Vilsol well i decided to go with your way but its kinda confusing... could you plz try and explain it a little better?
     
  9. Offline

    gabizou

    Personally, I'd go with the maven route as fireblast709 and zombiekiller753 has been saying earlier.

    What you'll need to do is:
    1) http://crunchify.com/how-to-convert-existing-java-project-to-maven-in-eclipse/
    2) Add (what seems to be the Apache Commons net library) to your pom in dependencies, alongside the craftbukkit dependency:
    3) Set up Maven Shade Plugin to relocate ApacheCommons to something like "com.myplugin.apache.commons.net"
    http://maven.apache.org/plugins/maven-shade-plugin/examples/class-relocation.html
    4) Use Maven's builder with "mvm clean package" and it will build your jar for you :)
    Examples of plugins already doing this:
    Let me know kmecpp if you still have more questions :)
     
    kmecpp likes this.
  10. Offline

    Ivan

    kmecpp
    Try this. When exporting, select "Runnable Jar File". You need to select a Launch configuration but that part doesn't really matter, just select whatever it'll accept. The important part is that you select "Extract required libraries into generated JAR". Press finish and you're done.
     
  11. Offline

    fireblast709

    zombiekiller753 the suggestion Vilsol provided is pretty much the same as adding the jar like he did just now ;3. Shading is a whole new chapter. gabizou provides a decent method with relocation, so use that if you need to shade the classes in ;3
     
  12. Offline

    Deleted user

Thread Status:
Not open for further replies.

Share This Page