在过去的3个小时里,我一直在尝试让我的Java程序与Postgres服务器对接。我无法忽略错误消息"找不到适合jdbc:p的驱动程序"ostgresql://localhost:5432/gis".这是一个Bukkit插件,我正在使用IntelliJ IDEA.
代码:
try
{
//Class.forName("org.postgresql.Driver");
c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/gis");
}
catch(Exception e)
{
getLogger().info(e.getMessage());
}
我尝试过的东西:
java -cp ./path/to/postgresjdbc.jar -jar spigot-1.15.2.jar
将jdbc文件内部直接添加到我的jar文件中
在IntelliJ项目中添加jdbc文件作为依赖项
切换到maven,并在pom.xml中放入以下内容:
<dependencies>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.1</version>
</dependency>
</dependencies>
我无法克服我发布的错误。在这一点上,它占据了我整个晚上的时间。
在开发使用MySQL数据库的Bukket/Spigot插件时,我曾多次遇到这个问题。Postgres的流程应该是相同的。
通常,当您的插件找不到PostgresqlJDBC驱动程序时,就会发生此错误。你有两个解决办法:
选项1。将.jar添加到插件的类路径:
建议您在plugins/lib
中设置库,这样您的服务器就不会尝试将库作为Bukkit插件加载。通过在pom.xml
:中添加此配置,将前缀lib/
添加到类路径中
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath> <-- don't know if this is needed -->
<classpathPrefix>lib/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
然后确保将postgresjdbc.jar
放在插件文件夹中名为lib
的文件夹中。
选项2。直接在您的jar中添加依赖项:
请注意,此选项将增加插件的jar大小。
这可以通过Maven的组装插件来完成
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>your.main.class</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
如果你用7-zip这样的文件压缩程序打开插件的jar,你应该有驱动程序类和插件类。
如果这解决了你的问题,请告诉我。