OS name: "linux" version: "2.6.32-27-generic" arch: "i386" Family: "unix"
Apache Maven 2.2.1 (r801777; 2009-08-06 12:16:01-0700)
Java version: 1.6.0_20
I am trying to use the mysql dependency in with maven in ubuntu. If I move the "mysql-connector-java-5.1.14.jar" file that maven downloaded into my $JAVA_HOME/jre/lib/ext/ folder, everything is fine when I run the jar.
I think I should be able to just specify the dependency in the pom.xml file and maven should take care of setting the classpath for the dependency jars automatically. Is this incorrect?
My pom.xml file looks like this:
<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.ion.common</groupId> <artifactId>TestPreparation</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>TestPrep</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <mainClass>com.ion.common.App</mainClass> </manifest> </archive> </configuration> </plugin> </plugins> </build> <dependencies> <!-- JUnit testing dependency --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- MySQL database driver --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.14</version> <scope>compile</scope> </dependency> </dependencies> </project>
The command "mvn package" builds it without any problems, and I can run it, but when the application attempts to access the database, this error is presented:
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver at java.net.URLClassLoader$1.run(URLClassLoader.java:217) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:205) at java.lang.ClassLoader.loadClass(ClassLoader.java:321) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294) at java.lang.ClassLoader.loadClass(ClassLoader.java:266) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:186) at com.ion.common.Functions.databases(Functions.java:107) at com.ion.common.App.main(App.java:31)
The line it is failing on is:
Class.forName("com.mysql.jdbc.Driver");
Can anyone tell me what I'm doing wrong or how to fix it?
Maven does set the classpath to the dependencies correctly, but not prefixed with repository location. It will look like this in your Manifest file. It is upto you to place the dependant jars in the same folder as the jar which you are running.
Raghuram gave me a push in the right direction. The way to get maven to take care of copying the jars automatically is to add this code inside the tag in the pom.xml file:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>copy-dependencies</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}</outputDirectory> <overWriteReleases>false</overWriteReleases> <overWriteSnapshots>true</overWriteSnapshots> </configuration> </execution> </executions> </plugin>
More details on this can be found here: https://maven.apache.org/plugins/maven-dependency-plugin/usage.html
Getting maven to package the jars together would be nice, but this is good enough to answer this question. Related answers on stackoverflow:
Building executable jar with maven?
How can I create an executable JAR with dependencies using Maven?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With