Reputation: 230276
I have a project that contains a single module, and some dependencies. I'd like to create a JAR, in a separate directory, that contains the compiled module. In addition, I would like to have the dependencies present beside my module.
No matter how I twist IntelliJ's "build JAR" process, the output of my module appears empty (besides a META-INF
file).
Upvotes: 680
Views: 949838
Reputation: 845
Please follow these steps from your IDE( IntelliJ IDEA):
File -> Project Structure -> Project Settings -> Artifacts -> Click on + (plus sign) -> Jar -> Select option 'From modules with dependencies...'
Select a Main Class (or Application class) if you need to make the jar runnable.
Select Extract to the target Jar
Click OK
Click Apply/OK
Now click on build menu from Menu bar and follow these steps:
Build -> Build Artifact -> Build
Upvotes: 0
Reputation: 73
If you're using the new beta IntelliJ UI, the above solutions will require modification.
Currently, in the new beta IntelliJ UI, there is a bug where if you click on the "build" button in the build artifacts menu, nothing will happen.
However, if you use arrow keys to navigate to the "build" button, and then press "enter" on your keyboard, it will work.
or you can do the following:
You have to shift+shift type [Build Artifacts...] > then use arrow keys to navigate build artifact floating menu and hit enter.
Bug tracker: https://youtrack.jetbrains.com/issue/IDEA-316889/Build-artifacts-inactive-if-hamburger-menu-is-on
Hopefully this saves someone from the hour of pain I experienced trying to get my JAR file to be built.
Upvotes: 0
Reputation: 15559
File -> Project Structure -> Project Settings -> Artifacts -> Click + (plus sign) -> Jar -> From modules with dependencies...
Select a Main Class (the one with main()
method) if you need to make the jar runnable.
Select Extract to the target Jar
Click OK
Click Apply/OK
The above sets the "skeleton" to where the jar will be saved to. To actually build and save it do the following:
Build -> Build Artifact -> Build
Try Extracting the .jar file from:
📦ProjectName
â”— đź“‚out
â”— đź“‚artifacts
â”— đź“‚ProjectName_jar
â”— đź“śProjectName.jar
Upvotes: 832
Reputation: 411
I tried most of the given answers, but in my case "out" folder (which contains jar file) was not getting created. So this did the job :
Build -> Build Artifacts -> (you will see the class name) -> Build
Now, "out" folder was created and jar file was in it.
Upvotes: 0
Reputation: 69
Using
mvn clean package spring-boot:repackage
type above command inside IntelliJ Terminal. It generates a Target file with Executable Jar.
Upvotes: 2
Reputation: 163
Use Gradle to build your project, then the jar is within the build file. Here is a Stack Overflow link on how to build .jar files with Gradle.
Java project with Gradle and building jar file in Intellij IDEA - how to?
Upvotes: 0
Reputation: 456
In case you are reading this answer because you are facing "resource file not found" error, try this:
Output Layout
, press Create Archive
to create a new jar, and again, give it a nice name ;)Manifest File
then set correct Main Class
and Class Path
.Add Library Files
, and select libraries you need (you can use Ctrl+A to select all).Upvotes: 0
Reputation: 547
Here are 2 examples with maven project, step by step:
at pom.xml, add maven-jar-plugin.
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>somePackage.sample</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
screen capture:
4. open maven project box by click on the search icon and type maven,
test your jar using java -jar
The MANIFEST.MF needs to be in your resources folder and the Main.Class needs to refer to {package}-{class-name-that-contains-main-class}
Upvotes: 38
Reputation: 4165
For those that benefit from images as I do:
File -> Project Structure
Upvotes: 51
Reputation: 2635
As the people above says, but I have to note one point. You have to check the checkbox:
Include in project build
Upvotes: 3
Reputation: 3057
I was trying to build a jar from a multi-module Kotlin app and was getting the notorious 'no main manifest attribute' error. Creating the manifest directory in src/main/resources
didn't work either.
Instead, it works when creating it in src
directly: src/META-INF/MANIFEST.MF
.
Upvotes: 5
Reputation: 786
My problem was this: I used Intellij IDEA (tried different versions) + Gradle. When I compiled the project and builded artifacf jar, as indicated in the above responses, I received an error - "no main manifest attrubute ..."
This solution worked for me (special thanks to Thilina Ashen Gamage (see above) for tip):
Excerpt - if you use external libraries and build a project through Project Settings - Artifacts - Add (+) - Jar - From modules with dependencies, then probably because of a program bug the META-INF folder with MANIFEST_MF file not including to jar. To avoid it create EMPTY jar file.
Project Settings - Artifacts - Add (+) - Jar - EMPTY JAR. META-INF folder will added to resources folder. Then select your main class. You will see following jar structure:
Note the presence of a folder META-INF
Then you can build your project and build artifacts. This solution worked for javaFX applications too.
Upvotes: 1
Reputation: 3520
When i use these solution this error coming:
java -jar xxxxx.jar
no main manifest attribute, in xxxxx.jar
and solution is:
You have to change manifest directory:
<project folder>\src\main\java
change java to resources
<project folder>\src\main\resources
Upvotes: 7
Reputation: 2431
Here is the official answer of IntelliJ IDEA 2018.3 Help. I tried and It worked.
To build a JAR file from a module;
On the main menu, choose Build | Build Artifact.
From the drop-down list, select the desired artifact of the type JAR. The list shows all the artifacts configured for the current project. To have all the configured artifacts built, choose the Build all artifacts option.
Upvotes: 1
Reputation: 432
If you are working on spring/mvn project you can use this command:
mvn package -DskipTests
The jar file will be saved on target directoy.
Upvotes: -3
Reputation: 3616
This is still an issue in 2017, I hope it will help somebody out there! I found 2 possibilities to create working jar-s under IntelliJ 2017.2
1. Creating artifact from IntelliJ:
You have to change manifest directory:
<project folder>\src\main\java
replace "java" with "resources"
<project folder>\src\main\resources
This is how it should look like:
Then you choose the dependencies what you want to be packed IN your jar, or NEAR your jar file
To build your artifact go to build artifacts and choose "rebuild". It will create an "out" folder with your jar file and its dependencies.
2. Using maven-assembly-plugin
Add build section to the pom file
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<finalName>ServiceCreate</finalName>
<appendAssemblyId>false</appendAssemblyId>
<archive>
<manifest>
<mainClass>com.svt.optimoo.App</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
This procedure will create the jar file under the "target" folder
Upvotes: 384
Reputation: 1038
In case you are trying to build a jar with kotlin you need to create a src/main/java
folder and use this folder as a location for the META-INF folder.
Upvotes: 0
Reputation: 62090
Some of the other answers are useless because as soon as you re-import the IntelliJ IDEA project from the maven project, all changes will be lost.
The building of the jar needs to be triggered by a run/debug configuration, not by the project settings.
Jetbrains has a nice description of how you can accomplish this here:
https://www.jetbrains.com/help/idea/maven.html
Scroll down to the section called "Configuring triggers for Maven goals".
(The only disadvantage of their description is that their screenshots are in the default black-on-white color scheme instead of the super-awesome darcula theme. Ugh!)
So, basically, what you do is that you open the "Maven Projects" panel, you find the project of interest, (in your case, the project that builds your jar,) underneath it you find the maven goal that you want to execute, (usually the "package" goal creates jars,) you open up the context menu on it, (right-click on a Windows machine,) and there will be an "Execute before Run/Debug..." option that you can select and it will take you by the hand from there. Really easy.
Upvotes: 0
Reputation: 1735
If you are using third party libraries with your project or if you have problems with creating MANIFEST.MF file properly, there can be conflicts when running JAR files generated using
File > Project Structure > Artifacts > '+' > JAR > From modules with dependencies > .....
method mentioned above.
Instead I suggest you to create an empty JAR and add all other elements to the output root manually. A wonderful blog article for this method can be found here: http://karthicraghupathi.com/2016/07/10/creating-an-executable-jar-in-intellij-idea/ I tried the steps mentioned there and everything worked fine for me!
Upvotes: 0
Reputation: 111
With Maven you can use this plugin:
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>[path you class main]</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Upvotes: 2
Reputation: 33
I recently had this problem and think these steps are easy to follow if any prior solution or link is missing detail.
How to create a .jar
using IntelliJ IDEA 14.1.5:
Upvotes: 153
Reputation: 1056
It is probably little bit late, but I managed to solve it this way -> open with winrar and delete ECLIPSEF.RSA and ECLIPSEF.SF in META-INF folder, moreover put "Main-class: main_class_name" (without ".class") in MANIFEST.MF. Make sure that you pressed "Enter" twice after the last line, otherwise it won't work.
Upvotes: 6
Reputation: 7257
You might want to take a look at Maven (http://maven.apache.org). You can use it either as the main build process for your application, or just to perform certain tasks through the Edit Configurations dialog. The process of creating a JAR of a module within Maven is fairly trivial, if you want it to include all the dependencies in a self-executable JAR that is trivial as well.
If you've never used Maven before then you want to read Better Builds With Maven.
Upvotes: 0
Reputation: 6450
Ant and Maven are widely used. I prefer Ant, I feel it's more lightweight and you the developer are more in control. Some would suggest that's its downside :-)
Upvotes: -4
Reputation: 1436
Idea 8.1.3
Jar is ok, since there is compiled output in 'output' directory (project/out/production//)
I guess, you have to run 'make' before building jar
for dependencies just check "show library" and choose what you want.
Upvotes: 1