我试图将HikariCP库用于Java中的PostgreSQL连接池。我正在使用Maven,但出现此错误:java.lang.ClassNotFoundException: org.postgresql.Driver
。
我尝试使用不同版本的PostgreSQL驱动程序,但没有一个对我有利。 (我做了很多事情,但是我遇到了这个问题,我没有注意到)
org.postgresql.ds.PGSimpleDataSource
,org.postgresql.Driver
和com.impossibl.postgres.jdbc.PGDataSource
仍然会产生此错误,即使在HikariCP guide中说使用第一个或第三个也是如此。我从研究中发现了第二个。
(...)
<build>
<defaultGoal>clean package</defaultGoal>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<minimizeJar>true</minimizeJar>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<repositories>
<repository>
<id>spigotmc-repo</id>
<url>https://hub.spigotmc.org/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.12.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>3.3.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.6.4</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.5</version>
</dependency>
</dependencies>
</project>
try {
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.out.println("Failed to load.");
}
HikariConfig config = new HikariConfig("database.properties");
ds = new HikariDataSource(config);
(构造函数HikariDataSource(config);
也会产生此错误)
我相信发生这种情况的原因是未将驱动程序纳入类路径-但是,我尝试过的所有努力似乎都无法做到这一点。驾驶员不在最终罐子中的问题(据我所知): 。
[20:16:35 WARN]: java.lang.ClassNotFoundException: org.postgresql.Driver
[20:16:35 WARN]: at java.net.URLClassLoader.findClass(Unknown Source)
[20:16:35 WARN]: at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:152)
[20:16:35 WARN]: at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:100)
[20:16:35 WARN]: at java.lang.ClassLoader.loadClass(Unknown Source)
[20:16:35 WARN]: at java.lang.ClassLoader.loadClass(Unknown Source)
[20:16:35 WARN]: at java.lang.Class.forName0(Native Method)
[20:16:35 WARN]: at java.lang.Class.forName(Unknown Source)
[20:16:35 WARN]: at me.test.kitpvp.Kitpvp.onEnable(Kitpvp.java:43)
[20:16:35 WARN]: at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:264)
[20:16:35 WARN]: at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:337)
(...)
[20:16:35 INFO]: Failed to load.
[20:16:35 WARN]: 74 [Server thread] INFO com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Starting...
编辑
<scope>compile</scope>
会产生相同的结果-无效。