线程“main”中的异常java.lang.NoClassDefFoundError:org / sqlite / NativeDB

时间:2014-02-28 21:55:06

标签: sqlite

我正在尝试在mac上运行一个简单的sqlite示例。我很确定该代码在Windows上运行良好。但不是在Mac上。如果有人能帮助我,我真的很感激。

代码在Eclipse中运行。我添加了sqlite-jdbc4-3.8.2-SNAPSHOT.jar作为内部和&项目中的外部jar。

  public class Test1 {

  private static Connection c;
  private static String filepath = "/Users/zerocraft/Documents/workspace/sql_test/";
  private static String sql;
  private static Statement query;


  public static void main(String[] args) {
          System.out.println("START");
          try{

            Class.forName("org.sqlite.JDBC");
            System.out.println("START2");

            c = DriverManager.getConnection("jdbc:sqlite:"+filepath+"projone.db");
            System.out.println("START3");

            }

            catch(Exception e){
                e.printStackTrace();
            }

                 sql =  "INSERT INTO table(date,time,client_id,run_id,latitude," +
                        "longitude,bearing,speed,altitude,sensor_id,sensor_type," +
                        "sensor_value,attribute)"
                + "VALUES ('A','B','C','D',0,1,2,3,4,'E','F','G','H')";


                try{
                query = c.createStatement();

                 query.executeUpdate(sql);
                 query.close();
                }
                catch(SQLException el){el.printStackTrace();}
        }   
   }        

控制台##

  START
  START2
  Exception in thread "main" java.lang.NoClassDefFoundError: org/sqlite/NativeDB
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary1(ClassLoader.java:1965)
at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1890)
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1851)
at java.lang.Runtime.load0(Runtime.java:795)
at java.lang.System.load(System.java:1062)
at org.sqlite.SQLiteJDBCLoader.loadNativeLibrary(SQLiteJDBCLoader.java:200)
at org.sqlite.SQLiteJDBCLoader.extractAndLoadLibraryFile(SQLiteJDBCLoader.java:148)
at org.sqlite.SQLiteJDBCLoader.loadSQLiteNativeLibrary(SQLiteJDBCLoader.java:249)
at org.sqlite.SQLiteJDBCLoader.initialize(SQLiteJDBCLoader.java:65)
at org.sqlite.core.NativeDB.load(NativeDB.java:53)
at org.sqlite.core.CoreConnection.open(CoreConnection.java:136)
at org.sqlite.core.CoreConnection.<init>(CoreConnection.java:66)
at org.sqlite.jdbc3.JDBC3Connection.<init>(JDBC3Connection.java:21)
at org.sqlite.jdbc4.JDBC4Connection.<init>(JDBC4Connection.java:23)
at org.sqlite.SQLiteConnection.<init>(SQLiteConnection.java:44)
at org.sqlite.JDBC.createConnection(JDBC.java:113)
at org.sqlite.JDBC.connect(JDBC.java:87)
at java.sql.DriverManager.getConnection(DriverManager.java:571)
at java.sql.DriverManager.getConnection(DriverManager.java:233)
at sql_test.Test1.main(Test1.java:22)
   Caused by: java.lang.ClassNotFoundException: org.sqlite.NativeDB
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 21 more

3 个答案:

答案 0 :(得分:13)

最后,我想出了如何让它发挥作用。我尝试了另一个jar,它(sqlite-jdbc-3.7.2.jar)适用于Mac。看来是sqlite-jdbc4-3.8.2-SNAPSHOT.jar。 @ddevienne,也许3.8.2不支持Mac OS。

答案 1 :(得分:6)

这是3.8.2-SNAPSHOT版本中的错误:http://bitbucket.org/xerial/sqlite-jdbc/issue/127
本机库适用于Linux,Mac和Windows,但只有一个窗口使用NativeDB类的正确包。

答案 2 :(得分:1)

如果仔细阅读堆栈跟踪,您会看到SQLiteJDBCLoader已成功找到并加载,然后运行extractAndLoadLibraryFile,以加载本机SQLite动态库。但是这个本机lib的负载失败了。这意味着jar嵌入了本机lib,与Java字节代码不同,平台无关。因此要么为几个操作系统嵌入本机库,要么在Windows之类的单个操作系统中嵌入,解释了为什么你的jar可以在Windows而不是MAC OS上工作。