英文:
Issue when packaging to jar (postgresql)
问题
代码错误为:找不到适合的驱动程序
链接有效 - 在IntelliJ IDE中运行正常,但作为jar文件时却不起作用。有人在之前的问题中建议添加maven assembly插件。以下是我可能做错的部分,这是构建部分:
<build>
<plugins>
<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>
</configuration>
</plugin>
</plugins>
</build>
这里会出现错误:return DriverManager.getConnection(dbUrl);
链接完全正常 - 我甚至手动创建了一个URL,创建了一个测试类,并在IDE中运行。它编译了,我能够打印出连接对象。然而,一旦我将它变成了一个jar文件(构建工件)
注意:如果有关系的话,数据库是使用Heroku创建和托管的。
它会创建一个无适用驱动程序的错误。我认为这可能是因为我在pom.xml文件中输入了错误内容,所以我重新复制粘贴了插件。
如果需要的话,以下是所有内容(链接也包括在内,因为它只是一个测试数据库)
主类
package main;
import java.sql.DriverManager;
import java.sql.SQLException;
public class test {
public static void main(String[]args) throws SQLException {
System.out.println(DriverManager.getConnection("jdbc:postgresql://ec2-50-17-90-177.compute-1.amazonaws.com/d7mfkbl2si37vm?sslmode=require&user=tzfelalpgpknzs&password=99cce506d410d89c1ee51f2f8b35eae9c6faacd4b578c29ff18b5e5de3cfcf98"));
}
}
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>org.example</groupId>
<artifactId>Database-Test</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<build>
<plugins>
<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>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4-1203-jdbc4</version>
</dependency>
</dependencies>
</project>
英文:
Code errors as: No suitable driver found for
The link works - running it in IntelliJ IDE works fine, but, as a jar file, it doesn't work. Someone suggested, in a previous question, to add maven assembly plugin. I might be doing that wrong, heres the build:
<build>
<plugins>
<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>
</configuration>
</plugin>
</plugins>
</build>
It creates an error here: return DriverManager.getConnection(dbUrl);
The link works perfectly fine - I even created a url by hand, created a test class, and ran that in the IDE. It compiled and I was able to print the connection object. However, once I made it a jar file (build artifacts)
Note: if it matters, the database is made and hosted with Heroku.
it creates a no suitable driver error. I thought this might be because I entered something wrong in my pom.xml file, so I re-copy-pasted the plugin.
If needed, here is everything (link included since its a test database anyways)
MAIN CLASS
package main;
import java.sql.DriverManager;
import java.sql.SQLException;
public class test {
public static void main(String[]args) throws SQLException {
System.out.println(DriverManager.getConnection("jdbc:postgresql://ec2-50-17-90-177.compute-1.amazonaws.com/d7mfkbl2si37vm?sslmode=require&user=tzfelalpgpknzs&password=99cce506d410d89c1ee51f2f8b35eae9c6faacd4b578c29ff18b5e5de3cfcf98"));
}
}
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>org.example</groupId>
<artifactId>Database-Test</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<build>
<plugins>
<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>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4-1203-jdbc4</version>
</dependency>
</dependencies>
</project>
答案1
得分: 0
哈哈,结果证明我太蠢了。
多读了一些内容,决定切换到Maven Shade。再次尝试构建 - 没成功。然后,我找到一个网站,告诉我运行“mvn clean package”。运行时没有清单文件。然后,找到另一个网站,复制粘贴了一些其他内容来声明主类(因为基本上这就是我的清单文件的全部内容)。
英文:
Haha, turns out I'm stupid.
Did a bit more reading, and decided to switch to maven shade. Tried building it again - no luck. Then, I found a website that told me to run "mvn clean package." No manifest file when running. Then, found another website and copy pasted some other stuff to delcare the main class (since that's basically all my manifest did anyways).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论