java.lang.ClassNotFoundException:带有Maven的org.postgresql.Driver

时间:2017-08-10 08:02:13

标签: java postgresql maven intellij-idea

我正在使用Intellij Idea开发环境。为了构建我的项目,我使用maven来连接postgres驱动程序。

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

当我在Intellij中运行项目时,一切正常。但是,当我在Jar中编译项目时,会返回以下错误:

java.lang.ClassNotFoundException: org.postgresql.Driver

这是我的完整pom.xml文件:

<?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>

如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

使用pom.xml,以下内容应该有效

  1. 创建应用程序

    mvn package
    
  2. 运行您的应用程序

    cd target/
    java -jar Monitoring-1.0-SNAPSHOT.jar
    
  3. 假设Monitor.java

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

    顶部的步骤将打印

    Hello driver
    

    要运行您的应用程序,target/*.jar中的所有jar文件必须与Monitoring-1.0-SNAPSHOT.jar位于同一目录中。