英文:
Class Not Found Exception Postgresql. Already imported maven dependency in pom
问题
错误为:java: 未报告的异常 java.lang.ClassNotFoundException;必须捕获或声明为可抛出
,并出现在此处:Class.forName("org.postgresql.Driver");
大多数情况下,这是因为奇怪的Maven格式或类似的问题,但我认为我已经弄对了(复制粘贴)。
以下是 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>
如果需要,这是文件路径:
还有主类部分:
import java.net.URI;
import java.net.URISyntaxException;
import java.sql.*;
public class DBTEST{
public static void main (String[]args) throws SQLException, URISyntaxException{
System.out.println(getConnection());
}
private static Connection getConnection() throws URISyntaxException, SQLException{
Class.forName("org.postgresql.Driver");
//String dbUrl = System.getenv("DATABASE_URL");
String dbUrl = "postgres://fxeymwfokhsimv:bd5c9975533c20ed3ab4df7c0ea263911207854c2cac46983fafce18689a1161@ec2-184-72-162-198.compute-1.amazonaws.com:5432/dlfvrduvj5qcr";
return DriverManager.getConnection(dbUrl);
}
}
DB 主机和数据库:
不要担心我分享我的数据库凭据 - 这只是一个测试项目,只是为了让我弄清楚如何使用它。
英文:
Error is: java: unreported exception java.lang.ClassNotFoundException; must be caught or declared to be thrown
and occurs at this: Class.forName("org.postgresql.Driver");
Most of the time, it was weird maven formatting or the such, but I think I have it right (copy pasted).
Here is the 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>
If needed, here is the file path
and the main class
import java.net.URI;
import java.net.URISyntaxException;
import java.sql.*;
public class DBTEST{
public static void main (String[]args) throws SQLException, URISyntaxException{
System.out.println(getConnection());
}
private static Connection getConnection() throws URISyntaxException, SQLException{
Class.forName("org.postgresql.Driver");
//String dbUrl = System.getenv("DATABASE_URL");
String dbUrl = "postgres://fxeymwfokhsimv:bd5c9975533c20ed3ab4df7c0ea263911207854c2cac46983fafce18689a1161@ec2-184-72-162-198.compute-1.amazonaws.com:5432/dlfvrduvj5qcr";
return DriverManager.getConnection(dbUrl);
}
}
DB Host and Database
Don't worry about me sharing my database credentials - this is a test project and is just for me to figure out how to use it.
答案1
得分: 0
Maven默认情况下不会将依赖项与jar一起打包,您必须使用插件来完成。请查看此帖子 - https://stackoverflow.com/questions/1729054/including-dependencies-in-a-jar-with-maven
由于您正在使用jdbc 4驱动程序,不需要使用Class.forName
。
程序中的错误是由于未处理的ClassNotFoundException
(使用Class.forName
)和无效的驱动程序URL引起的。
英文:
Maven won't package dependencies with jar by default, you have to do it with the plugin.
Check this thread - https://stackoverflow.com/questions/1729054/including-dependencies-in-a-jar-with-maven
You don't need Class.forName
as you are using jdbc 4 driver.
The error in your program is due to unhandled ClassNotFoundException
for using Class.forName
and invalid driver URL.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论