First, it’s kind of weird, to see you run java -jar "app"
and not java -jar app.jar
Second, to make a jar executable… you need to jar a file called META-INF/MANIFEST.MF
the file itself should have (at least) this one liner:
Main-Class: com.mypackage.MyClass
Where com.mypackage.MyClass
is the class holding the public static void main(String[] args) entry point.
Note that there are several ways to get this done either with the CLI, Maven, Ant or Gradle:
For CLI, the following command will do: (tks @dvvrt)
jar cmvf META-INF/MANIFEST.MF <new-jar-filename>.jar <files to include>
For Maven, something like the following snippet should do the trick. Note that this is only the plugin definition, not the full pom.xml:
Latest doc on this plugin: see https://maven.apache.org/plugins/maven-jar-plugin/
<build>
<plugins>
<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>com.mypackage.MyClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
(Pick a <version>
appropriate to your project.)
For Ant, the snippet below should help:
<jar destfile="build/main/checksites.jar">
<fileset dir="build/main/classes"/>
<zipfileset includes="**/*.class" src="lib/main/some.jar"/>
<manifest>
<attribute name="Main-Class" value="com.acme.checksites.Main"/>
</manifest>
</jar>
Credits Michael Niemand —
For Gradle:
plugins {
id 'java'
}
jar {
manifest {
attributes(
'Main-Class': 'com.mypackage.MyClass'
)
}
}
Every executable jar file in a Java application should contain a main method. It is usually placed at the beginning of the application. Manifest files must be included with self-executing jars, as well as being wrapped in the project at the appropriate location, to run a main method. Manifest files have a main attribute that specifies the class having the main method.
“no main manifest attribute” is a common error which happens when we try to run an executable jar
file. The full error output may look like what’s shown below
Unable to execute jar- file: "no main manifest attribute."
Code language: JavaScript (javascript)
Or
no main manifest attribute, in target/exec.jar
In this article, we will show you a few possible fix that you can apply to your Java project to avoid getting “no main manifest attribute” error.
“no main manifest attribute” error message is thrown because of various reasons, but most of the time, it fall under one of the following category.
- Missing entry point of Main-Class in MANIFEST.MF file.
- Missing Maven dependency in
pom.xml
- Missing entry point in
build.gradle
The Main, or Main-Class is an essential attribute to make your jar
executable. It tells Java which class would be used as the entry point of the application.
Without a Main
, or Main-Class
attribute, Java have no way to know which class it should run when you execute the jar
.
Inside the jar
file, the MANIFEST.MF
file is located in META-INF
folder. Opening the jar
file with WinRAR, you would see the contents of it, along with MANIFEST.MF
.
A typical MANIFEST.MF
file should contain the following lines
Manifest-Version: the version of the Manifest file.
Built-By: your PC name.
Build-Jdk: the JDK version installed in your machine.
Created-By: the plugin name used in IDE.
Code language: HTTP (http)
Below is an example of a MANIFEST.MF
file.
Putting maven-jar-plugin in pom.xml
The “no main manifest attribute” error message may occur in a Maven project due to the absence of the Main-Class entry in MANIFEST.MF.
This issue can be resolved by adding maven-jar-plugin
to our pom.xml
file.
<build>
<plugins>
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<mainClass>com.linuxpip.AppMain</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
Code language: HTML, XML (xml)
In the code snippet above, com.linuxpip.AppMain
is our fully-qualified name of the Main-Class. You have to change this according to your specific project.
If you need more information, dig deep into maven-jar-plugin
documentation: see https://maven.apache.org/plugins/maven-jar-plugin/
Specify Main-Class in Gradle
The following entries can be put into your build.gradle
file if you receive this error in your Gradle project:
plugins {
id 'java'
}
jar {
manifest {
attributes(
'Main-Class': 'com.linuxpip.MyClass'
)
}
}
Code language: JavaScript (javascript)
In the code snippet above, com.linuxpip.MyClass
is our fully-qualified name of the Main-Class. You have to change this according to your specific project.
Change default MANIFEST.MF folder in IntelliJ IDEA
People have been reporting that IntelliJ IDEA keeps putting the JAR artifact into the wrong folder.
In order to fix “no main manifest attribute” in IntelliJ IDEA, choose JAR > From modules with dependencies
In the Create JAR from Modules window, change the default Directory for META-INF/MANIFEST.MF path from <project folder>srcmainjava
to <project folder>srcmainresources
.
Otherwise it would generate the manifest and including it in the jar, but not the one in <project folder>srcmainjava
that Java expects.
After that, just continue to Build Artifacts as you usually do.
Specify entry point in Eclipse
If you’re exporting the JAR file in Eclipse, it has a built-in option that allows you to specify the Application’s entry point, avoiding “no main manifest attribute” error message.
In Eclipse’s JAR Export window, you would see “Select the class of the application entry point” near the end of the Export process. Now you can pick a class and Eclipse will automatically generate the proper MANIFEST.MF
file with the settings you’ve set.
I’m building a JAR file with Gradle. When I try to run it I get the following error
no main manifest attribute, in RxJavaDemo.jar
I tried manipulating the manifest
property but I think I’m forgetting to add the dependencies or something to it. What exactly am I doing wrong?
apply plugin: 'java'
apply plugin: 'application'
mainClassName = 'demo.MainDashboard'
dependencies {
compile files ("H:/Processes/Development/libraries/hikari-cp/HikariCP-2.4.1.jar")
compile files ("H:/Processes/Development/libraries/controls-fx/controlsfx.jar")
compile files ("H:/Processes/Development/libraries/database_connections/sqlite-jdbc-3.8.6.jar")
compile files ("H:/Processes/Development/libraries/guava/guava-18.0.jar")
compile files ("H:/Processes/Development/libraries/rxjava/rxjava-1.0.12.jar")
compile files ("H:/Processes/Development/libraries/rxjava-extras/rxjava-extras-0.5.15.jar")
compile files ("H:/Processes/Development/libraries/rxjavafx/RxJavaFX-1.0.0-RC1-SNAPSHOT.jar")
compile files ("H:/Processes/Development/libraries/rxjavaguava/rxjava-guava-1.0.3.jar")
compile files ("H:/Processes/Development/libraries/rxjava-jdbc/rxjava-jdbc-0.6.3.jar")
compile files ("H:/Processes/Development/libraries/slf4j/slf4j-api-1.7.12.jar")
compile files ("H:/Processes/Development/libraries/tom-commons/tom-commons.jar")
}
sourceSets {
main.java.srcDir "src/main/java"
main.resources.srcDir "src/main/resources"
}
jar {
manifest {
attributes(
"Class-Path": configurations.compile.collect { it.getName() }.join(' '))
}
from configurations.compile.collect { entry -> zipTree(entry) }
}
Mahozad
17.1k13 gold badges114 silver badges127 bronze badges
asked Sep 14, 2015 at 14:18
Try to change your manifest attributes like:
jar {
manifest {
attributes(
'Class-Path': configurations.compile.collect { it.getName() }.join(' '),
'Main-Class': 'hello.HelloWorld'
)
}
}
And then just change 'hello.helloWorld'
to '<your packagename>.<the name of your Main class>'
(where your Main class has a main method). In this case, you make in your manifest an attribute, which point to this class, then a jar is running.
a.t.
1,7863 gold badges24 silver badges63 bronze badges
answered Sep 14, 2015 at 14:35
StanislavStanislav
27.3k9 gold badges87 silver badges82 bronze badges
6
To make the jar
file executable (so that the java -jar
command works), specify the Main-Class
attribute in MANIFEST.MF
.
In Gradle, you can do it by configuring the jar
task.
- for Groovy DSL see these answers ([1], [2])
- for Kotlin DSL you can use the following code snippet:
tasks.withType<Jar> {
manifest {
attributes["Main-Class"] = "com.caco3.Main"
}
}
Why mainClassName
does not work as expected?
Or why mainClassName
does not specify the attribute in the manifest?
The mainClassName
property comes from the application
plugin. The plugin:
makes it easy to start the application locally during development, and to package the application as a TAR and/or ZIP including operating system specific start scripts.
So the application
plugin does not aim at producing executable jar
s
When a mainClassName
property set, then:
$ ./gradlew run
will launch themain
method in the class specified in the attribute- the
zip
/tar
archive built usingdistZip
/distTar
tasks will contain a script, which will launch themain
method of the specified previously class.
Here is the line of shell script setting the main class:
$ grep Main2 gradletest
eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLETEST_OPTS -classpath ""$CLASSPATH"" com.caco3.gradletest.Main2 "$APP_ARGS"
answered Sep 6, 2020 at 18:10
Denis ZavedeevDenis Zavedeev
7,3924 gold badges31 silver badges53 bronze badges
2
To complement Denis Zavedeev answer, here are more ways for Kotlin DSL (build.gradle.kts):
tasks.jar {
manifest.attributes["Main-Class"] = "com.example.MyMainClass"
}
Another notation:
tasks.jar {
manifest {
attributes["Main-Class"] = "com.example.MyMainClass"
}
}
Side note: to create a runnable fat JAR (also called uber JAR), see this post.
answered Feb 12, 2022 at 14:39
MahozadMahozad
17.1k13 gold badges114 silver badges127 bronze badges
3
FWIW — I used the following jar task to assemble all my compile dependencies into the jar file, and used the above recommendation to get the class-path properly set
apply plugin: 'java-library'
jar {
manifest {
attributes(
'Class-Path': configurations.compile.collect { it.getName() }.join(' '),
'Main-Class': 'your.main.class.goes.here'
)
}
// You can reference any part of the dependency configurations,
// and you can have as many from statements as you need
from configurations.compile
// I just copied them into the top of the jar, so it looks like the eclipse exported
// runnable jar, but you could designate a lib directory, and reference that in the
// classpath as "lib/$it.name" instead of it.getName()
into ''
}
answered May 6, 2019 at 16:17
In this post, we will see how to solve Unable to execute jar- file: “no main manifest attribute”.
Table of Contents
- Problem
- Solution
- Maven
- Spring boot application
- Gradle
- Maven
- Root cause
Problem
When you have a self-executable jar and trying to execute it. You might get this error.
Unable to execute jar- file: “no main manifest attribute”
Solution
Maven
You might get this error when Main-Class
entry is missing in MANIFEST.MF file. You can put maven-jar-plugin
plugin in pom.xml
to fix it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<build> <plugins> <plugin> <!— Build an executable JAR —> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.1.0</version> <configuration> <archive> <manifest> <mainClass>org.arpit.java2blog.AppMain</mainClass> </manifest> </archive> </configuration> </plugin> </plugins> </build> |
org.arpit.java2blog.AppMain
is a fully qualified name of main class. You need to replace it with your application’s main class.
Run mvn clean install
and then execute the jar file as below.
java —jar AppMain—0.0.1—SNAPSHOT.jar |
AppMain-0.0.1-SNAPSHOT.jar
is application jar that you want to run.
Spring boot application
In case, you are getting an error while running spring boot application, you can solve it with spring-boot-maven-plugin
.
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.0.1.RELEASE</version> </plugin> </plugins> </build> |
Gradle
In case you are using gradle, you can solve this with following entry.
plugins { id ‘java’ } jar { manifest { attributes( ‘Main-Class’: ‘org.arpit.java2blog.AppMain’ ) } } |
Please replace org.arpit.java2blog.AppMain
with your main class.
Root cause
When you run self-executable jar, java will look for the Main-Class
in MANIFEST.MF file located under META-INF folder. If it is not able to find an entry,then it will complain with Unable to execute jar- file: “no main manifest attribute”
.
MANIFEST.MF contains information about files contained in the Jar file.
Manifest—Version: 1.0 Built—By: Arpit Mandliya Build—Jdk: 1.8.0_101 Created—By: Maven Integration for Eclipse |
Once you run the above-mentioned solution and reopen MANIFEST.MF
file in jar again, you will see Main-Class entry.
Manifest—Version: 1.0 Built—By: Arpit Mandliya Build—Jdk: 1.8.0_101 Created—By: Maven Integration for Eclipse Main—Class: org.arpit.java2blog.AppMain |
This should solve Unable to execute jar- file: “no main manifest attribute”. Please comment in case you are still facing the issue.
In a Java project, every executable jar file contains a main method. Usually, it is placed at starting point of the application. To execute a main method by a self-executing jar file, we must have a proper manifest file and wrap it with our project at the proper location. These files have a main manifest attribute that is used to define the path to class having the main method.
Sometimes, we have a self-executable jar, and when we try to execute the project, it will throw the following error message:
The above error message is thrown because of a missing entry of Main-Class in MANIFEST.MF file.
You may encounter this problem in any configuration-based Java project. Let’s discuss its solutions:
Solution:
Let’s understand its solutions in different projects:
Maven
In Maven Project, this problem may encounter because of the missing entry of Main-Class in MANIFEST.MF file. To overcome this issue, we may define it in our pom.xml file by manually putting a maven-jar-plugin in pom.xml.
In the above code, com.javatpoint.AppMain is our fully qualified name of the Main-Class. You have to replace the fully qualified name according to your classpath. To copy the fully qualified name, right-click on the package that contains the main class and select Copy Fully Qualified Name.
Gradle
If you are getting this error in your Gradle project, you can solve this by putting the following entries in your build.gradle file:
Spring Boot Application
Sometimes this error may be encountered in your Spring Boot project. It is easy to resolve this error in the Spring Boot project. To fix this error in the SB project, put the maven-plugin dependency under the <plugins> tag in the pom.xml file.
Root Cause of the No Main Manifest Attribute Error
The «no main manifest error» is usually caused if you messed in MANIFEST.MF file located under META-INF folder. By default, it has the following entry:
Where,
Manifest-Version is the version of the Manifest file.
Built-By is your PC name.
Build-Jdk is the JDK version installed in your machine.
Created-By is the plugin name used in IDE.
When we run the project, it will look for the Main-Class in MANIFEST.MF file. It will throw the error message «Unable to execute jar-file» if it does not find the entry. When we run the above solutions, the class entry will automatically be updated in the project’s Manifest file as follows: