UCanAccess初始化程序错误(无IDE编译/运行) [英] UCanAccess Initializer Error (compile/run without IDE)

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

问题描述

我一直在尝试创建一个新项目来使用UCanAccess来读取MS Access文件。我一直在关注来自@Gord Thompson的信息和ganub中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,那么你需要指定CLASSPATH条目UCanAccess的JAR文件及其所有依赖项(HSQLDB,Jackcess等)。一种方法是在运行代码时使用 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 条目将以冒号()而不是分号(<$ c $)分隔c>; )和文件路径会有所不同。)

(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天全站免登陆