0

My application(java maven) works fine when I run it from within netbeans but when I extract its jar file it shows me this error : java.lang.classnotfoundexception: com.mysql.jc.jdbc.driver or this no suitable driver found for jdbc:mysql:/localhost

in my pom.xml file i have add my sql driver to dependency

<?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>NIS</groupId>
    <artifactId>NIS</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <repositories>
        <repository>
            <id>unknown-jars-temp-repo</id>
            <name>A temporary repository created by NetBeans for libraries and jars it could not identify. Please replace the dependencies in this repository with correct ones and delete this repository.</name>
            <url>file:${project.basedir}/lib</url>
        </repository>
    </repositories>
    
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <exec.mainClass>NIS.NIS</exec.mainClass>
        <maven.compiler.release>17</maven.compiler.release>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <version>8.0.33</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>unknown.binary</groupId>
            <artifactId>AbsoluteLayout</artifactId>
            <version>SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>unknown.binary</groupId>
            <artifactId>jcalendar-1.4</artifactId>
            <version>SNAPSHOT</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin> 
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.1.0</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>NIS.NIS</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
  • You've got no Maven Shade (or similar) there so you'll have no fat jar. Without a fat jar, you will have to include the driver jar in the classpath if you want to to run your jar – g00se Jul 01 '23 at 12:23
  • 1
    I'm sorry but i didn't understand you – mohamed boulaoudja Jul 01 '23 at 13:03
  • @mohamedboulaoudja see https://stackoverflow.com/questions/75083820/maven-exported-jar-doesnt-find-maven-dependency-although-i-can-run-the-main-clas – Rob Spoor Jul 01 '23 at 14:58

1 Answers1

0

Your jar is missing the MySQL dependency. To make a jar with all of your project's dependecies you can use the Maven Assembly plugin. Add this to your pom.xml file:

<build>
    <finalName>my-project-name</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>3.6.0</version>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Then build the jar using: mvn clean package

This post explains it nicely: jenkov.com/tutorials/maven/maven-build-fat-jar.html