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?
Class.forNamecall, does the DB connection work?forName()catches the IntelliJ's jdbc driver and the connection works. On Heroku,forName()triggers theClassNotFoundExceptionheroku run ls target/dependency/?ls: cannot access target/dependency/: No such file or directorygit pushormvn heroku:deploy?