0

I have been struggling generating an executable jar file with Maven. I included a separate sqljdbc.jar file and put it in a /lib folder and the project is bug-free and runnable in either Eclipse or IntelliJ.

However when using Maven, I generated the jar file with:

mvn clean package

and ran it with the following code:

java -jar target/etl-aggregation-0.0.1-SNAPSHOT.jar

and got the following message:

ClassNotFoundException e : java.lang.ClassNotFoundException:
com.microsoft.sqlserver.jdbc.SQLServerDriver null

I have tried several methods like How can I create an executable JAR with dependencies using Maven? and still couldn't solve the problem.

This is the build part of my pom.xml file:

<build>
    <resources>
        <resource>
            <directory>${project.basedir}</directory>
            <includes>
                <include>lib/*.jar</include>
            </includes>
        </resource>
    </resources>
    <plugins>
        <!-- download source code in Eclipse, best practice and i don't know its usage here..-->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-eclipse-plugin</artifactId>
            <version>2.9</version>
            <configuration>
                <downloadSources>true</downloadSources>
                <downloadJavadocs>false</downloadJavadocs>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
        </plugin>

        <!-- Set a compiler level -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <mainClass>com.etl_aggregation.app.App</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>

        <!-- Maven Assembly Plugin -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.4.1</version>
            <configuration>
                <!-- get all project dependencies -->
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <!-- MainClass in mainfest make a executable jar -->
                <archive>
                    <manifest>
                        <mainClass>com.etl_aggregation.app.App</mainClass>
                    </manifest>
                </archive>

            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <!-- bind to the packaging phase -->
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

problem solved

I manually added -cp lib/sqljdbc4.jar to run the jar and it worked.

Owen Pauling
  • 11,349
  • 20
  • 53
  • 64
  • suppose when the jar file is packaged it does not contains the `com.microsoft.sqlserver.jdbc.SQLServerDriver`. you need to put the `sqlserver.jdbc` jar file to the `.m2` and re package the jar file as maven fetches jar files from local repository first. – Rajith Pemabandu Jul 11 '17 at 00:29
  • You can also create an answer to your own question and accept it. – Gerold Broser Jul 11 '17 at 04:20

1 Answers1

0

Previously what I thought to make it work is to execute the following command, which, in this case I thought I added the sqljdbc4.jar dependency to class path by hand...

java -cp lib/sqljdbc4.jar -jar target/etl-aggregation-0.0.1-SNAPSHOT.jar

but actually what happened is Run a JAR file from the command line and specify classpath and the reason that the program ran is that I also manually added the sqljdbc4.jar dependency in my .m2 repository

    mvn install:install-file -Dfile=lib/sqljdbc4.jar -DgroupId=com.microsoft.sqlserver \
    -DartifactId=sqljdbc4 -Dversion=4.0 -Dpackaging=jar

so that the program just found it as @Rajith Pemabandu suggested.