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

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

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>

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

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

还有主类部分:

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 主机和数据库

类未找到异常 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

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;
         xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
         xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;

    &lt;groupId&gt;org.example&lt;/groupId&gt;
    &lt;artifactId&gt;Database-Test&lt;/artifactId&gt;
    &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
    &lt;packaging&gt;jar&lt;/packaging&gt;
    &lt;build&gt;
        &lt;plugins&gt;
            &lt;plugin&gt;
                &lt;artifactId&gt;maven-assembly-plugin&lt;/artifactId&gt;
                &lt;executions&gt;
                    &lt;execution&gt;
                        &lt;phase&gt;package&lt;/phase&gt;
                        &lt;goals&gt;
                            &lt;goal&gt;single&lt;/goal&gt;
                        &lt;/goals&gt;
                    &lt;/execution&gt;
                &lt;/executions&gt;
                &lt;configuration&gt;
                    &lt;descriptorRefs&gt;
                        &lt;descriptorRef&gt;jar-with-dependencies&lt;/descriptorRef&gt;
                    &lt;/descriptorRefs&gt;
                &lt;/configuration&gt;
            &lt;/plugin&gt;
        &lt;/plugins&gt;
    &lt;/build&gt;
    &lt;dependencies&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.postgresql&lt;/groupId&gt;
            &lt;artifactId&gt;postgresql&lt;/artifactId&gt;
            &lt;version&gt;9.4-1203-jdbc4&lt;/version&gt;
        &lt;/dependency&gt;
    &lt;/dependencies&gt;
&lt;/project&gt;

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

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(&quot;org.postgresql.Driver&quot;);
        //String dbUrl = System.getenv(&quot;DATABASE_URL&quot;);
        String dbUrl = &quot;postgres://fxeymwfokhsimv:bd5c9975533c20ed3ab4df7c0ea263911207854c2cac46983fafce18689a1161@ec2-184-72-162-198.compute-1.amazonaws.com:5432/dlfvrduvj5qcr&quot;;
        return DriverManager.getConnection(dbUrl);
    }
}

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:

确定