No suitable driver found for jdbc:postgresql - executable java jar - java

When I run the project as a java application in eclipse it works perfectly but running a java -jar on windows cmd throws the SQL exception (No suitable driver found)
I have used maven(maven-assembly) to create the executable jar, a jar-with-dependencies is created in project\target.
The postgresql jar (postgresql-42.2.5.jar) exists in the project\lib folder.
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.project.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
After running mvn clean package :
A manifest file (project\src\main\resources\MANIFEST.mf) is copied into project\target\classes folder. The manifest file contains Class-Path: lib/postgresql-42.2.5.jar.
I have added the dependency in my pom.xml as well.
<dependencies>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>${postgres.driver.version}</version>
</dependency>
</dependencies>
I was hoping that the Class-Path in manifest file would take care of this issue but it did not.
Might be a duplicate post but none of the existing solutions fixed my problem.

Related

How to run a jar with lib?

I have this jar file 'domain-directory-configurator-cli-3.1.3000.0.0.0.jar' that doesn't contain dependancies
I have also this file 'domain-directory-configurator-cli-3.1.3000.0.0.0-lib.zip'
I want to run the jar file and my application using the libs.
What is the current syntax?
I tried
java -cp 3.1.3000.0.0.0/domain-directory-configurator-cli-3.1.3000.0.0.0.jar com.imperva.itp.directory.configurator.cli.MainClass
but I keep getting the exception :
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
so how do I refer the jar to work with the lib file?
You can do this by using the maven plugin maven-assembly-plugin
https://maven.apache.org/plugins/maven-assembly-plugin/usage.html
http://tutorials.jenkov.com/maven/maven-build-fat-jar.html
Assuming domain-directory-configurator-cli-3.1.3000.0.0.0-lib.zip does contain a number of jar files, you would have to extract themand list them one by one on the classpath.
More user friendly would be to have an executable jar where the main class is already mentioned in the manifest. Note you can configure the classpath in the manifest as well,so for a uer it could be sufficient to run java -jar domain-directory-configurator-cli-3.1.3000.0.0.0.jar and both the main class and the classpath are taken from the manifest.
In case you are using Maven, the below combination can help. It downloads all dependencies into the lib directory and configures both the main class and the classpath into the project's jar file.
<project>
<build>
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<manifest>
<addDefaultEntries>true</addDefaultEntries>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.imperva.itp.directory.configurator.cli.MainClass</mainClass>
</manifest>
<manifestEntries>
<Build-Date>$(enf.BUILD_DATE)</Build-Date>
<Build-Date-Formatted>$(enf.BUILD_DATE_FORMATTED)</Build-Date-Formatted>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Finally, when distributing your application do not forget to deliver not only your jar but also the lib directory with content.

No suitable driver found for jdbc:postgresql://localhost:5432/gis

I've spent the last 3 hours trying to get my Java program to interface with my Postgres server. I cannot get past the error message "No suitable driver found for jdbc:postgresql://localhost:5432/gis". It is a Bukkit plugin, and I am using IntelliJ IDEA.
The code:
try
{
//Class.forName("org.postgresql.Driver");
c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/gis");
}
catch(Exception e)
{
getLogger().info(e.getMessage());
}
Things I have tried:
java -cp ./path/to/postgresjdbc.jar -jar spigot-1.15.2.jar
adding the jdbc file internals directly to my jar file
adding the jdbc file as a dependency within the IntelliJ project
switching to maven, and putting the following in pom.xml:
<dependencies>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.1</version>
</dependency>
</dependencies>
I am unable to get past the error I posted. At this point, it has taken over my entire evening.
I've stumbled with this issue several times when developing Bukkit/Spigot plugins that make use of MySQL databases. The process for Postgres should be the same.
Usually, this error happens when your plugin can't find the PostgresqlJDBC driver. You have two workarounds:
Option 1. Adding the .jar to the plugin's classpath:
It's recommended that you set the libraries inside plugins/lib as then your server won't try to load the libraries as a Bukkit plugin. Add the prefix lib/ to your classpath by adding this configuration in your pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath> <-- don't know if this is needed -->
<classpathPrefix>lib/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
Then make sure to put your postgresjdbc.jar inside a folder called lib inside your plugin's folder.
Option 2. Add dependencies directly in your jar:
Note that this option will increase your plugin's jar size.
This can be done via Maven's assembly plugin:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>your.main.class</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
If you open your plugin's jar with a file compressor like 7-zip you should there should the driver's classes in it apart from your plugin ones.
Let me know if this solved your issue.

Bundle local JAR dependency within the target JAR

My project structure is something like this.
ProjectX - depends on ProjectY which is a local JAR, added as a dependency like this:
<dependency>
<groupId>com.wow.projecty</groupId>
<artifactId>projecty</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>/Users/somepath/ProjectY.jar</systemPath>
</dependency>
Now, I'm creating a JAR for ProjectX with all the dependencies bundled in the JAR using this.
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>ProjectXDriver</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
This is bundling all the dependencies from Maven but not the ones from local filesystem. In this case, classes from ProjectY are missing from the final JAR. (Also checked using jar tf)
What am I missing?
Quick fix I found for this. Just install the JAR in the local maven repository and use it normally (without system scope / systemPath.
mvn install:install-file -Dfile=ProjectY.jar -DpomFile=../pom.xml

Create Executable Jar with Maven (maven-assembly-plugin) Not Working vs. Eclipse Export Function

I am trying to create a runnable jar using maven (maven-assembly-plugin) which includes all required libraries in a generated jar file, maven successfully creates .jar file however when executing it seems some libraries are not being correctly loaded (specifically it is trying to load com.ibm.mq.jms classes it throws a DetailedJMSException: JMSCC0091), however when exported in eclipse as a runnable jar it successfully creates a runnable jar and when executed no exceptions are thrown.
Just wondering how I can use maven to do the same as the eclipse export runnable jar function without the exception?
Note: I have also tried a similar approach using maven-shade-plugin with the same exception thrown.
Here is the relevant build information in my pom.xml:
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4.1</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<groupId>org.apache.maven.plugins</groupId>
<executions>
<execution>
<id>make-executable-jar-with-dependencies</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.tapcons.execute.Execute</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
seeing other questions related to this you may should remove ur plugin from the pluginmanagement.
see here:
maven assembly plugin is not working with pluginManagement

log4j class not found in generated jar

I generate jar file with maven-assembly-plugin plugin. I use java -jar to execute jar. I got error message:
log4j: WARN JmDNS or serviceInfo not found
I tried to use path to jar in -classpath, but got same error.
Plugin configuration:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<archive>
<manifest>
<mainClass>test.LeanFTest</mainClass>
</manifest>
</archive>
<finalName>${project.artifactId}-fatjar-${project.version}</finalName>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>src/main/assembly/leanft-assembly.xml</descriptor>
</descriptors>
</configuration>
</plugin>
Most likely, this boils down to:
Your maven build does not include dependent artefacts into your JAR file. In other words: the JAR you create does not include the logj4 JARs. You can change that with your maven config, see here for details.
As your JAR doesn't contain the dependencies, all JARs you depend on must be in your classpath. Meaning: when you run your new JAR on the command line, all elements that might be required for running it must be present on the classpath.

Categories