执行 .jar 时将 OJBDC 添加到类路径
Add OJBDC to Classpath when Executing .jar
我正在尝试开发一个 Java 连接到 Oracle 数据库并执行功能的应用程序。如果我 运行 Eclipse 中的应用程序它工作,但是当我尝试 运行 在 Windows 命令提示符中的 .jar 我得到 "Error encountered: java.sql.SQLException: No suitable driver found".
我在命令行执行时传递了Eclipse中Maven Dependencies中显示的所有.jar文件的路径。
步骤
执行"mvn clean install -U"
使用 "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"
从目标目录执行 .jar
- 或者(如下所示)使用 "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"
从目标目录执行 .jar
申请
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
似乎没有依赖项,因此您的神谕 ojdbc
class 未包含在最终的 jar
中。你想要的是一个"fat jar".
方法 - 添加 maven 插件,它将打包应用程序中的所有依赖项 jar
。这可以通过 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>
所以你 运行 mvn package
,你将拥有 jar-with-dependencies
工件,其中将包含所有依赖项。
更新
您的应用无法在运行时发现驱动程序 class,因此您还必须手动加载它。它通常看起来像这样:
Class<?> clazz = Class.forName("oracle.jdbc.driver.OracleDriver");
Driver driver = (Driver) clazz.newInstance();
DriverManager.registerDriver(driver);
我正在尝试开发一个 Java 连接到 Oracle 数据库并执行功能的应用程序。如果我 运行 Eclipse 中的应用程序它工作,但是当我尝试 运行 在 Windows 命令提示符中的 .jar 我得到 "Error encountered: java.sql.SQLException: No suitable driver found".
我在命令行执行时传递了Eclipse中Maven Dependencies中显示的所有.jar文件的路径。
步骤
执行"mvn clean install -U"
使用 "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"
从目标目录执行 .jar
- 或者(如下所示)使用 "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" 从目标目录执行 .jar
申请
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
似乎没有依赖项,因此您的神谕 ojdbc
class 未包含在最终的 jar
中。你想要的是一个"fat jar".
方法 - 添加 maven 插件,它将打包应用程序中的所有依赖项 jar
。这可以通过 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>
所以你 运行 mvn package
,你将拥有 jar-with-dependencies
工件,其中将包含所有依赖项。
更新
您的应用无法在运行时发现驱动程序 class,因此您还必须手动加载它。它通常看起来像这样:
Class<?> clazz = Class.forName("oracle.jdbc.driver.OracleDriver");
Driver driver = (Driver) clazz.newInstance();
DriverManager.registerDriver(driver);