Issue
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
I followed the Heroku tutorial, but when I saw that there is no connection, I put
Class.forName("org.postgresql.Driver");
before
String dbUrl = System.getenv("JDBC_DATABASE_URL");
return DriverManager.getConnection(dbUrl);
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>
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.
What can be the cause of the driver not loading? Am I forgetting something in pom.xml, or Procfile, or something else?
Solution
Sometimes, convenience (like using IntelliJ's git integration to push to Heroku like I did) leads to unwanted problems.
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.
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) Execute maven goal heroku:deploy
.
That's it
Answered By - kit
Answer Checked By - Gilberto Lyons (JavaFixing Admin)