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

4
  • Do you use maven/gradle for build your jar? Commented Sep 15, 2014 at 14:40
  • Can't I do this from command line? It should be easy, but I can't find it anywhere Commented Sep 15, 2014 at 16:16
  • You should include the driver inside the JAR. Are you doing that? Commented Sep 15, 2014 at 16:27
  • No. I'm not sure I know how. Commented Sep 15, 2014 at 16:33

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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.
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.
I updated the answer, hopefully making it more clear. Actually all you need to do is merge the library jar with your jar.
It does work now. The corruption was not an serious issue. Thanks.
Also works if I add org/sqlite to my java directory after compilation and type jar cvfe exec.jar main_class *.class org
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

2 Comments

But how do I get the SQLite to work with the executable?
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.