32

My process for creating a runnable JAR from a project with many libraries with Eclipse has been.

Export > Runnable JAR > Select launch configuration > Package required libraries into generated JAR > Finish

This creates a single JAR in my export destination which I FTP to my server and run fine.

I recently switched to IntelliJ for various reasons and it has been an improvement in all cases except for building my JARs which forces me to go back to eclipse temporarily.

With IntelliJ I:

Open Project Structure > Artifacts and create a new JAR from modules with dependencies. I then have my output layout with the name of the JAR, a META-INF inside it and all my libaries with the format Extracted <lib.jar/> (sorry I can't upload screenshots on this VPN.)

I build my artifact (which is about 15MB bigger) and FTP it to my server, try to run it and I get the error:

Exception in thread "main" java.lang.SecurityException: Invalid signature file digest for Manifest main attributes

How do I mimic how I do it in Eclipse?

EDIT

The two JARs that are created differ considerably:

The valid jar that Eclipse creates looks like this: Good jar

Whereas the invalid .JAR looks like this: Bad jar

Nanor
  • 2,400
  • 6
  • 35
  • 66
  • is it a maven project? you can do it using `maven-shade-plugin` – nandsito Jan 19 '17 at 16:45
  • It's not Maven, sadly. – Nanor Jan 19 '17 at 16:46
  • You can add `Main Class` in `jar` task run jar task `jar { manifest { attributes 'Main-Class': 'class name' } from { configurations.compile.collect { it.isDirectory()? it: zipTree(it) } } }` – mallaudin May 02 '17 at 09:01
  • This might be useful https://blog.jetbrains.com/idea/2010/08/quickly-create-jar-artifact/ More specifically this comment: https://blog.jetbrains.com/idea/2010/08/quickly-create-jar-artifact/#comment-1787 – trappski May 02 '17 at 09:41
  • 2
    Isn't this covered here? http://stackoverflow.com/questions/1082580/how-to-build-jars-from-intellij-properly – mindas May 02 '17 at 16:14
  • @mindas The created JAR is 15mb bigger than the Eclipse counterpart. – Nanor May 03 '17 at 15:17
  • @Nanor you would have to look at the contents of the jar (it's a zip file) and check what the differences are. Don't think anybody here could provide any help without knowing the details. – mindas May 04 '17 at 06:55
  • @mindas good point! Added. – Nanor May 04 '17 at 08:33
  • 1
    This is why you should use a build tool for all projects rather than depending on your IDE for this. You can create a runnable jar with Ant, Gradle, or Maven. Then you just have to configure the build tool once and then it will work regardless of the IDE used. – Michael May 08 '17 at 19:18

4 Answers4

24

You're facing two issues one seems major and one is minor:

  • Major: signature exception
  • Minor: size of the jar is 15 MB greater than the jar produced by eclipse.

The solution of both of the issues lies in the way you are building the artifact. First of all, remove all extracted *.jar then add all the jar's from the available elements pan as shown in the figure.

Dependencies screenshot

It is obvious that adding packaged(compressed) jars only, will decrease the size. But it also solves the signing issue. For more explanation, please have a look at this article. I'll only quote one line.

It's probably best to keep the official jar as is and just add it as a dependency in the manifest file......

It seems that some of the dependencies are signed and repackaging messes up the META-INF of your project.

  • 1
    I get `java.lang.NoClassDefFoundError: scala/Function0` after doing this. Apparently it doesn't set up the class path correctly. – Eric Ferreira Dec 23 '19 at 15:01
5

If you are using Maven you need to put META-INF directory under /main/resources folder instead of main/java.

Reference: Intellij - Nikolay Chashnikov

Then you can execute the runnable jar normally with java -jar filename.jar

another
  • 3,440
  • 4
  • 27
  • 34
3

You can these steps:

1) File -> Project Structure -> Project Settings -> Artifacts -> Jar -> From modules with dependencies

2) Check the Include in project build checkbox.

3) Right mouse click on module -> build module 'ModuleName'

Kirill
  • 131
  • 1
  • 13
-1

you could try using a gradle script. eclipse will make you a gradle project. you can use that build file with any ide. it has a built in jar task.

Ray Tayek
  • 9,841
  • 8
  • 50
  • 90