gpt4 book ai didi

java - 执行.jar时将OJBDC添加到类路径

转载 作者:行者123 更新时间:2023-12-01 17:43:18 28 4
gpt4 key购买 nike

我正在尝试开发一个连接到 Oracle 数据库并执行函数的 Java 应用程序。如果我在 Eclipse 中运行该应用程序,它可以工作,但是当我尝试在 Windows 命令提示符中运行 .jar 时,我收到“遇到错误:java.sql.SQLException:找不到合适的驱动程序”。

当我在命令行中执行时,我将传递 Eclipse 中 Maven 依赖项中显示的所有 .jar 文件的路径。

步骤

  1. 执行“mvn clean install -U”

  2. 使用“java -cp example-1.0-SNAPSHOT.jar;C:/Users/me/.m2/repository/junit/junit/4.11/junit-4.11-sources.jarC”从目标目录执行.jar :/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. 或者(如下所示)使用“java -cp example-1.0-SNAPSHOT.jar;C:/Users/me/.m2/repository/junit/junit/4.11/junit-4.11”从目标目录执行.jar。 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函数.example.App”

申请

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>

修复

从我的 pom.xml 中删除了 ojdbc10 依赖项。从Oracle网站下载了ojdbc8(我似乎无法使用mvnrepository下载它),在命令行中执行时将这些.jar文件手动添加到Eclipse和类路径中。

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

最佳答案

您的jar 似乎没有依赖项,因此您的oracles ojdbc 类未包含在最终的jar 中。你想要的是一个“ fat jar 子”

实现方法 - 添加 Maven 插件,该插件将打包应用 jar 中的所有依赖项。这可以通过 maven-assemble-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>

因此,您运行 mvn package 后,您将拥有 jar-with-dependencies Artifact ,其中将包含所有依赖项。

更新

您的应用程序无法在运行时发现驱动程序类,因此您还必须手动加载它。它通常看起来像这样:

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

关于java - 执行.jar时将OJBDC添加到类路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60909489/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com