类未找到异常 Postgresql。已在 pom 中导入 maven 依赖。

huangapple go评论136阅读模式
英文:

Class Not Found Exception Postgresql. Already imported maven dependency in pom

问题

错误为:java: 未报告的异常 java.lang.ClassNotFoundException;必须捕获或声明为可抛出,并出现在此处:Class.forName("org.postgresql.Driver");

大多数情况下,这是因为奇怪的Maven格式或类似的问题,但我认为我已经弄对了(复制粘贴)。
以下是 pom.xml 部分内容:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>org.example</groupId>
  7. <artifactId>Database-Test</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <packaging>jar</packaging>
  10. <build>
  11. <plugins>
  12. <plugin>
  13. <artifactId>maven-assembly-plugin</artifactId>
  14. <executions>
  15. <execution>
  16. <phase>package</phase>
  17. <goals>
  18. <goal>single</goal>
  19. </goals>
  20. </execution>
  21. </executions>
  22. <configuration>
  23. <descriptorRefs>
  24. <descriptorRef>jar-with-dependencies</descriptorRef>
  25. </descriptorRefs>
  26. </configuration>
  27. </plugin>
  28. </plugins>
  29. </build>
  30. <dependencies>
  31. <dependency>
  32. <groupId>org.postgresql</groupId>
  33. <artifactId>postgresql</artifactId>
  34. <version>9.4-1203-jdbc4</version>
  35. </dependency>
  36. </dependencies>
  37. </project>

如果需要,这是文件路径:

类未找到异常 Postgresql。已在 pom 中导入 maven 依赖。

还有主类部分:

  1. import java.net.URI;
  2. import java.net.URISyntaxException;
  3. import java.sql.*;
  4. public class DBTEST{
  5. public static void main (String[]args) throws SQLException, URISyntaxException{
  6. System.out.println(getConnection());
  7. }
  8. private static Connection getConnection() throws URISyntaxException, SQLException{
  9. Class.forName("org.postgresql.Driver");
  10. //String dbUrl = System.getenv("DATABASE_URL");
  11. String dbUrl = "postgres://fxeymwfokhsimv:bd5c9975533c20ed3ab4df7c0ea263911207854c2cac46983fafce18689a1161@ec2-184-72-162-198.compute-1.amazonaws.com:5432/dlfvrduvj5qcr";
  12. return DriverManager.getConnection(dbUrl);
  13. }
  14. }

DB 主机和数据库

类未找到异常 Postgresql。已在 pom 中导入 maven 依赖。

不要担心我分享我的数据库凭据 - 这只是一个测试项目,只是为了让我弄清楚如何使用它。

英文:

Error is: java: unreported exception java.lang.ClassNotFoundException; must be caught or declared to be thrown and occurs at this: Class.forName(&quot;org.postgresql.Driver&quot;);

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

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  2. &lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;
  3. xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
  4. xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
  5. &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  6. &lt;groupId&gt;org.example&lt;/groupId&gt;
  7. &lt;artifactId&gt;Database-Test&lt;/artifactId&gt;
  8. &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
  9. &lt;packaging&gt;jar&lt;/packaging&gt;
  10. &lt;build&gt;
  11. &lt;plugins&gt;
  12. &lt;plugin&gt;
  13. &lt;artifactId&gt;maven-assembly-plugin&lt;/artifactId&gt;
  14. &lt;executions&gt;
  15. &lt;execution&gt;
  16. &lt;phase&gt;package&lt;/phase&gt;
  17. &lt;goals&gt;
  18. &lt;goal&gt;single&lt;/goal&gt;
  19. &lt;/goals&gt;
  20. &lt;/execution&gt;
  21. &lt;/executions&gt;
  22. &lt;configuration&gt;
  23. &lt;descriptorRefs&gt;
  24. &lt;descriptorRef&gt;jar-with-dependencies&lt;/descriptorRef&gt;
  25. &lt;/descriptorRefs&gt;
  26. &lt;/configuration&gt;
  27. &lt;/plugin&gt;
  28. &lt;/plugins&gt;
  29. &lt;/build&gt;
  30. &lt;dependencies&gt;
  31. &lt;dependency&gt;
  32. &lt;groupId&gt;org.postgresql&lt;/groupId&gt;
  33. &lt;artifactId&gt;postgresql&lt;/artifactId&gt;
  34. &lt;version&gt;9.4-1203-jdbc4&lt;/version&gt;
  35. &lt;/dependency&gt;
  36. &lt;/dependencies&gt;
  37. &lt;/project&gt;

If needed, here is the file path
类未找到异常 Postgresql。已在 pom 中导入 maven 依赖。

and the main class

  1. import java.net.URI;
  2. import java.net.URISyntaxException;
  3. import java.sql.*;
  4. public class DBTEST{
  5. public static void main (String[]args) throws SQLException, URISyntaxException{
  6. System.out.println(getConnection());
  7. }
  8. private static Connection getConnection() throws URISyntaxException, SQLException{
  9. Class.forName(&quot;org.postgresql.Driver&quot;);
  10. //String dbUrl = System.getenv(&quot;DATABASE_URL&quot;);
  11. String dbUrl = &quot;postgres://fxeymwfokhsimv:bd5c9975533c20ed3ab4df7c0ea263911207854c2cac46983fafce18689a1161@ec2-184-72-162-198.compute-1.amazonaws.com:5432/dlfvrduvj5qcr&quot;;
  12. return DriverManager.getConnection(dbUrl);
  13. }
  14. }

DB Host and Database类未找到异常 Postgresql。已在 pom 中导入 maven 依赖。
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.

huangapple
  • 本文由 发表于 2020年9月9日 00:50:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/63798321.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定