Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
396 views
in Technique[技术] by (71.8m points)

java - UCanAccess Initializer Error (compile/run without IDE)

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.

References:

Manipulating an Access database from Java without ODBC

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

my code sample is the following:

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();

My error message is:

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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);
        }
    }

}

... 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

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...