Bungee can't access local database

Discussion in 'BungeeCord Plugin Development' started by ChistOiii, Oct 18, 2020.

  1. Hi, I'm trying to make a plugin that saves information on a player to a database installed locally on my dedicated server when they connect to a Forge server using cheating mods. The plugin finds the cheating players but can't connect to my MariaDB database. I'm using Eclipse and Maven finds the correct dependencies and jars.

    Code (Text):
     <!-- https://mvnrepository.com/artifact/org.mariadb.jdbc/mariadb-java-client -->
        <dependency>
          <groupId>org.mariadb.jdbc</groupId>
          <artifactId>mariadb-java-client</artifactId>
          <version>2.7.0</version>
          <type>jar</type>
          <scope>compile</scope>
        </dependency>
      </dependencies>
     
      <repositories>
        <repository>
          <id>mariadb-repo</id>
          <url>https://repo1.maven.org/maven2</url>
        </repository>
        <repository>
          <id>bungeecord-repo</id>
          <url>https://oss.sonatype.org/content/repositories/snapshots</url>
        </repository>
      </repositories>
    I tried like so but even without mariadb-repo and without mariadb type and scope tags.

    I get the connection from another class:

    Code (Java):
    public class DBManager {
     
        private static Connection conn = null;
     
        public static Connection getConnection() {
         
            try {
                if(conn == null || conn.isClosed()) {
                    Class.forName("org.mariadb.jdbc.Driver");
                    conn = DriverManager.getConnection("jdbc:mariadb://localhost:3306/DatabaseName", "username", "password");
                }
            } catch (SQLException | ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
         
            return conn;
        }
     
        public static void closeConnection() {
            try {
                if(conn != null && !conn.isClosed()) {
                    conn.close();
                }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
     
    The database operations are done in another class and the methods of that class are then called from inside my commands and listeners.

    The problem is that i get a ClassNotFoundException:

    Code (Text):
    [03:00:33 ERROR]: java.lang.ClassNotFoundException: org.mariadb.jdbc.Driver
    >
    [03:00:33 ERROR]:       at net.md_5.bungee.api.plugin.PluginClassloader.loadClass0(PluginClassloader.java:63)
    >
    [03:00:33 ERROR]:       at net.md_5.bungee.api.plugin.PluginClassloader.loadClass(PluginClassloader.java:37)
    >
    [03:00:33 ERROR]:       at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
    >
    [03:00:33 ERROR]:       at java.lang.Class.forName0(Native Method)
    >
    [03:00:33 ERROR]:       at java.lang.Class.forName(Class.java:264)
    I tried doing this operations creating asynchronous threads using bungee api and without doing so. I've tried different maven builds. Nothing seems to work.

    If i create a test jar that does a database insert operation using the same classes and the same mariadb client jar and launch it in whichever folder inside my server from the console (without using bungee) the class is found and everything works great.

    Maybe Maven doesn't deploy the mariadb so Bungee doesn't find it but I tried doing a compile build and I still get the same error.
     
  2. Do you use the maven shade plugin?
     
    • Like Like x 1
  3. I'm new to Bungee and Maven development so I don't know what that is...could you explain?
     
  4. SteelPhoenix

    Moderator

    The mariadb files are not available at runtime because bungeecord does not provide them and your plugin does not include them. Maven shade is a plugin that allows you to include your dependencies into the final jar file, making all required files present at runtime.
     
    • Like Like x 1
  5. Thank you for your explanation, but there's something I'm not sure about . I saw online that using the shade plugin I have to specify a main class but I don't have a class with a main function. Also, will the entire dependency tree be exported? Can I export only the mariadb jar into my plugin jar?
     
  6. Ok I actually managed to do it with the shade plugin, even without specifying a main class. It only included the mariadb library so everything worked out great. It also works on my server. Thank you again!