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

    zombiekiller753

    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

    kmecpp

    zombiekiller753 If it wasn't there then i don't see why it would work flawlessly in the Eclipse IDE?
     
  4. 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)
     
  5. Offline

    zombiekiller753

    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
  6. Offline

    Vilsol

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

    fireblast709

    zombiekiller753 When two people shade in the same library, you might have issues referencing classes.
    Hence the relative path.
    Bukkit is software, not a plugin.

    Vilsol that's not the issue here. The issue is that JRE cannot find the classes because they aren't loaded.
     
  8. Offline

    kmecpp

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

    zombiekiller753

    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.
     
  10. 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?
     
  11. 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.
  12. 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.
     
  13. 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
     
  14. Offline

    zombiekiller753

Thread Status:
Not open for further replies.

Share This Page