How to create executable JAR using Intellij ?
Hi,
I am using Intellij IDEA 12.1.3 on MAC OS X 10.8.3 to create an executable jar file.
I go to File -> Project Structure -> Artifacts -> + -> Jar -> From modules with dependencies -> Selected Main Class after browsing -> selected extract to the target jar -> Directory for META-INF automatically gets populated -> OK -> Apply -> OK -> Build -> Build Artifacts -> Build
Now If I try to run jar using:
java -jar path_to_jar/jar_file
I get this error
no main manifest attribute, in path_to_jar/jar_file
I check the MANIFEST.MF file in the jar file it indeed does not have Main-Class entry.
Using Maven by adding the <build> section to pom.xml sucesfully creates working jar. > mvn assembly:assembly
Thanks
Please sign in to leave a comment.
Hi Sergey,
Please provide a sample project with your artifact setup
Denis
If you're using Maven you need to put META-INF directory under main/resources folder instead of main/java. We already have an issue to suggest this
folder automatically (http://youtrack.jetbrains.com/issue/IDEA-99596).
--
Nikolay Chashnikov
Software Developer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"
Hi Denis,
Just create your own simple one file project HelloWorld and try to build JAR and execute it.
Thanks,
Edouard
Hi Nikolay,
I did as suggested - moved META-INF directory from main/java to main/resources.
After Building Artefact the JAR includes another than before MANIFEST.MF.
But it is identical to the original in main/resource/MATA-INF
has only two lines:
Manifest-Version: 1.0
Main-Class: HelloWorld
Attempt to run the JAR gives this error:
Exception in thread "main" java.lang.SecurityException: Invalid signature file digest for Manifest main attributes
at sun.security.util.SignatureFileVerifier.processImpl(SignatureFileVerifier.java:240)
Thanks
Hello, Sergey,
by default IDEA configures a single jar containing content of all jars from the dependencies of the module. Looks like some of these jars is signed so
it doesn't work when its content is extracted to another jar. You can try to select option 'Copy to the output directory and link via manifest' in
'Create Jar from Modules' dialog to see if it helps.
--
Nikolay Chashnikov
Software Developer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"
Hi Nikolay,
Thanks for your helpl, it works.
Sergey
Hello Nikolay,
I pointed 'Copy to the output directory and link via manifest' to the directory "main/resource", but it does not generate a JAR with proper MANIFEST.MF. This is the extracted file with missing Class/Main:
--------------
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven 3.3.9
Built-By: Lap127
Build-Jdk: 9.0.1
--------------
IntelliJ IDEA 2018.2.5 (Community Edition)
Build #IC-182.4892.20, built on October 16, 2018
JRE: 1.8.0_152-release-1248-b19 amd64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Windows 10 10.0
----------------
Thanks in advance!
Best regards
Gerrit
@Gerrit Leder
>Created-By: Apache Maven 3.3.9
tells that this manifest was not created by IDEA, rather than by Maven.
In Artifacts configuration dialog you should see the manifest attributes that will be used:
I have all the properties in IDEA for JAR and MANIFEST.MF and Hello world building and running in IDE is doing fine. But the maven3 created JAR is broken "kein Hauptmanifestattribut, ..."/"Main Manifest attribute missing".
Seems your maven3 integration is broken for IntelliJ IDEA:
Please provide sample project.
https://gitlab.com/gerrit.leder/hellotestkt
Please have a look!
I have corrected the project structure and the artifact:
Find the project here https://www.dropbox.com/s/99z2i01ph42eixm/hellotestkt.rar?dl=0
Thank you, but I wonder how you build the jar. This is what `mvn install` creates:
Do you have a screenshot for building, please?
> I wonder how you build the jar.
Build | Build Actifacts... action, see Building artifacts.
ok, JAR has been build. But when I change my original project with the changes in your screenshots, then I have this error:
C:\Users\Lap127\IdeaProjects\deauticonkotlin>java -Dfile.encoding=windows-1252 -jar out\artifacts\deauticonkotlin_jar\de
auticonkotlin.jar
Exception in thread "main" java.lang.NoClassDefFoundError: kotlin/jvm/internal/Intrinsics
at de.auticon.kotlin.HelloKt.main(Hello.kt)
Caused by: java.lang.ClassNotFoundException: kotlin.jvm.internal.Intrinsics
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 1 more
What additional changes did you make to the project in order to prevent this?
Check the project by Dropbox link that I gave - it runs the jar just fine.
ok, thank you!
I try to build the jar in a GUI project and the manifest has main class, as well as the jar has the main class
de.auticon.maven.tornadofx.MainAppKt
But the jar cannot execute it:
-----------------------
C:\Users\Lap127\IdeaProjects\deauticonmaventornadofx\out\artifacts\deauticonmaventornadofx_jar>java -cp . -jar deauticon
maventornadofx.jar
Fehler: Hauptklasse de.auticon.maven.tornadofx.MainAppKt konnte nicht gefunden oder geladen werden
------------------------
Please have a look!
https://drive.google.com/file/d/1VvVEFBbxPLSHJHQweH96Qz3MIWy003C-/view?usp=sharing
Use Jar Run/Debug Configuration as on my screenshot (hellotestkt.jar is the name on configuration) to run it. Check also the command line that is used to run it.
Sorry, cannot find the error...