Heroku找不到Postgres JDBC驱动程序 [英] Heroku cannot find Postgres JDBC Driver

查看:71
本文介绍了Heroku找不到Postgres JDBC驱动程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Maven为Heroku编写一个纯Java服务器应用程序.连接到Heroku的Postgres数据库.
使用IntelliJ的运行Java应用程序的配置,将Heroku的DB URL指定为环境变量时,本地运行时,一切正常.该应用程序可以运行,并且我可以连接到数据库.
注意:我发现IntelliJ会以某种方式自动使用其自己的Postgres驱动程序,显然,我在pom.xml中指定的驱动程序会被忽略
但是,当我部署该应用程序时,我在连接到数据库后立即在Heroku日志中收到以下错误:
java.lang.ClassNotFoundException:org.postgresql.Driver

Writing a pure Java server app for Heroku using Maven. Connecting to Heroku's Postgres database.
Everything works when running locally, using IntelliJ's config for running Java apps, specifying Heroku's DB URL as an environment variable. The app works, and I can connect to the DB.
Note: I found out that IntelliJ somehow automatically uses its own Postgres driver, the one that I specified in pom.xml gets ignored, obviously
However, when I deploy the app, i get the following error in my Heroku log as soon as I connect to the DB:
java.lang.ClassNotFoundException: org.postgresql.Driver

我遵循了Heroku教程,但是当我发现没有连接时,我放了

I followed the Heroku tutorial, but when I saw that there is no connection, I put

Class.forName("org.postgresql.Driver");

之前

String dbUrl = System.getenv("JDBC_DATABASE_URL");
return DriverManager.getConnection(dbUrl);

只是为了确保找到驱动程序.如果取消 Class.forName()... 检查,则会得到找不到适合的驱动程序 [我的数据库URL] 错误.br>关于最后一个错误,我尝试了 JDBC_DATABASE_URL DATABASE_URL env vars,甚至使用 sslmode = require 手动构建了数据库URL.等,但仍然没有运气.
我的pom.xml指定了 modelVersion,groupId,artifactId,版本名称,以及:

just to make sure the driver gets found. If I remove Class.forName()... check, I get the no suitable driver found for [my db url] error.
Concerning the last error, I tried both JDBC_DATABASE_URL and DATABASE_URL env vars, and even constructing the DB URL "by hand", with sslmode=require etc, but still no luck.
My pom.xml has modelVersion, groupId, artifactId, version and name specified, as well as:

<dependencies>
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.4.1208</version>
    </dependency>
</dependencies>

Heroku用于启动应用程序的我的Procfile看起来像这样:

My Procfile, that Heroku uses to launch the app, looks like this:

web: java $JAVA_OPTS -cp target/classes:"target/dependency/*" Main --port $PORT

部署应用程序时,没有任何错误,构建成功.

When I deploy the app, I get no errors, build is successful.

驱动程序无法加载的原因可能是什么?我会忘记pom.xml或Procfile中的某些内容吗?

What can be the cause of the driver not loading? Am I forgetting something in pom.xml, or Procfile, or something else?

推荐答案

有时,便利性(例如像我一样使用IntelliJ的git集成来推送到Heroku)会导致不必要的问题.

Sometimes, convenience (like using IntelliJ's git integration to push to Heroku like I did) leads to unwanted problems.

我使用 git push 在Heroku上更新了我的应用程序.一切都很好,只是所需的库没有安装到位.
尝试了 mvn heroku:deploy ,经过测试,一切正常!感谢 codefinger 的建议.

I used git push to update my app on Heroku. Everything was fine, except the required libraries didn't fall into place.
Tried mvn heroku:deploy, tested, everything works! Thanks codefinger for the suggestion.

如果有人问,这是以前从未做过的事情:
1)添加 Heroku Maven插件到您的 pom.xml :

If anyone asks, here's how you do it if never done before:
1) Add the Heroku Maven Plugin to your pom.xml:

    <build>
        <plugins>
            <plugin>
                <groupId>com.heroku.sdk</groupId>
                <artifactId>heroku-maven-plugin</artifactId>
                <version>2.0.6</version>
            </plugin>
        </plugins>
    </build>

2)执行Maven目标 heroku:deploy .
就这样

2) Execute maven goal heroku:deploy.
That's it

这篇关于Heroku找不到Postgres JDBC驱动程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆