ERROR: java.lang.NoClassDefFoundError: javax/persistence/PersistenceException

Discussion in 'Spigot Plugin Development' started by relucaXIV, Jul 17, 2018.

  1. Code (Text):


    [05:09:49 ERROR]: Error occurred while enabling IslandCraft v1.0.8 (Is it up to date?)
    java.lang.NoClassDefFoundError: javax/persistence/PersistenceException
            at com.github.hoqhuuep.islandcraft.bukkit.IslandCraftPlugin.onEnable(IslandCraftPlugin.java:77) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:264) ~[spigot.jar:git-Spigot-596221b-9a1fc1e]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:337) [spigot.jar:git-Spigot-596221b-9a1fc1e]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:402) [spigot.jar:git-Spigot-596221b-9a1fc1e]
            at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugin(CraftServer.java:374) [spigot.jar:git-Spigot-596221b-9a1fc1e]
            at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugins(CraftServer.java:323) [spigot.jar:git-Spigot-596221b-9a1fc1e]
            at net.minecraft.server.v1_12_R1.DedicatedServer.init(DedicatedServer.java:206) [spigot.jar:git-Spigot-596221b-9a1fc1e]
            at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:544) [spigot.jar:git-Spigot-596221b-9a1fc1e]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_161]
    Caused by: java.lang.ClassNotFoundException: javax.persistence.PersistenceException
            at java.net.URLClassLoader.findClass(Unknown Source) ~[?:1.8.0_161]
            at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:101) ~[spigot.jar:git-Spigot-596221b-9a1fc1e]
            at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:86) ~[spigot.jar:git-Spigot-596221b-9a1fc1e]
            at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_161]
            at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_161]
            ... 9 more

     
    Code (Text):

        @Override
       public void onEnable() {
           ICLogger.logger = new JavaUtilLogger(getLogger());

           // https://github.com/Hidendra/Plugin-Metrics/wiki/Usage
    // Temporarily disabled due to incompatibility with 1.9
    //       try {
    //           final Metrics metrics = new Metrics(this);
    //           metrics.start();
    //       } catch (final Exception e) {
    //           ICLogger.logger.warning("Failed to start MCStats");
    //       }

           saveDefaultConfig();
           FileConfiguration config = getConfig();
           if (!config.contains("config-version") || !config.isString("config-version")) {
               ICLogger.logger.error("No string-value for 'config-version' found in config.yml");
               ICLogger.logger.error("Check for updates at http://dev.bukkit.org/bukkit-plugins/islandcraft/");
               setEnabled(false);
               return;
           }
           final String configVersion = config.getString("config-version");
           if (!configVersion.equals("1.0.0")) {
               ICLogger.logger.error("Incompatible config-version found in config.yml");
               ICLogger.logger.error("Check for updates at http://dev.bukkit.org/bukkit-plugins/islandcraft/");
               setEnabled(false);
               return;
           }

           if (!config.contains("verbose-logging") || !config.isBoolean("verbose-logging")) {
               ICLogger.logger.warning("No boolean-value for 'verbose-logging' found in config.yml");
               ICLogger.logger.warning("Default value 'false' will be used");
           }
           final boolean verboseLogging = config.getBoolean("verbose-logging", false);
           getLogger().setLevel(verboseLogging ? Level.ALL : Level.WARNING);

           final NmsWrapper nms = NmsWrapper.getInstance(getServer());
           if (nms == null) {
               ICLogger.logger.error("IslandCraft does not currently support this CraftBukkit version");
               ICLogger.logger.error("Check for updates at http://dev.bukkit.org/bukkit-plugins/islandcraft/");
               setEnabled(false);
               return;
           }

           IslandDatabase database;
           try {
               final EbeanServer ebeanServer = EbeanServerUtil.build(this);
               database = new EbeanServerIslandDatabase(ebeanServer);
           } catch (final Exception e) {
               ICLogger.logger.error("Error creating EbeanServer database");
               ICLogger.logger.error("Check for updates at http://dev.bukkit.org/bukkit-plugins/islandcraft/");
               ICLogger.logger.error("Exception message: " + e.getMessage());
               setEnabled(false);
               return;
           }

           try {
               islandCraft = new DefaultIslandCraft();
               final Listener listener = new BiomeGeneratorListener(this, database, nms);
               getServer().getPluginManager().registerEvents(listener, this);
           } catch (final Exception e) {
               ICLogger.logger.error("Error creating or registering BiomeGeneratorListener");
               ICLogger.logger.error("Check for updates at http://dev.bukkit.org/bukkit-plugins/islandcraft/");
               ICLogger.logger.error("Exception message: " + e.getMessage());
               setEnabled(false);
               return;
           }
       }
     

    Code (Text):

    package com.github.hoqhuuep.islandcraft.bukkit;

    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    import java.util.List;

    import javax.persistence.PersistenceException;

    import org.bukkit.configuration.ConfigurationSection;
    import org.bukkit.plugin.java.JavaPlugin;

    import com.avaje.ebean.EbeanServer;
    import com.avaje.ebean.EbeanServerFactory;
    import com.avaje.ebean.config.DataSourceConfig;
    import com.avaje.ebean.config.ServerConfig;
    import com.avaje.ebean.config.dbplatform.SQLitePlatform;
    import com.avaje.ebeaninternal.api.SpiEbeanServer;
    import com.avaje.ebeaninternal.server.ddl.DdlGenerator;
    import com.avaje.ebeaninternal.server.lib.sql.TransactionIsolation;
    import com.github.hoqhuuep.islandcraft.core.ICLogger;

    public class EbeanServerUtil {
        public static EbeanServer build(final IslandCraftPlugin javaPlugin) {
            ICLogger.logger.info("Creating EbeanServer for plugin with name: " + javaPlugin.getDescription().getName());
            final String name = javaPlugin.getDescription().getName();
            final ConfigurationSection configurationSection = javaPlugin.getConfig().getConfigurationSection("database");
            final String driver = configurationSection.getString("driver");
            final String url = replaceDatabaseString(configurationSection.getString("url"), javaPlugin);
            final String username = configurationSection.getString("username");
            final String password = configurationSection.getString("password");
            final int isolationLevel = TransactionIsolation.getLevel(configurationSection.getString("isolation"));
            final List<Class<?>> classes = javaPlugin.getDatabaseClasses();

            final ServerConfig serverConfig = new ServerConfig();
            serverConfig.setDefaultServer(false);
            serverConfig.setRegister(false);
            serverConfig.setClasses(classes);
            serverConfig.setName(name);

            final DataSourceConfig dataSourceConfig = new DataSourceConfig();
            dataSourceConfig.setDriver(driver);
            dataSourceConfig.setUrl(url);
            dataSourceConfig.setUsername(username);
            dataSourceConfig.setPassword(password);
            dataSourceConfig.setIsolationLevel(isolationLevel);

            if (driver.contains("sqlite")) {
                serverConfig.setDatabasePlatform(new SQLitePlatform());
                serverConfig.getDatabasePlatform().getDbDdlSyntax().setIdentity("");
            }
            serverConfig.setDataSourceConfig(dataSourceConfig);

            javaPlugin.getDataFolder().mkdirs();
            final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
            Thread.currentThread().setContextClassLoader(getClassLoader(javaPlugin));
            final EbeanServer ebeanServer = EbeanServerFactory.create(serverConfig);
            Thread.currentThread().setContextClassLoader(classLoader);

            // Hack to ensure database exists
            try {
                ebeanServer.find(classes.get(0)).findRowCount();
            } catch (final PersistenceException e) {
                createDdl(ebeanServer);
            }

            return ebeanServer;
        }

        public static void createDdl(final EbeanServer ebeanServer) {
            final SpiEbeanServer spiEbeanServer = (SpiEbeanServer) ebeanServer;
            final DdlGenerator ddlGenerator = spiEbeanServer.getDdlGenerator();
            ddlGenerator.runScript(false, ddlGenerator.generateCreateDdl());
        }

        public static void dropDdl(final EbeanServer ebeanServer) {
            final SpiEbeanServer spiEbeanServer = (SpiEbeanServer) ebeanServer;
            final DdlGenerator ddlGenerator = spiEbeanServer.getDdlGenerator();
            ddlGenerator.runScript(true, ddlGenerator.generateDropDdl());
        }

        private static String replaceDatabaseString(String input, final JavaPlugin plugin) {
            input = input.replaceAll("\\{DIR\\}", plugin.getDataFolder().getPath().replaceAll("\\\\", "/") + "/");
            input = input.replaceAll("\\{NAME\\}", plugin.getDescription().getName().replaceAll("[^\\w_-]", ""));
            return input;
        }

        private static ClassLoader getClassLoader(final JavaPlugin javaPlugin) {
            try {
                final Method method = JavaPlugin.class.getDeclaredMethod("getClassLoader");
                method.setAccessible(true);
                return (ClassLoader) method.invoke(javaPlugin);
            } catch (SecurityException e) {
                throw new RuntimeException("Failed to retrieve the ClassLoader of the plugin using Reflection", e);
            } catch (NoSuchMethodException e) {
                throw new RuntimeException("Failed to retrieve the ClassLoader of the plugin using Reflection", e);
            } catch (IllegalAccessException e) {
                throw new RuntimeException("Failed to retrieve the ClassLoader of the plugin using Reflection", e);
            } catch (InvocationTargetException e) {
                throw new RuntimeException("Failed to retrieve the ClassLoader of the plugin using Reflection", e);
            }
        }

        private EbeanServerUtil() {
            // Utility class
        }
    }
     

    Some help?

    P.S. This is not my code, I'm trying to compile it. Here the link of the original code:
    https://github.com/hoqhuuep/IslandCraft
     
  2. Are you building com.avaje.ebean into your jar?
     
  3. I added "ebean-2.7.3.jar" with add external jar in build path, recompiled it and restarted the server but nothing changed
     
  4. Some help? Thanks very much
     
  5. you also need to add ebean-2.7.3.jar in your plugins folder
     
  6. I added it but naturally doesn't work: eaben is not a plugin or an api made for minecraft
     
  7. Code (Text):

    [21:51:07 ERROR]: Could not load 'plugins\ebean-2.7.3.jar' in folder 'plugins'
    org.bukkit.plugin.InvalidDescriptionException: Invalid plugin.yml
            at org.bukkit.plugin.java.JavaPluginLoader.getPluginDescription(JavaPluginLoader.java:152) ~[spigot.jar:git-Spigot-596221b-9a1fc1e]
            at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:133) [spigot.jar:git-Spigot-596221b-9a1fc1e]
            at org.bukkit.craftbukkit.v1_12_R1.CraftServer.loadPlugins(CraftServer.java:298) [spigot.jar:git-Spigot-596221b-9a1fc1e]
            at net.minecraft.server.v1_12_R1.DedicatedServer.init(DedicatedServer.java:205) [spigot.jar:git-Spigot-596221b-9a1fc1e]
            at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:544) [spigot.jar:git-Spigot-596221b-9a1fc1e]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_161]
    Caused by: java.io.FileNotFoundException: Jar does not contain plugin.yml
            ... 6 more
    [21:51:07 INFO]: Set PluginClassLoader as parallel capable
    [21:51:07 INFO]: [IslandCraft] Loading IslandCraft v1.0.8
    [21:51:07 INFO]: [Multiverse-Core] Loading Multiverse-Core v2.5.0-b727
    [21:51:07 INFO]: [IslandCraft] Enabling IslandCraft v1.0.8
    [21:51:07 ERROR]: Error occurred while enabling IslandCraft v1.0.8 (Is it up to date?)
    java.lang.NoClassDefFoundError: javax/persistence/PersistenceException
            at com.github.hoqhuuep.islandcraft.bukkit.IslandCraftPlugin.onEnable(IslandCraftPlugin.java:77) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:264) ~[spigot.jar:git-Spigot-596221b-9a1fc1e]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:337) [spigot.jar:git-Spigot-596221b-9a1fc1e]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:402) [spigot.jar:git-Spigot-596221b-9a1fc1e]
            at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugin(CraftServer.java:374) [spigot.jar:git-Spigot-596221b-9a1fc1e]
            at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugins(CraftServer.java:323) [spigot.jar:git-Spigot-596221b-9a1fc1e]
            at net.minecraft.server.v1_12_R1.DedicatedServer.init(DedicatedServer.java:206) [spigot.jar:git-Spigot-596221b-9a1fc1e]
            at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:544) [spigot.jar:git-Spigot-596221b-9a1fc1e]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_161]
    Caused by: java.lang.ClassNotFoundException: javax.persistence.PersistenceException
            at java.net.URLClassLoader.findClass(Unknown Source) ~[?:1.8.0_161]
            at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:101) ~[spigot.jar:git-Spigot-596221b-9a1fc1e]
            at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:86) ~[spigot.jar:git-Spigot-596221b-9a1fc1e]
            at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_161]
            at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_161]
            ... 9 more
     
     
  8. Someone'd have an idea of what is happening?
     
  9. Use maven then and use the goal "shade" , so that jar comes complied into your plugin
     
    • Agree Agree x 1
  10. yea, adding it as a plugin will do nothing. Maven is the best option as that will most likely fix it.

    Edit: Maven allows you to manage external jars, google it :) It's great. A lot of resources don't come with jars but tell you to add it to your maven, it automatically builds the jar for you and adds it to your project.
     
  11. And I need to edit the code then, not only compile it
     
  12. I can edit the code with maven?
     
  13. I only used eclipse up to now
     
  14. This project is using Gradle, it's like maven but different. Make sure the Gradle has ebean in it and your IDE has gradle enabled for this project, if you're using IntelliJ I can tell you how.
     
    • Agree Agree x 1
  15. A Plugin for eclipse and other java IDEs
     
  16. Someone can help me about to installa grandle on eclipse, or if it's not possible, how to compile projects with gramble? Thanks
     
  17. ok, it's 'gradle' not grandle or gramble haha. But sorry I can't help you, I don't use Eclipse & I don't use Gradle ;/ perhaps @SkyHash or somebody else could help you :)