1

I'm using the Intellij Idea development environment. To build my project, I use maven in which I connect the postgres driver.

<dependency>
      <groupId>postgresql</groupId>
      <artifactId>postgresql</artifactId>
      <version>9.1-901-1.jdbc4</version>
</dependency>

When I run the project in Intellij idea everything works. However, when I compile a project in Jar, the following error is returned:

java.lang.ClassNotFoundException: org.postgresql.Driver

This is my full pom.xml file:

<?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>Monitoring</groupId>
    <artifactId>Monitoring</artifactId>
    <version>1.0-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.0.2</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>Monitor</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <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>
        </plugins>
    </build>


    <dependencies>
        <dependency>
            <groupId>org.asteriskjava</groupId>
            <artifactId>asterisk-java</artifactId>
            <version>1.0.0-final</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.6</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.25</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.1</version>
        </dependency>
        <dependency>
            <groupId>com.rabbitmq</groupId>
            <artifactId>amqp-client</artifactId>
            <version>4.2.0</version>
        </dependency>
        <dependency>
            <groupId>postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.1-901-1.jdbc4</version>
        </dependency>
    </dependencies>
</project>

How is it possible to solve this problem?

1 Answers1

0

With your pom.xml the following should work

  1. create the application

    mvn package
    
  2. run your application

    cd target/
    java -jar Monitoring-1.0-SNAPSHOT.jar
    

assuming Monitor.java as

public class Monitor {
    public static void main(String[] args) throws Exception {
        Class.forName("org.postgresql.Driver");
        System.out.println("Hello driver");
    }
}

the steps on top will print

Hello driver

To run your application all the jar files from target/*.jar must be in the same directory as the Monitoring-1.0-SNAPSHOT.jar.

SubOptimal
  • 22,518
  • 3
  • 53
  • 69
  • Thank you very much! And why build from Intellij idea does not work out? – Олег Корытников Aug 10 '17 at 09:17
  • @ОлегКорытников Depends on what you did. `Build -> Build Project` only compiles the classes. To build the package you need to execute the Maven goal `package` (means `mvn package`). To do the same in IntelliJ go `View -> Tools Windows -> Maven Projects` then select `Monitoring -> Lifecycle -> package` and execute it. Then all the jars will be in `target/`. – SubOptimal Aug 10 '17 at 12:54