[Fixed]-No native library for SQLite JDBC in Windows
The problem is that JDBC won’t connect to the database saying that there is no native SQLite library.
Problem method:
public static void connect() throws SQLException, ClassNotFoundException
Exception in thread "main" java.sql.SQLException: Error opening connection at org.sqlite.SQLiteConnection.open(SQLiteConnection.java:239) at org.sqlite.SQLiteConnection.(SQLiteConnection.java:61) at org.sqlite.jdbc3.JDBC3Connection.(JDBC3Connection.java:28) at org.sqlite.jdbc4.JDBC4Connection.(JDBC4Connection.java:21) at org.sqlite.JDBC.createConnection(JDBC.java:115) at org.sqlite.JDBC.connect(JDBC.java:90) at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:677) at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:251) . Caused by: java.lang.Exception: No native library found for os.name=Windows, os.arch=x86_64, paths=[C:\Program Files\Java\jdk-15.0.2\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;E:\Program Files\gcc\bin;E:\Program Files\gcc\libexec\gcc\x86_64-w64-mingw32\8.3.0;C:\Program Files\Common Files\Oracle\Java\javapath;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\ProgramData\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\nodejs\;E:\Program Files\Maven\bin;E:\Program Files\PuTTY\;C:\Program Files\Git\cmd;C:\Users\Lenovo\AppData\Local\Microsoft\WindowsApps;C:\Users\Lenovo\AppData\Roaming\npm;E:\Program Files\IntelliJ IDEA 2020.3.3\bin;E:\Program Files\WebStorm 2020.1\bin;C:\Gradle\gradle-6.7.1\bin;C:\Users\Lenovo\AppData\Local\Programs\Microsoft VS Code\bin;C:\Program Files\heroku\bin;.] at org.sqlite.SQLiteJDBCLoader.loadSQLiteNativeLibrary(SQLiteJDBCLoader.java:367) at org.sqlite.SQLiteJDBCLoader.initialize(SQLiteJDBCLoader.java:67) at org.sqlite.core.NativeDB.load(NativeDB.java:63) at org.sqlite.SQLiteConnection.open(SQLiteConnection.java:235) . 10 more
Add this in your build file
kapt "org.xerial:sqlite-jdbc:3.34.0"
Check out this thread for details.
How do I include my embedded SQLite db in or with my executable Jar?
posted 4 years ago
I’m really struggling to with my Intellij jar (executable). I’m ready to hand it out for some friendly user testing but Intellij won’t handle the embedded SQLite db. So I was hoping someone would have a suggestion or solution.
The embeded DB is called research.db. Here are my details:
Code referencing the DB:
Where it is located:
src > research.db
Problem:
I cannot get the Jar to run because it cannot access the DB. What I thought I could do was create a «sqlite» folder under the src folder and Intellij would embed (literally) the DB within the JAR. I don’t think that is possible now. So I moved it where it was previously, back within the src folder.
What I’ve done:
I’ve been on a number of sites each with similar steps using Intellij (see attached image). I reached out to Intellij Community support then eventually was asked to enter a ticket, and they found the jar works but the database is the issue causing it not to launch.
I’ve never, ever, created an installer in my short coding career, but I don’t know if that is what needs to happen and edit the SQLConn (above code) in my application code. I did something similar but smaller than this in NetBeans with a DB and had no issue, but the DB was outside the JAR and had to be within the same folder. So I’m not sure what is different about Intellij.
I’m open to any suggestion or pointer in the right direction.
Saloon Keeper
posted 4 years ago
This is why you don’t let your IDE do your thinking for you. In production, you don’t have an IDE anymore.
To talk to an embedded SQLite db within an executable jar, you need two things:
1. The SQLite JDBC driver
2.. SQLite implementation code.
I’m not sure if SQLite has a native Java implementation jar or if you have to have a native-code rathole class (which would be part of the JDBC driver). The «rathole» would be to communicate with an SQLite DLL or .so library.
If it’s all available in native Java, you’d just need to ensure that both driver and DBMS code were made part of your executable JAR, and here please note that a JAR embedded within a JAR is not part of the outer JAR’s classpath — you need special logic to make that possible (Maven has a plugin for it).
If you need to talk to non-Java SQLite code, then the DLL/so has to be part of your library lookup path, and that’s a complicated (and OS-dependent) affair that I’ll have to ask you to ask Google about.
The secret of how to be miserable is to constantly expect things are going to happen the way that they are «supposed» to happen.
You can have faith, which carries the understanding that you may be disappointed. Then there’s being a willfully-blind idiot, which virtually guarantees it.
Marshal
posted 4 years ago
Just to add to Tim’s post: in the «target» system where the executable jar is going to run, the database cannot be inside the jar. It must be in a directory on the target system. So yeah, you just can’t send out the jar file and be done.
As for writing an installer, I’d suggest for your first attempt just sending the executable jar and the database and have the person at the target system receive them and put them somewhere. The person could be you and the target system your system, only your test would be to install them in a directory which has nothing at all to do with your IDEA directories.
posted 4 years ago
Hi Paul and Tim!
Based upon your comments, you may or may not approve of this approach, but here is what I have done and provided the code below. It took all day to research how and to create it, but I built an Install Class to verify and/or install the database. Since the JAR file in essence is like a ZIP file, the main class of my app (i.e ResearchApp) will implement the Install class and then verify or install the database to the users home path, validating and managing folder permissions. So far, it has worked on a couple user systems but I’m sure I may run into an issue being new to java and attempting at my best to manage all possible exceptions.
In essence, the first thing the Install will do is capture path where the JAR resides and then call to the users home path and concatenate an application folder, then check if the folder path and file exists. If not, it will iterate through the JAR to find the DB then try to confirm and or manage folder/file permissions if it fails to create. This database will come with some initial data samples. If successful, the later attempts to skip the install if the Install object class validates successfully. Thereafter, database connections are established success or failure.
I’m certainly still open to thoughts or suggestions on this attempt.
ResearchApp (main) call:
Saloon Keeper
posted 4 years ago
A JAR file isn’t «like» a ZIP file. It is a ZIP file. The only differences being the file extension and the presence of the META-INF directory where Java looks for magic properties. You can literally create a JAR using only the ZIP utility.
I think what Paul was getting at is that a ZIP file is not simply a subfile tree, even though in some cases an OS might make it look so. In particular, the only way in Java to «write» to a ZIP (JAR) file is to create a whole new ZIPfile.
Nothing wrong with having logic to look for and initialize a SQLite database if needed. In fact, that’s pretty much standard procedure in Android apps.
But please lose the Windows file delimiters. You can almost always use «real» slashes in file paths in Java, even for Windows files. Like so: «C:/Users/myself/databasex.db». It’s both more portable (works on Windows, Linux and MacOS) and it’s less likely to zap you since you don’t have to remember to double-up on backslashes.
And it’s easy to play with files in directories:
Your file logic is much too complicated. The File class and File Exceptions can make things much simpler if you let them.
The secret of how to be miserable is to constantly expect things are going to happen the way that they are «supposed» to happen.
You can have faith, which carries the understanding that you may be disappointed. Then there’s being a willfully-blind idiot, which virtually guarantees it.
posted 4 years ago
Hey, I’m all about simple, efficient and clearly understood! I like your signature line «An IDE is no substitute for an Intelligent Developer.» I just need to get there. I’m leaning on Intellij tohelp me find efficiencies at the moment. Your suggestion is good and I agree (for what that is worth) that adding back in the back slashes. However, I did not pickup on the sample code for the file directory and file name you provided. I like that.
Everyone is so helpful on this site. I was attracted to the idea new Java users could learn here with «no question too simple», instead of «why are you doing that stupid».
Saloon Keeper
posted 4 years ago
I’m afraid that signature is a rather bitter jab at managers who think they can hire the dumbest, cheapest monkeys they can get and expect the IDE to compensate for their deficiencies. But really, you can get into serious trouble using an IDE to do your work for you if you don’t know what it’s producing. I know that, alas, from bitter personal experience.
One of the problems with many traditional Internet newsgroups was that when people asked «dumb» or often-repeated questions, the Lords of the Forums would typically flame them to the point of physic scarring. One of our most fundamental rules here is that you don’t do that.
In more recent times, a certain alternative style of forum has emerged that is frequently used as a copy/paste resource, but it, also has suffered from a reputation for traumatizing what they (sometimes wrongly) interpreted as duplicate questions, dumb questions, and questions that didn’t meet the qualifications of the forum even if the answers might have been of interest anyway. They’ve been trying to remedy that though.
In the mean time, I’ll often answer the «same» question over and over and over. Partly because I can’t be bothered to put it in the Wiki, but also because everyone has different holes in how they understand things, and sometimes a personalized answer is simplly better.
So we’re glad we can be of help!
The secret of how to be miserable is to constantly expect things are going to happen the way that they are «supposed» to happen.
You can have faith, which carries the understanding that you may be disappointed. Then there’s being a willfully-blind idiot, which virtually guarantees it.
No Sqlite Library found
posted 9 years ago
The library is compiled with encryption library attached.
The driver I compiled
http://www.mediafire.com/?dx7i8bfnxi52dkx
The unencrypted db file
http://www.mediafire.com/download/6vmswm54dwu4iy7/ICJB.sqlite
The encrypted file
http://www.mediafire.com/download/84qgrm5ucbt56t3/
But I am getting SQlite library not found error
Any hints?
Seems like the jar cannot locate the sqlitejdbc.dll, but where should I put it to?
Don’t know why it doesn’t pass the JUnit test
I’ve made 2 tweaks to the source code
1) Adding #define NO_CRYPT to SqlCipher where there is #include
2) Adding
posted 9 years ago
I finally resolved it by downloading the prebuilt library from java2s.
But One thing funny is even though I don’t supply a password
to open the encrypted database, it opens it up nevertheless.
How can I prevent that from happening?
Thanks
Jack