javasqlitejdbcsqlitejdbc

Java SQLite org.sqlite.JDBC classpath broken?


I stumbled upon a weird error while using JDBC sqlite with org.sqlite.JDBC my code compiles and runs fine on Windows. But when I tried moving it to Ubuntu it started showing this:

Exception in thread "main" java.lang.ClassNotFoundException: org.sqlite.JDBC
        at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:259)
        at mall.SQLiteJDBC.<init>(SQLiteJDBC.java:27)
        at mall.AllegroReader.<init>(AllegroReader.java:33)
        at mall.Mall.main(Mall.java:31)

I'm running it with java -classpath "sqlite-jdbc-3.7.2.jar" -jar Mall.jar" and java -classpath "sqlite-jdbc4-3.8.2-SNAPSHOT.jar" -jar Mall.jar with both versions in the same directory as my jar and I've tried a dozen different options specifying classpath and it behaves exactly the same. I tried openjdk and oracle jdk. I tried rebuilding it on Ubuntu, changing ant .xmls, changing paths, etc.

I have no idea what is going on. Pls help.

Here is what happens inside my dist directory:

work1@workwork:/var/www/mall/dist$ ls
mall.db  Mall.jar  Mall.jar.old  sqlite-jdbc-3.8.4.3-SNAPSHOT.jar

work1@workwork:/var/www/mall/dist$ java -classpath "sqlite-jdbc-3.8.4.3-SNAPSHOT.jar:Mall.jar" Mall
Error: Could not find or load main class Mall

Solution

  • The classpath is ignored when you use -jar.

    You have to either include the dependencies in the jar (or at least have the jar manifest point to them), or run it with -classpath sqlite.jar:Mall.jar the.main.class.

    Error: Could not find or load main class Mall.main. all files are there, my main class comes from Mall.java and is in mall package which compiles to Mall.jar

    So the correct command line is:

    java -classpath "sqlite-jdbc-3.8.4.3-SNAPSHOT.jar:Mall.jar" mall.Mall
    

    OP findings

    to view the classes in jar use jar tf Mall.jar - from this I got mall/Mall.class meaning my class containing main was mall.Mall

    it showed

    mall/Mall.class

    so I should have used mall.Mall as the class to run (instead of pulling my hair)