Search code examples
javaintellij-ideaguava

How to properly build .jar artifact with dependencies?


After spending countless hours, googling similar threads and whatnot, i'm still stuck with this issue. I have simple Java app, that uses Guava. I'm using Intellij IDEA CE 2021. So, i have copied guava.jar in /lib, and included it in Project Structure > Modules > Dependencies: screenshot

I have also created .jar atrifact, and added guava dependency: screenshot

After building artifact and running it from console, i get Exception in thread "main" java.lang.NoClassDefFoundError: com/google/common/io/Files, no matter what i try. I don't have any more ideas, any advice here?


Solution

  • Some simple steps and you are done with this task:

    1. put the guava.jar file in a directory inside the project (e.g. lib)
    2. if you have just one dependency go in the project explorer and right-click on guava-30...jar and choose "Add as library"
    3. if you know there are more dependencies right-click on the "lib" folder and choose "Add as library..."
    4. File - Project Structure - Artifacts - press "+" - JAR - from Modules...
    5. choose the Main class of your project and "OK"
    6. generate the jar: Build - Build Artifacts - Build
    7. you now have a folder in your project "out" - artifacts with the generated project jar

    Just a note (not valid for Guava): if you are using signed jars like Bouncy Castle you will notice that this doesn't work like describes because the "including" of the external dependency file in the project jar will destroy the signature.