gpt4 book ai didi

java.lang.ClassNotFoundException : org. postgresql.Driver 与 Maven

转载 作者:行者123 更新时间:2023-11-29 13:47:47 36 4
gpt4 key购买 nike

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

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

当我在 Intellij idea 中运行项目时,一切正常。但是,当我编译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>

如何解决这个问题?

最佳答案

使用你的pom.xml,下面的应该可以工作

  1. 创建应用

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

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

假设 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 位于同一目录中。

关于java.lang.ClassNotFoundException : org. postgresql.Driver 与 Maven,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45607801/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com