{"id":2902,"date":"2021-04-24T13:00:00","date_gmt":"2021-04-24T12:00:00","guid":{"rendered":"https:\/\/chewett.co.uk\/blog\/?p=2902"},"modified":"2021-04-24T15:16:32","modified_gmt":"2021-04-24T14:16:32","slug":"making-a-fully-packaged-jarfile-with-all-dependencies-with-gradle","status":"publish","type":"post","link":"https:\/\/chewett.co.uk\/blog\/2902\/making-a-fully-packaged-jarfile-with-all-dependencies-with-gradle\/","title":{"rendered":"Making a fully packaged Jarfile with all dependencies with gradle"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"678\" height=\"254\" data-attachment-id=\"2906\" data-permalink=\"https:\/\/chewett.co.uk\/blog\/2902\/making-a-fully-packaged-jarfile-with-all-dependencies-with-gradle\/gradle_fat_jar\/\" data-orig-file=\"https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2021\/04\/gradle_fat_jar.jpg?fit=800%2C300&amp;ssl=1\" data-orig-size=\"800,300\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"gradle_fat_jar\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2021\/04\/gradle_fat_jar.jpg?fit=300%2C113&amp;ssl=1\" data-large-file=\"https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2021\/04\/gradle_fat_jar.jpg?fit=678%2C254&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2021\/04\/gradle_fat_jar.jpg?resize=678%2C254&#038;ssl=1\" alt=\"\" class=\"wp-image-2906\" srcset=\"https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2021\/04\/gradle_fat_jar.jpg?w=800&amp;ssl=1 800w, https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2021\/04\/gradle_fat_jar.jpg?resize=300%2C113&amp;ssl=1 300w, https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2021\/04\/gradle_fat_jar.jpg?resize=768%2C288&amp;ssl=1 768w, https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2021\/04\/gradle_fat_jar.jpg?resize=50%2C19&amp;ssl=1 50w\" sizes=\"auto, (max-width: 678px) 100vw, 678px\" \/><\/figure>\n\n\n\n<p>This blog post talks about how you can create a fully packaged jarfile including all its gradle dependencies.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">What is a fully packaged jar file also known as a fat jar or uber jar<\/h2>\n\n\n\n<p>When using gradle dependencies it will download the various required files and have them available. When running in the IDE it will add these libraries to your class path so your code can access them.<\/p>\n\n\n\n<p>However when you produce a standard jar file unless you put all your dependencies library jar&#8217;s in the same location it will fail to run normally.<\/p>\n\n\n\n<p>In my test repository I can run the created jar but it fails with a message explaining that it is missing some dependencies as shown below.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nException in thread &quot;main&quot; java.lang.NoClassDefFoundError: com\/google\/gson\/Gson\n        at net.chewett.github.gradlepackagedjarexample.GradleImportTest.main(GradleImportTest.java:9)\nCaused by: java.lang.ClassNotFoundException: com.google.gson.Gson\n        at java.base\/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)\n        at java.base\/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)\n        at java.base\/java.lang.ClassLoader.loadClass(ClassLoader.java:521)\n        ... 1 more\n<\/pre><\/div>\n\n\n<p>To ensure your jar file includes all dependencies you need it to include you can &#8220;package&#8221; up these dependencies into the jar file. This jar is also sometimes called a fat jar or an uber jar.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Creating a fully packaged jar file\/fat jar\/uber jar<\/h2>\n\n\n\n<p>To create a fully packaged jar file you will can create a new gradle task to include all compile time files. An example of this is below<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\ntask goodjar (type: Jar) {\n    manifest {\n        attributes &quot;Main-Class&quot;: &quot;net.chewett.github.gradlepackagedjarexample.GradleImportTest&quot;\n    }\n    archiveBaseName.set(project.name + &#039;-good&#039;)\n    from {\n        configurations.compileClasspath.collect { it.isDirectory() ? it : zipTree(it) }\n    } with jar\n}\n<\/pre><\/div>\n\n\n<p>This will create a jarfile including all required dependencies that will launch the main class:<\/p>\n\n\n\n<p><code>net.chewett.github.gradlepackagedjarexample.GradleImportTest<\/code><\/p>\n\n\n\n<p>By doing this all dependencies in the <code>dependencies<\/code> section that are required for <code>implementation<\/code> are included in the jarfile.<\/p>\n\n\n\n<p>When I run my new jar gradle command it will now correctly run and use the Google GSON library noted as missing above.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nJSON int array converted from a string by Gson\n&#x5B;1, 2, 3, 4, 5]\n<\/pre><\/div>\n\n\n<p>A git repository showing this <a href=\"https:\/\/github.com\/chewett\/gradle-packaged-jar-example\" data-type=\"URL\" data-id=\"https:\/\/github.com\/chewett\/gradle-packaged-jar-example\" target=\"_blank\" rel=\"noreferrer noopener\">working is available on github<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This blog post talks about how you can create a fully packaged jarfile including all its gradle dependencies.<\/p>\n","protected":false},"author":1,"featured_media":2907,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[5],"tags":[405,368,407,364,406,404],"class_list":["post-2902","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-informational","tag-fat-jar","tag-gradle","tag-jarfile","tag-java","tag-packaged-jar","tag-uber-jar"],"wppr_data":{"cwp_meta_box_check":"No"},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2021\/04\/gradle_fat_jar_posticon_OUTPUT.png?fit=1200%2C628&ssl=1","jetpack_shortlink":"https:\/\/wp.me\/p2toWX-KO","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":2420,"url":"https:\/\/chewett.co.uk\/blog\/2420\/fixing-gradle-error-unnecessarily-replacing-a-task-that-does-not-exist-is-not-supported\/","url_meta":{"origin":2902,"position":0},"title":"Fixing Gradle error Unnecessarily replacing a task that does not exist is not supported.","author":"Chewett","date":"March 7, 2020","format":false,"excerpt":"This short blog post talks about why you get this error and how to fix \"Unnecessarily replacing a task that does not exist is not supported\". What is the error and why does it occur When you attempt to run or debug a method using Gradle and IntelliJ you may\u2026","rel":"","context":"In &quot;Fixes&quot;","block_context":{"text":"Fixes","link":"https:\/\/chewett.co.uk\/blog\/category\/fixes\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2020\/03\/fixing_gradle6_error_icon.jpg?fit=800%2C800&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2020\/03\/fixing_gradle6_error_icon.jpg?fit=800%2C800&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2020\/03\/fixing_gradle6_error_icon.jpg?fit=800%2C800&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2020\/03\/fixing_gradle6_error_icon.jpg?fit=800%2C800&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":953,"url":"https:\/\/chewett.co.uk\/blog\/953\/fixing-twitch-minecraft-launcher-showing-white-white-screen\/","url_meta":{"origin":2902,"position":1},"title":"Fixing Twitch Minecraft Launcher only showing white or white screen","author":"Chewett","date":"January 3, 2018","format":false,"excerpt":"This blog post describes how you can fix the Twitch Minecraft Launcher only showing a white or black screen. Minecraft launcher showing white or black screen This blog post aims to fix the issue where the Twitch\u00a0Minecraft\u00a0launcher only shows a black or white screen. This prevents Twitch from properly launching\u2026","rel":"","context":"In &quot;Fixes&quot;","block_context":{"text":"Fixes","link":"https:\/\/chewett.co.uk\/blog\/category\/fixes\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2018\/01\/fixing_twitch_minecraft_white_black_screen.jpg?fit=800%2C800&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2018\/01\/fixing_twitch_minecraft_white_black_screen.jpg?fit=800%2C800&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2018\/01\/fixing_twitch_minecraft_white_black_screen.jpg?fit=800%2C800&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2018\/01\/fixing_twitch_minecraft_white_black_screen.jpg?fit=800%2C800&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":997,"url":"https:\/\/chewett.co.uk\/blog\/997\/installing-dropbox-fedora-27-xfce\/","url_meta":{"origin":2902,"position":2},"title":"Installing Dropbox on Fedora 27 XFCE","author":"Chewett","date":"February 7, 2018","format":false,"excerpt":"This blog post describes how you can install Dropbox on Fedora 27 XFCE. Installing pre-requisite packages Before we can install Dropbox we need to install a number of pre-requisite packages. The full command to install the required packages is: sudo\u00a0dnf\u00a0install\u00a0libgnome\u00a0nautilus-extensions python-gpgme The first two packages, libgnome\u00a0and\u00a0nautilus-extensions, are required by the\u2026","rel":"","context":"In &quot;Software&quot;","block_context":{"text":"Software","link":"https:\/\/chewett.co.uk\/blog\/category\/software\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2018\/02\/installing_dropbox_on_fedora.jpg?fit=800%2C800&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2018\/02\/installing_dropbox_on_fedora.jpg?fit=800%2C800&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2018\/02\/installing_dropbox_on_fedora.jpg?fit=800%2C800&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2018\/02\/installing_dropbox_on_fedora.jpg?fit=800%2C800&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":461,"url":"https:\/\/chewett.co.uk\/blog\/461\/updating-raspberry-pi-cluster-without-logging-every-pi\/","url_meta":{"origin":2902,"position":3},"title":"Updating the Raspberry Pi Cluster without logging in to every Pi","author":"Chewett","date":"July 1, 2017","format":false,"excerpt":"The Raspberry Pi foundation have recently updated their Raspbian image. This brings bugfixes and upgrades to the Raspberry Pi. In this blogpost I share how you can run a command on your Raspberry Pi without logging in (this is mostly true although you still do \"log in\" just not in\u2026","rel":"","context":"In &quot;Raspberry Pi Cluster&quot;","block_context":{"text":"Raspberry Pi Cluster","link":"https:\/\/chewett.co.uk\/blog\/category\/raspberry-pi-cluster\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2017\/07\/raspbian_update_command.png?fit=580%2C307&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2017\/07\/raspbian_update_command.png?fit=580%2C307&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2017\/07\/raspbian_update_command.png?fit=580%2C307&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":1283,"url":"https:\/\/chewett.co.uk\/blog\/1283\/fixing-failed-to-build-nokogiri-1-8-3-gem-fedora-28\/","url_meta":{"origin":2902,"position":4},"title":"Fixing Failed to build nokogiri 1.8.3 gem Fedora 28","author":"Chewett","date":"June 27, 2018","format":false,"excerpt":"Today I talk about how I fixed \"Failed to build nokogiri\u00a01.8.3\" on Fedora 28. Whats the issue with nokogiri 1.8.3 on Fedora 28? At the moment any ruby applications that requires nokogiri\u00a01.8.3 in the Gemfile will fail to build. This is due to an issue with the libraries packaged in\u2026","rel":"","context":"In &quot;Fixes&quot;","block_context":{"text":"Fixes","link":"https:\/\/chewett.co.uk\/blog\/category\/fixes\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2018\/06\/fixing_failed_to_build_nokogiri_fedora.jpg?fit=800%2C800&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2018\/06\/fixing_failed_to_build_nokogiri_fedora.jpg?fit=800%2C800&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2018\/06\/fixing_failed_to_build_nokogiri_fedora.jpg?fit=800%2C800&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2018\/06\/fixing_failed_to_build_nokogiri_fedora.jpg?fit=800%2C800&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":490,"url":"https:\/\/chewett.co.uk\/blog\/490\/setting-ssl-certbot-apache-fedora\/","url_meta":{"origin":2902,"position":5},"title":"Setting up SSL with certbot with Apache and Fedora","author":"Chewett","date":"August 16, 2017","format":false,"excerpt":"This post describes how to set \u00a0up a SSL certificate with Certbot on Apache and Fedora. The guide primarily follows the guide on the certbot website however adds some additional information for if auto configuration fails. Running Certbot on Fedora On Fedora running certbot is relatively easy as it is\u2026","rel":"","context":"In &quot;Informational&quot;","block_context":{"text":"Informational","link":"https:\/\/chewett.co.uk\/blog\/category\/informational\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2017\/08\/ssl_with_apache_fedora_and_certbot.jpg?fit=800%2C800&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2017\/08\/ssl_with_apache_fedora_and_certbot.jpg?fit=800%2C800&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2017\/08\/ssl_with_apache_fedora_and_certbot.jpg?fit=800%2C800&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2017\/08\/ssl_with_apache_fedora_and_certbot.jpg?fit=800%2C800&ssl=1&resize=700%2C400 2x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/chewett.co.uk\/blog\/wp-json\/wp\/v2\/posts\/2902","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/chewett.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/chewett.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/chewett.co.uk\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/chewett.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=2902"}],"version-history":[{"count":4,"href":"https:\/\/chewett.co.uk\/blog\/wp-json\/wp\/v2\/posts\/2902\/revisions"}],"predecessor-version":[{"id":2908,"href":"https:\/\/chewett.co.uk\/blog\/wp-json\/wp\/v2\/posts\/2902\/revisions\/2908"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/chewett.co.uk\/blog\/wp-json\/wp\/v2\/media\/2907"}],"wp:attachment":[{"href":"https:\/\/chewett.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=2902"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/chewett.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=2902"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/chewett.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=2902"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}