0

I have the following pom:

    <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>do_can_proj</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

    <dependencyManagement>
        <dependencies>
            
            <dependency>
                <groupId>com.oracle.database.jdbc</groupId>
                <artifactId>ojdbc11-production</artifactId>
                <version>21.1.0.0</version>
                <type>pom</type>
            </dependency>
            
        </dependencies>

    </dependencyManagement>
    <build>
        <pluginManagement>
            <plugins>

                <plugin>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <version>3.3.0</version>
                    <configuration>
                        <archive>
                            <manifest>
                                <mainClass>com.do.can.Main</mainClass>
                            </manifest>
                        </archive>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                    </configuration>
                    <executions>
                        <execution>
                            <id>make-assembly</id>
                            <phase>package</phase>
                            <goals>
                                <goal>single</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>

            </plugins>
        </pluginManagement>
    </build>
</project>

Running,

$ mvn clean package assembly:single

generates a jar in the target folder.

When I run the jar file from the command line:

java -jar target/do_can_proj-1.0-SNAPSHOT-jar-with-dependencies.jar

I get the following error:

SQL State: 08001
No suitable driver found for jdbc:oracle:thin:@hostname:1521:SID

And this is how I have my project setup:

enter image description here

sotn
  • 1,833
  • 5
  • 35
  • 65
  • Does this answer your question? [How can I create an executable JAR with dependencies using Maven?](https://stackoverflow.com/questions/574594/how-can-i-create-an-executable-jar-with-dependencies-using-maven) – Thorbjørn Ravn Andersen Mar 12 '22 at 12:37
  • No, it did not. I tried a bunch of variations but I still see the error. – sotn Mar 12 '22 at 20:20

1 Answers1

0

When running with the -jar argument, the classpath is set to the entry in the manifest in the jar file. By default this is empty. It can be argued that the assembly target should be improved to allow this.

It needs to be set to include all libraries used by your code for this to work and the libraries needs to be added from the Maven repository. How exactly to do this depends on your needs. See How can I create an executable JAR with dependencies using Maven? for suggestions.

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347