2

My project is a maven project and i use JDBC - postgres driver to run a few queries in the java program. I used maven-jar-plugin to get an executable jar on maven build. When i run jar as java -jar xyz.jar, I get

"Exception in thread "main" java.lang.ClassNotFoundException: org.postgresql.Driver"

I checked manifest.mf file and I could see postgres in classpath as below:

Class-Path: postgresql-42.1.4.jar

Please favour on resolution. This same code runs well as Java application. It only creates problem when it is a jar file.

Maven-jar-plugin in pom.xml is as below:

 <dependencies>


    <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.1.4</version>
    </dependency> 

  </dependencies>
    <build>
          <plugins>
             <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.1.0</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>
                                com.app.App
                            </mainClass>
                        </manifest>
                    </archive>
                </configuration>
             </plugin>
          </plugins>
      </build>
Gayathri
  • 1,776
  • 5
  • 23
  • 50
  • Are you running this as a JAR with dependencies packaged into the JAR? If not, where does the Postgres JAR reside? See this: https://stackoverflow.com/questions/18413014/run-a-jar-file-from-the-command-line-and-specify-classpath – Riaan Nel Jul 11 '18 at 12:44
  • it is inside maven dependency..tha's it. i have added pom. – Gayathri Jul 11 '18 at 12:47
  • Is the postgresql-42.1.4.jar in the same folder as your own jar when you try to execute it? If not: that is your problem. – Mark Rotteveel Jul 11 '18 at 12:48
  • is there a way to get all maven dependencies into this same jar ? like..packaging dependencies? – Gayathri Jul 11 '18 at 12:51

1 Answers1

0

it looks like that the postgresql artifact is not packaged into the xyz.jar. in your case, the postgresql jar have to be in the same level as the xyz.jar - xyz.jar - postgresql-42.1.4.jar

and that will works.

In case of the java application, the needed jar's are installed in a lib folder and the application resolved the dependencies from the folder.

muboch
  • 1
  • 1
  • 3
  • is there a way to package postgres into jar , instead of keeping jar explicitly? – Gayathri Jul 11 '18 at 12:56
  • 1
    You can do this using the maven-assembly plugin with the "jar-with-dependencies" descriptor [link] (https://maven.apache.org/plugins/maven-assembly-plugin/) – muboch Jul 11 '18 at 13:04