Ofek Gila Ofek Gila - 3 months ago 15
Java Question

How to include dependency jar files into jar file

I'm build a jar file with code that uses other jar files as dependencies, and I can't get it to run (I keep getting a NoClassDefFoundError).

All the other similar issues I could find online used maven as a solution, which I want to avoid doing. When I run my code like this:

#!/bin/bash
java -cp "src/.:src/jsoup-1.9.2.jar:src/commons-cli-1.3.1.jar" WikipediaWords $@


it runs fine, but when I try:

java -cp "src/.:src/jsoup-1.9.2.jar:src/commons-cli-1.3.1.jar" -jar WikipediaWords.jar $@


it complains.

I compile my jar file with all the dependencies inside, though, so I really have no clue why it can't find anything. Ideally, I wouldn't even want to have to add the -cp path to my java execution, and just have the jar file know to use the nested jar dependency files.

If you have any clue about how to solve this (run the jar file without class def error) without the use of Maven, please let me know!

Thanks in advance!

P.S. My building code might be of use:

#!/bin/bash
javac -cp "src/.:src/jsoup-1.9.2.jar:src/commons-cli-1.3.1.jar" src/WikipediaWords.java
jar cfm src/WikipediaWords.jar src/Manifest.txt -C src/ .

Answer

Try running the below command:

java -cp "src/.:src/jsoup-1.9.2.jar:src/commons-cli-1.3.1.jar:WikipediaWords.jar" WikipediaWords $@

OR

java -cp "WikipediaWords.jar:src/.:src/jsoup-1.9.2.jar:src/commons-cli-1.3.1.jar" WikipediaWords $@