0

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

Vivian Maya
  • 498
  • 1
  • 4
  • 11

3 Answers3

2

All that you need to create your jar executable with your dependencies and launch it as a standalone app:

<build>
  <plugins>
    <plugin>
    <artifactId>maven-assembly-plugin</artifactId>
      <configuration>
        <archive>
          <manifest>
            <mainClass>com.mypackage.main.Main</mainClass>
          </manifest>
        </archive>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
      </configuration>
    </plugin>
  </plugins>
</build>

and then run goal :

clean package assembly:single 
Jileni Bouguima
  • 444
  • 2
  • 6
1

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
StephaneM
  • 4,779
  • 1
  • 16
  • 33
  • god bless you my friend,It worked perfect,exactly perfect !, do I alway need to do ? or is there some weird situation ? – Vivian Maya Jan 07 '15 at 10:37
  • 1
    Well, you need to add this only when you want to include dependencies in your target jar. This is not always the case. Say you have project A depending on Z.jar and project B depending on the same Z.jar. If you have a 3rd project depending on A and B, you don't want A and B to package Z.jar, only C. – StephaneM Jan 07 '15 at 12:13
0

Don't use hyphen in commands, use mvn package and mvn compile

Deepu--Java
  • 3,742
  • 3
  • 19
  • 30