Gson

Discussion in 'Spigot Plugin Development' started by Stevie212, May 22, 2017.

  1. Hello everyone. I realised something about json. When I run a 1.8 server and I use Gson it gives me this error
    java.lang.NoClassDefFoundError: com/google/gson/Gson

    I tried everything to fix this problem and I still have not solved it. If anyone is having this problem can you show me how to fix it. Thanks

    Also this gson works for all version except 1.8
     
  2. Did you add the external jar to your classpath and make sure it is in the correct folder?
     

  3. Yes I did
     
  4. What's the name of the jar you are using in your classpath, are you positive it is the right one? You aren't doing something right, and it's probably in the classpath.
     
  5. What is your run command (the start.sh or start.bat or whatever file)?

    You should also have some check on plugin load like:
    Code (Text):
    // Yet also not checked
    try {
      Class.forName("com.google.gson.Gson");
      getLogger().info("Found Gson!");
    } catch (Exception e) {
      getLogger().info("Didn't find Gson! Disabling!");
      getServer().getPluginManager().disablePlugin(this);
      return;
    }
     
  6. My recommendation would be to skip all this, and simply shade gson into your jar. Gson is built into spigot, but it appears it's not built into your version. Which implementation of spigot are you using? Open the jar file in winRAR or something, and look to see if com/google/gson exists inside the jar. If not than the JAR did not have it's dependencies shaded into it properly.

    Google gson should come with every version of spigot in recent memory.
     
  7. In 1.8 gson wasn't located in that spot. Its in org/bukkit/craftbukkit/libs/com/google/gson/Gson;

    You need to use abstraction or make wrappers for gson and detect which location to use on plugin load.

    No need to shade gson into your plugin becuase it's already there since 1.8 and up. Just the location was moved.
     
  8. The suggestion of shading was to prevent any version-to-version location conflicts, so it will work consistently across all versions. That way you don't have to use reflection or anything. It's hackish, but it's a lot less of a PITA in my opinion.

    However I must say in my 1.8.8 jar it is, in fact, in the location I said.
    http://i.imgur.com/vHi50cr.png

    I made mine from the build tools.

    Is it just a thing with the 1.8.not8 jars?
     
  9. Class.forName("com.google.gson.Gson");
    Does not work.

    It gives me an error on Class.
     
  10. Put it in a try catch to see if that class exists. Don't you understand that code???