1

I personally prefer to keep my coding problem off of here unless I think I'm stumped. I'm trying to add the mysql java dependency to my project (I'm coding a Minecraft-Spigot 1.12.2 plugin). I've been coding MySQL for years but I've never had to use it with Java until now so I'm following this tutorial: https://www.spigotmc.org/wiki/connecting-to-databases-mysql/

Which redirected me to this page: https://mvnrepository.com/artifact/mysql/mysql-connector-java/8.0.26 Naturally adding a Maven dep is easy enough you just copy and paste it! Beyond that, I'm not very in tune with the inner workings of the system.

Whenever I compile (it does in-fact compile without throwing any fits) and upload it to my servers I get this error: java.lang.NoClassDefFoundError: com/mysql/cj/jdbc/MysqlDataSource. Now, I know that this has to do with the class being undefined or something. So obviously, I searched the error code. A lot of the problems I saw didn't seem to apply to me.

I double checked and I DO see mysql-connector-java inside of my "External Libraries" section in my IDE (InteliJ IDEA)

I also saw some people sending this link: How can I create an executable JAR with dependencies using Maven? Which, the answer didn't seem to help me out either (unless I missed something).

My hope is that by sending my file someone can help me out here? I've seen some people mention that it could be a problem with a classpath? How exactly can I go about fixing that (if it is the problem). I couldn't seem to find anything to help me with that problem (tho in this case classpath is a little over my head. Def want to read up on it more in the future).

Here's my pom.xml any helpful assistance is appreciated!

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>example</artifactId>
    <version>1.12.2-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>example</name>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.4</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <createDependencyReducedPom>false</createDependencyReducedPom>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>

    <repositories>
        <repository>
            <id>spigotmc-repo</id>
            <url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
        </repository>
        <repository>
            <id>sonatype</id>
            <url>https://oss.sonatype.org/content/groups/public/</url>
        </repository>
        <repository>
            <id>dmulloy2-repo</id>
            <url>https://repo.dmulloy2.net/repository/public/</url>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>org.spigotmc</groupId>
            <artifactId>spigot-api</artifactId>
            <version>1.12.2-R0.1-SNAPSHOT</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.comphenix.protocol</groupId>
            <artifactId>ProtocolLib</artifactId>
            <version>4.6.0</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.25</version>
        </dependency>

    </dependencies>
</project>
Nathan Martin
  • 295
  • 1
  • 3
  • 13
  • @g00se you're saying when I upload to my server? I used `original-example-1.12.2-SNAPSHOT` because I noticed the filesize was significantly smaller (I know it's a dumb reason haha) should I instead use the regular `example-1.12.2-SNAPSHOT`? – Nathan Martin Jul 21 '21 at 23:13
  • 1
    Did you use the right SNAPSHOT jar? Because that should work for you and `jar tf .jar | find "com/mysql/cj/jdbc/MysqlDataSource"` on Windows or `jar tf .jar | grep "com/mysql/cj/jdbc/MysqlDataSource"` on *nix should find that 'missing' class in the jar – g00se Jul 21 '21 at 23:14
  • The more shading, the bigger the snapshot jar ;) – g00se Jul 21 '21 at 23:15
  • @g00se Yeah! So every time I go to reupload to the server I'll delete the original jars (and let the compiler recompile them) I just uploaded the non `original-` jar and it seemed to have worked! – Nathan Martin Jul 21 '21 at 23:17
  • Well you need the latest version - the shaded one when you've worked on it. Then you do `mvn package` to get what you need to upload. I'll post my comment as the answer for you to accept – g00se Jul 21 '21 at 23:19

1 Answers1

1

Did you use the right SNAPSHOT jar? Because that should work for you and

jar tf <the snapshot>.jar | find "com/mysql/cj/jdbc/MysqlDataSource"

on Windows or

jar tf <the snapshot>.jar | grep "com/mysql/cj/jdbc/MysqlDataSource"

on *nix should find that 'missing' class in the jar

g00se
  • 3,207
  • 2
  • 5
  • 9