0

I am trying to make my application work in a stand alone jar. The problem is, a jar is generated for my program, and a bunch of other jars are generated for the libraries. Is there any way to get these jars to get inside one? I am using Gradle if that helps.

The IntelliJ IDEA artifact config:

enter image description here

The output directory:

enter image description here

What I expected (and want) to happen:

enter image description here

5
  • Added a picture for what I would like to be the result. Commented Aug 29, 2015 at 22:15
  • stackoverflow.com/questions/4871656/… refers to this as an uberjar or a fatjar. Using maven this is called a shaded jar. Commented Aug 29, 2015 at 22:17
  • Instead of merging all the jars into a fat jar, just put them all in the same folder and enhance the manifest to specify the classpath, so they're all loaded when you run with java -jar. Commented Aug 29, 2015 at 22:22
  • 1
    @Andreas I want to release my software as a stand along file. That way it simplifies the download process. It does work with having all the jars spread out, but that isn't what I want to do. Commented Aug 29, 2015 at 22:26
  • It appears that this cannot be done normally: stackoverflow.com/questions/12357136/… Commented Aug 29, 2015 at 22:55

1 Answer 1

2

You need a fat-jar (jar file with all it's dependencies inside). It's not a big problem for Gradle, you just need to make one additional task of type jar, which will collect all the dependencies and zip it alltogether

There are many examples, how you can do it, here is one of them. Take a closer look at task fatJar:

task fatJar(type: Jar) {        
  baseName = project.name + '-all'
  from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
  with jar
}
Sign up to request clarification or add additional context in comments.

6 Comments

I have never used Gradle before for building, it just does whatever IntelliJ IDEA does by default.
I'm not sure that it's a default Idea's behaviour, because it's not standart Gradle's behaviour. And the only Idea does - is call the gradle script. And furthermore, it will put all the dependencies inside a single jar, but they'll be unziped
You can configure the way stuff is built inside IDEA, I don't see anything about the specific build stuff that I have configured inside build.gradle
Ok, now I see, you don't use your gradle scripts to pack the artfacts, good. But anyway, not sure that it's possible to do like you want it, because you need a specific classloader for this, take a look here stackoverflow.com/questions/183292/…
So, if you need just a single jar, why don't you try to do it with unziped classes added to the root of your jar? I'm pretty sure, it's possible to do not only with maven or gradle, but with Idea too.
|

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.