Add jdbc postgre driver to jar

651 Views Asked by At

I'm building a java server program that connects to a psql database with JDBC drivers. I need to compile the program in a .jar file, but when I try to do so and run it, I get this exception. (the program has to run on a linux machine)

java.lang.ClassNotFoundException: org.postgresql.Driver at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:264) at Server.connectToDatabase(Server.java:53) at Server.(Server.java:33) at Main.main(Main.java:10)

Since the program works fine on IntelliJ editor, I assume that the problem is that there isn't the jdbc driver in the jar file. How can I add it?

1

There are 1 best solutions below

0
On

You need add PostgreDriver to your classpath and use command like following.

$Java -classpath postgre-jar+additional-jars-if-any Your-Main-class