Add OJBDC to Classpath when Executing .jar

jrandj :

I'm trying to develop a Java application that connects to an Oracle database and executes a function. If I run the application in Eclipse it works, but when I try to run the .jar in Windows Command Prompt I get "Error encountered: java.sql.SQLException: No suitable driver found".

I am passing the path of all .jar files shown in Maven Dependencies in Eclipse when I execute in the command line.

Steps

  1. Execute "mvn clean install -U"

  2. Execute .jar from Target directory with "java -cp example-1.0-SNAPSHOT.jar;C:/Users/me/.m2/repository/junit/junit/4.11/junit-4.11-sources.jarC:/Users/me/.m2/repository/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3-sources.jar;C:/Users/me/.m2/repository/com/oracle/ojdbc/ojdbc10/19.3.0.0/ojdbc10-19.3.0.0-sources.jar;C:/Users/me/.m2/repository/com/oracle/ojdbc/ucp/19.3.0.0/ucp-19.3.0.0-sources.jar;C:/Users/me/.m2/repository/com/oracle/ojdbc/oraclepki/19.3.0.0/oraclepki-19.3.0.0-sources.jar;C:/Users/me/.m2/repository/com/oracle/ojdbc/osdt_cert/19.3.0.0/osdt_cert-19.3.0.0-sources.jar;C:/Users/me/.m2/repository/com/oracle/ojdbc/osdt_core/19.3.0.0/osdt_core-19.3.0.0-sources.jar;C:/Users/me/.m2/repository/com/oracle/ojdbc/simplefan/19.3.0.0/simplefan-19.3.0.0-sources.jar;C:/Users/me/.m2/repository/com/oracle/ojdbc/ons/19.3.0.0/ons-19.3.0.0-sources.jar function.example.App"

  3. OR (as suggested below) execute .jar from Target directory with "java -cp example-1.0-SNAPSHOT.jar;C:/Users/me/.m2/repository/junit/junit/4.11/junit-4.11.jar;C:/Users/me/.m2/repository/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar;C:/Users/me/.m2/repository/com/oracle/ojdbc/ojdbc10/19.3.0.0/ojdbc10-19.3.0.0.jar;C:/Users/me/.m2/repository/com/oracle/ojdbc/ucp/19.3.0.0/ucp-19.3.0.0.jar;C:/Users/me/.m2/repository/com/oracle/ojdbc/oraclepki/19.3.0.0/oraclepki-19.3.0.0.jar;C:/Users/me/.m2/repository/com/oracle/ojdbc/osdt_cert/19.3.0.0/osdt_cert-19.3.0.0.jar;C:/Users/me/.m2/repository/com/oracle/ojdbc/osdt_core/19.3.0.0/osdt_core-19.3.0.0.jar;C:/Users/me/.m2/repository/com/oracle/ojdbc/simplefan/19.3.0.0/simplefan-19.3.0.0.jar;C:/Users/me/.m2/repository/com/oracle/ojdbc/ons/19.3.0.0/ons-19.3.0.0.jar function.example.App"

Application

package function.example;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class App {
    public static void main(String[] args) {
        String result = null;
        try {
            result = checkForPalindrome("racecar");
        } catch (SQLException e) {
            System.out.println("Error encountered: " + e);
            e.printStackTrace();
        }
        System.out.println(result);
    }

    public static Connection getConnection() {

        Properties prop = ReadPropertyFile();
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(prop.getProperty("db.URL"), prop.getProperty("db.user"),
                    prop.getProperty("db.password"));
        } catch (SQLException e) {
            System.out.println("Error encountered: " + e);
            e.printStackTrace();
        }
        return conn;
    }

    public static String checkForPalindrome(String word) throws SQLException {
        String sql = "{? = call CHECKFORPALINDROME(?)}";
        try (Connection conn = getConnection(); java.sql.CallableStatement stmt = conn.prepareCall(sql);) {
            stmt.setString(2, word);
            stmt.registerOutParameter(1, java.sql.Types.VARCHAR);
            stmt.execute();
            String stmtResult = stmt.getString(1);
            return stmtResult;
        }
    }

    private static Properties ReadPropertyFile() {
        Properties prop = new Properties();
        try (InputStream input = new FileInputStream("c:\\config.properties")) {
            prop.load(input);
        } catch (FileNotFoundException e) {
            System.out.println("Error encountered: " + e);
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("Error encountered: " + e);
            e.printStackTrace();
        }
        return prop;
    }
}

POM

<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>function</groupId>
    <artifactId>example</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>ODB-function-example-pom</name>
    <url>http://maven.apache.org</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.oracle.ojdbc/ojdbc10 -->
        <dependency>
            <groupId>com.oracle.ojdbc</groupId>
            <artifactId>ojdbc10</artifactId>
            <version>19.3.0.0</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <release>8</release> 
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>function.example.App</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Fix

Removed the ojdbc10 dependency from my pom. Downloaded ojdbc8 from the Oracle website (I couldn't seem to download it using mvnrepository), added these .jar files manually to Eclipse and to the classpath when executing in the Command Line.

java -cp example-1.0-SNAPSHOT.jar;C:\Users\me\Desktop\ojdbc8-full* function.example.App

rxn1d :

It seems like that you have jar without dependencies, so your oracles ojdbc classes are not included in the final jar. What you want to have is a "fat jar".

The way to do it - to add maven plugin which will pack all the dependencies in app jar. This could be achieved with maven-assembly-plugin:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.4.1</version>
    <configuration>
        <!-- get all project dependencies -->
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        <!-- MainClass in mainfest make a executable jar -->
        <archive>
            <manifest>
                <mainClass>function.example.App</mainClass>
            </manifest>
        </archive>
    </configuration>
    <executions>
        <execution>
            <id>make-assembly</id>
            <phase>package</phase> 
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>

So one you ran mvn package, you will have jar-with-dependencies artifact, which will contain all the dependencies.

Update

Your app is unable to discover driver class in the runtime, so you also have to load it manually. It usually looks something like that:

Class<?> clazz = Class.forName("oracle.jdbc.driver.OracleDriver");
Driver driver = (Driver) clazz.newInstance();
DriverManager.registerDriver(driver);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=355754&siteId=1