1

I've been trying to deploy my JavaFX application with the Oracle client (ojdbc6) embedded into the same jar.

The application runs fine and connects to the database if I'm running it through IntelliJ, but once I run the "package" task and try and run the application from double clicking the jar or running:

"java -jar .\maven-Newton-1.0-SNAPSHOT.jar"

The application starts, but it won't connect to the DB:

enter image description here`

In the code I've tried both:

Class.forName("oracle.jdbc.driver.OracleDriver");

and

Class.forName("oracle.jdbc.OracleDriver");

I'm just starting with maven and I'm not too sure if my configurations are correct:

• I've tried adding the ojdbc6.jar to the global libraries:

enter image description here

• I've tried adding the ojdbc6.jar file to the SDK classpath:

enter image description here

• And I've messed around with the module dependencies:

enter image description here

But my problem may be lying on the POM file because the other jar that I want to embed within my application is not deploying as well (org.reflections)

• pom.xml

<?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>Newton</groupId>
    <artifactId>maven-Newton</artifactId>
    <version>1.0-SNAPSHOT</version>

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


    <build>
        <plugins>
            <plugin>
                <!-- Build an executable JAR -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>Controller.Main</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}</outputDirectory>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>true</overWriteSnapshots>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.reflections</groupId>
                <artifactId>reflections</artifactId>
                <version>0.9.11</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.6</version>
            </plugin>
            <plugin>
                <!-- https://mvnrepository.com/artifact/oracle/ojdbc6 -->
                <groupId>com.oracle</groupId>
                <artifactId>ojdbc6</artifactId>
                <version>11.2.0</version>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.reflections</groupId>
            <artifactId>reflections</artifactId>
            <version>0.9.11</version>
        </dependency>
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0</version>
        </dependency>
    </dependencies>

</project>

Not too sure where to go from here, any help would be very much appreciated.

Thanks

Gus
  • 403
  • 7
  • 14
  • 1
    Have you checked this discussion for possible ways of adding dependencies to jar: https://stackoverflow.com/questions/10019549/how-to-eclipse-maven-install-build-jar-with-dependencies ? – y.bedrov Nov 13 '17 at 10:32
  • thanks for the suggestion. I went through the examples and tried to use the maven-shade-plugin, but that wasn't quite the problem that I was having. I've updated the pom.xml with the solution. – Gus Nov 13 '17 at 14:18

1 Answers1

2

The problem is that the single jar file, you produce with the 'package' goal does not contain the ojdbc.jar in itself. You will have to run the jar file with a classpath, e.g.

java -cp ojdbc.jar;othernecessary.jar maven-newton-project-1.0.jar

Btw, there is a distinction in maven between dependencies (which are needed for the code to work) and plugins (which are needed for maven to build). You dependency on maven-compiler-plugin and maven-resource-plugin suggests to me you are confusing these two concepts.

I would move these two into the section called <build><plugins> instead alongside the maven-jar-plugin.

If you want an easy to comprehend start, try this: http://www.darrencoxall.com/java/understanding-maven/

Niels Bech Nielsen
  • 4,777
  • 1
  • 21
  • 44
  • Thanks for the info! It was useful to clarify a couple of faulty steps that I had taken. Apparently, I need to have both plugin and dependencies tags. I've updated the pom.xml with the solution – Gus Nov 13 '17 at 16:07