billlipeng
billlipeng

Reputation: 320

Exception in thread "main" java.lang.NoClassDefFoundError: org/sqlite/NativeDB

I am trying run an simple example for sqlite on mac. I am pretty sure the code works well on Windows. But not on mac. I really appreciate it if someone could help me with this.

The code is running in Eclipse. I added the sqlite-jdbc4-3.8.2-SNAPSHOT.jar as internal & external jar in the project.

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

Console ##

  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

Upvotes: 6

Views: 8811

Answers (3)

This is a bug in the 3.8.2-SNAPSHOT release: http://bitbucket.org/xerial/sqlite-jdbc/issue/127
The native libraries are present for Linux, mac and windows, but only the windows one is using the correct package of the NativeDB class.

Upvotes: 6

billlipeng
billlipeng

Reputation: 320

Finally I figured out how to make it work. I tried another jar and it(sqlite-jdbc-3.7.2.jar) works well for Mac. It seems that sqlite-jdbc4-3.8.2-SNAPSHOT.jar. @ddevienne , maybe 3.8.2 does not support Mac OS.

Upvotes: 13

ddevienne
ddevienne

Reputation: 1882

If you read carefully the stack trace, you see that SQLiteJDBCLoader was successfully found and loaded, then it ran extractAndLoadLibraryFile, to load the native SQLite dynamic library. But the load of this native lib failed. This implies that the jar embeds the native lib, which unlike Java byte code is not platform independent. So either it embeds the native lib for several OSes, or a single OS like Windows, explaining why your jar works on Windows and not MAC OS.

Upvotes: 1

Related Questions