Search code examples
javasqlitemavenjfreechart

maven how to include dependencies correctly


I was trying to figuring out how to use maven but I didn't get it.

First I tried sqlite 3.8.7 from :

mvnrepository.com/artifact/org.xerial/sqlite-jdbc/3.8.7

I can compile well but when I try to execute maven doesn't find sqlite jar file, so I try to use :

mvn install:install-file

but It didn't work too, so I just used -cp and I have fixed.

Second I try to use jfreechart from :

http://mvnrepository.com/artifact/jfree/jfreechart/1.0.13

I did same steps below for jfreechart but this time it gives me NoClassDefFoundError.

Both of them works when I compile manually but with maven it's not. What I am missing about it ? If I always add manually like sqlite why should use maven anyway ?

Notes : I compile as :

mvn compile

and I'm packaging as :

mvn package

and finally I try to execute as :

java -cp target/porject.jar org.path.App

Edit :

This is for jfreechart app(pom.xml) I add dependency tags from mvnrepository.com

<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>org.project</groupId>
  <artifactId>ChartTest</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>ChartTest</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>jfree</groupId>
        <artifactId>jfreechart</artifactId>
        <version>1.0.13</version>
    </dependency>
  </dependencies>
</project>

Thanks in Advance


Solution

  • Your problem is you don't include your dependencies in your target jar.

    Add this to your pom.xml:

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.1</version>
                <configuration>
    
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
    
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id> <!-- this is used for inheritance merges -->
                        <phase>package</phase> <!-- append to the packaging phase. -->
                        <goals>
                            <goal>attached</goal> <!-- goals == mojos -->
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    

    Then you can start your app using the command line:

    java -cp target/ChartTest-1.0-SNAPSHOT-jar-with-dependencies.jar org.path.App