java.lang.ClassNotFoundException: org.sqlite.JDBC Androidstudio Java Module
Asked Answered
K

4

9

I have no clue (too stupid), why my app doesn't find the jdbc class at runtime:

Exception in thread "main" java.lang.ClassNotFoundException: org.sqlite.JDBC

Do you have any hint ? Complete Simplified usecase in this repo : https://bitbucket.org/solvapps/jdbcapp

I have added a module called "maintain" to my Androidstudio project for maintaining stuff, which should not be included in the app. (I have done this already with another app).

enter image description here

My maintain gradle :

apply plugin: 'java'

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
}

sourceCompatibility = "1.8"
targetCompatibility = "1.8"

repositories {
    mavenCentral()
}


dependencies {
    compile 'org.xerial:sqlite-jdbc:3.16.1'
    compile 'commons-net:commons-net:3.3'
    compile 'org.zeroturnaround:zt-zip:1.11'
    compile 'org.zeroturnaround:zt-zip:1.11'
    compile 'org.apache.commons:commons-lang3:3.5'
}

After sync I see the jar in External Libraries.

enter image description here

My code, which is not red :

 public static void connect(String language, boolean readonly) throws SQLException, ClassNotFoundException, IOException {

        // Hole DBPath
        String dbpath = Constants.getDbName(language);

        Class.forName("org.sqlite.JDBC");
        SQLiteConfig config = new SQLiteConfig();

        if (readonly){
            config.setReadOnly(true);
        }else{
            config.setReadOnly(false);
        }

        connection = DriverManager.getConnection("jdbc:sqlite:" + dbpath,config.toProperties());
    }

Error:

Exception in thread "main" java.lang.ClassNotFoundException: org.sqlite.JDBC
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at solveraapps.maintain.DbService.connect(DbService.java:24)
Karinakarine answered 2/4, 2018 at 5:37 Comment(3)
Android support of version xerial sqlite-jdbc 3.16.1 is explicitly marked as experimental. Why don't you use the latest version?Orton
This has nothing to do with version of jdbc driver. Its a general problem recognizing jars from a module.Karinakarine
Have you checked that the sqlite-jdbc classes are included into the app`s dex file(s) (e.g. via Android Sudio APK Inspector)?Orton
P
5
try{  
        Class.forName("com.mysql.jdbc.Driver");  
        Connection con;
        con=DriverManager.getConnection("jdbc:mysql://localhost:8080/DBName","root","root");  
        //here DBName is database name, root is username and password  
        Statement stmt=con.createStatement();  
        ResultSet rs=stmt.executeQuery("select * from Table_name");
        //Table_name is Table Name Which Must Exist IN Your Database
        while(rs.next())  
        System.out.println(rs.getInt(1)+"  "+rs.getString(2)+"  "+rs.getString(3));  
        con.close();  
    }catch(Exception e){ System.out.println(e);}  
    } 

You Must Add mysql-connector.jar File to Your Libraries, You can Download File From Here

Pentothal answered 8/4, 2018 at 21:12 Comment(1)
No this doesn't work for AndroidStudio. This only works in Apps domain, but not from a module, which I need. It works perfect in IntelliJ.Karinakarine
S
1

org.sqlite.JDBC belongs to sqlite-jdbc jar, you need to include this jar in your classpath. You can use gradle to download jar from maven repository. Find the gardle depedency

compile 'org.xerial:sqlite-jdbc:3.8.11'
Spoonerism answered 12/4, 2018 at 12:59 Comment(0)
A
1

It looks like you have added the library to the project, but didn't add this library to the dependencies of the module: Take Look At Here

Abednego answered 12/4, 2018 at 13:51 Comment(0)
K
1

Well, it's not the perfect solution but it might work. Add:

repositories {

    ...

    flatDir {
        dirs 'libs'
    }
}

And:

dependencies {

    ...

    compile files('libs/sqlite-jdbc-3.16.1.jar')
}

Don't forget to add sqlite-jdbc-3.16.1.jar into

[your project's folder]\app\libs

as you will be using a local jar file instead of importing it. Just create the "libs" folder if it doesn't exist.

It's not exactly what you're asking for, but it might solve the problem. Also note that "compile" is getting deprecated by the end of 2018. I only gave you this solution because I'm 100% sure it works. But you should try to replace that "compile files" for the new term ("implementation files", I think?).

Let me know how it works out.

Good luck.

Kalvn answered 12/4, 2018 at 15:19 Comment(2)
Thanks a lot for helping, but this seems not to be a sustained solution. In the meanwhile I splittet my project into 2 projects (app, Maintain). For the Maintain Project I use IntelliJ.Karinakarine
It is sustainable. You just have to find the new way Android imports local library files with the keyword "implementation". It's a brand new condition, so I haven't gotten around to it yet.Helminthic

© 2022 - 2024 — McMap. All rights reserved.