1

I made a code that connects to my sqlite driver which is in the CLASSPATH and reads some database file. I want to create an executable which can be used on computers that don't have the sqlite driver.

If I do:

jar cvfe exec.jar main_class

I will get "class not found: org.sqlite.JDBC" when running with

java -jar exec.jar

What should I do to make the executable work?

Edit: I don't know if it makes any difference, but this is the JDBC driver I use:

https://bitbucket.org/xerial/sqlite-jdbc

user3653831
  • 235
  • 6
  • 19

2 Answers2

2

You need to include the library inside the JAR. Maybe you don't know this, but JAR files are just ZIP files, so you can change their contents easily. Here are some quick instructions on how to do it. Assuming your JAR file is named exec.jar, and the JAR of the library you want to include (the JAR you downloaded) is driver.jar

  1. Change your file name from exec.jar to exec.zip.
  2. Extract all the contents of exec.zip into folder exec/
  3. Change your library file name from driver.jar to driver.zip
  4. Extract all the contents of driver.zip into folder driver/
  5. Copy the contents of driver/ into exec/, but do not copy the META-INF folder. If a pop-up asks if it's ok to merge the folders, click yes.
  6. Compress all files in exec/ into exec.zip
  7. Rename exec.zip to exec.jar (replace the original).

You can include any java library inside a JAR using this method.

Zenadix
  • 15,291
  • 4
  • 26
  • 41
  • Just a few questions. The driver has only one jar file. Is that what should be in the org/sqlite folder? Or do I have to extract the driver.jar? I've done both, but when I try to use the final exec.jar file it says it's corrupted. – user3653831 Sep 15 '14 at 17:52
  • Extract the driver.jar. Inside, you will find a folder named `org`. Inside `org` you will find a folder named `sqlite`. Copy all the contents of `sqlite` into your `exec.jar/org/sqlite`. – Zenadix Sep 15 '14 at 18:16
  • I updated the answer, hopefully making it more clear. Actually all you need to do is merge the library jar with your jar. – Zenadix Sep 15 '14 at 18:31
  • It does work now. The corruption was not an serious issue. Thanks. – user3653831 Sep 15 '14 at 18:48
  • Also works if I add org/sqlite to my java directory after compilation and type jar cvfe exec.jar main_class *.class org – user3653831 Sep 15 '14 at 18:55
0

Here is the doc:

C:\Windows\System32>jar /?
Illegal option: /
Usage: jar {ctxui}[vfmn0Me] [jar-file] [manifest-file] [entry-point] [-C dir] files ...

And so I think the command you need is:

jar cvfe exec.jar main_class main_class
djangofan
  • 28,471
  • 61
  • 196
  • 289
  • But how do I get the SQLite to work with the executable? – user3653831 Sep 15 '14 at 16:20
  • I suppose you just pass a -classpath arg to the above command that includes the location of the SQLite.jar file. of course, this is a problem because you aren't extracting the executable .jar and so you wont be able to load the resource. There is a way to handle it, just don't know the exact way. There is a Maven plugin that will do it. – djangofan Sep 15 '14 at 19:25