UCanAccess 初始化程序错误(在没有 IDE 的情况下编译/运行) [英] UCanAccess Initializer Error (compile/run without IDE)

查看:24
本文介绍了UCanAccess 初始化程序错误(在没有 IDE 的情况下编译/运行)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试创建一个新项目以使用 UCanAccess 读取 MS Access 文件.我一直在关注来自@Gord Thompson 的信息和 github 中 UCanAccess 的示例文件.排除我使用不同名称这一事实,每件事都是相同的.我不使用任何 GUI IDE.我只是从命令行编译,基本上,我写了一个执行命令行编译的 Java 程序.

I have been trying to create a new project to use UCanAccess to read a MS Access file. I have been following the information from @Gord Thompson and the example file in the github for UCanAccess. Excluding the fact that I am using different names every thing is the same. I do not use any of the GUI IDE's. I just compile from the command line, basically, I wrote a Java program that does command line compiling.

参考文献:

在没有 ODBC 的情况下从 Java 操作 Access 数据库

https://github.com/andrew-nguyen/ucanaccess/blob/master/example/net/ucanaccess/example/Example.java

我的代码示例如下:

String path = new java.io.File(PATH).getAbsolutePath();
db = "jdbc:ucanaccess://" + path;
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
Connection conn = DriverManager.getConnection( db );
Statement s = conn.createStatement();

我的错误信息是:

Exception in thread "main" java.lang.ExceptionInInitializerError
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at GreatBeyond.<init>(GreatBeyond.java:36)
    at GreatBeyond.main(GreatBeyond.java:66)
Caused by: java.lang.RuntimeException: org.hsqldb.jdbc.JDBCDriver
    at net.ucanaccess.jdbc.UcanaccessDriver.<clinit>(UcanaccessDriver.java:54)
    ... 4 more

第 36 行是Class.forName",我尝试在没有它的情况下运行该程序,但它失败了.我已经下载了 UCanAccess 的 zip 文件并将其解压到 com 目录,Jackcess 被解压到了 net 目录.谁能指出哪里出了问题?

Line 36 is "Class.forName", I have tried running the program with out it and it fails. I have downloaded the zip file for UCanAccess and extracted it to the com directory, the Jackcess was extracted to the net directory. Can anyone point out what is going wrong?

推荐答案

如果您不使用 IDE,那么您需要为 UCanAccess 的 JAR 文件及其所有依赖项(HSQLDB、Jackcess 等)指定 CLASSPATH 条目.).一种方法是在运行代码时使用 java 命令的 -cp 选项.

If you are not using an IDE then you need to specify the CLASSPATH entries for the JAR files of UCanAccess and all of its dependencies (HSQLDB, Jackcess, etc.). One way to do that is to use the -cp option of the java command when you run your code.

例如,我在UcaNoIde.java"中编译了以下代码后...

For example, after I compile the following code in "UcaNoIde.java" ...

import java.sql.*;

public class UcaNoIde {

    public static void main(String[] args) {
        String dbFileSpec = "C:/Users/Public/UCanAccessTest.accdb";
        String connStr = "jdbc:ucanaccess://" + dbFileSpec;
        try (Connection conn = DriverManager.getConnection(connStr)) {
            System.out.println("Connection established.");
        } catch (Exception e) {
            e.printStackTrace(System.err);
        }
    }

}

...进入UcaNoIde.class"文件我可以在我的 Windows 命令提示符下使用以下命令运行它:

... into a "UcaNoIde.class" file I can run it using the following command at my Windows command prompt:

java -cp .;C:/Users/Public/Downloads/UCanAccess/ucanaccess-3.0.3.jar;C:/Users/Public/Downloads/UCanAccess/lib/commons-lang-2.6.jar;C:/Users/Public/Downloads/UCanAccess/lib/commons-logging-1.1.1.jar;C:/Users/Public/Downloads/UCanAccess/lib/hsqldb.jar;C:/Users/Public/Downloads/UCanAccess/lib/jackcess-2.1.3.jar UcaNoIde

(对于 Linux,et. al.-cp 条目将用冒号 (:) 而不是分号(>;) 和文件路径会有点不同.)

(For Linux, et. al. the -cp entries will be separated by colons (:) instead of semicolons (;) and the file paths will be a bit different.)

另一种可能性是定义一个 CLASSPATH 环境变量,其条目与上面的 -cp 选项中的条目相同,这样您就不必每次都使用 -cp.

Another possibility is to define a CLASSPATH environment variable with the same entries as in the -cp option above so you don't have to use -cp every time.

这篇关于UCanAccess 初始化程序错误(在没有 IDE 的情况下编译/运行)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆