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'
)
}
}
пытаюсь собрать проект с помощью maven, выдается ошибка no main manifest attribute, in first_project.jar.
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>first_project</groupId>
<artifactId>first_project</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>firstProject</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<mainClass>deleveryClub.MainApp</mainClass>
</properties>
<organization>
<!-- Used as the 'Vendor' for JNLP generation -->
<name>Your Organisation</name>
</organization>
<dependencies>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.openjfx/javafx-controls -->
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>12</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.openjfx/javafx-base -->
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-base</artifactId>
<version>11.0.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.openjfx/javafx-graphics -->
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-graphics</artifactId>
<version>11</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>11</release>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>deleveryClub.MainApp</mainClass>
</configuration>
</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>deleveryClub.MainApp</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
ссылка на проект: https://github.com/CptWasp/vk-tester-selenium
-
Вопрос заданболее трёх лет назад
-
24795 просмотров
Здравствуйте!
Скажите пожалуйста, каким IDE вы собираете проект? Если Intellij IDEA, то мне эта проблема уже знакома…
Не раз натыкался на нее… В общем, когда вы собираете jar, то почему-то intellij idea если выбрать create jar from modules & dependencies создает jar некорректно. Эта проблема актуально до последней версии. И соответственно, в jar не попадает папка META-INF И файл MANIFEST.MF
Чтобы это исключить создайте jar — empty
Сама ошибка говорит о том, что в jar файле у вас нет вышеуказанного файла и папки. Тогда заработает корректно.
Также, если это maven || gradle, то положите папку meta-inf на уровень проекта, либо в папку resources. Не в java!
Вот, тут мой ответ по данному вопросу (со скриншотом) — https://stackoverflow.com/questions/1082580/how-to…
Для решения нужно изменить путь до папки с manifest`ом
srcmainjava -> srcmainresources
Картинка
Пригласить эксперта
Вообще-то! Для сведения: при ручной сборке JARа, из cmd строкой:
…>javac -cp «.;test.jar» prog.java,
JAR потом запускается в cmd вот такой строкой: ….>java -cp «.;test.jar» prog
Если же набить команду: ….>java -jar test.jar, то не запустится, И НЕ ДОЛЖЕН!
cmd выдаст такой же результат — no main manifest attribute, хотя, если раззиповать JARник, файл манифеста В НЁМ ЕСТЬ!
-
Показать ещё
Загружается…
13 июн. 2023, в 16:13
8500 руб./за проект
13 июн. 2023, в 15:58
400 руб./за проект
13 июн. 2023, в 15:58
400 руб./за проект
Минуточку внимания
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.
You’ve built a Java project and packaged a .jar file.
You are ready to run it outside your IDE, or deploy it somewhere.
When you run it using java -jar <your.jar>
it says something like this:
no main manifest attribute, in /src/project/target/your-jar-1.0-SNAPSHOT.jar
How to add a Manifest file
You need to create a META-INF/MANIFEST.MF
file with the following content:
Main-Class: com.mypackage.MyClass
The class name com.mypackage.MyClass
is the main class that has the public static void main(String[] args) entry point in your application.
If you’re using Maven
If using Maven, then you will need to add this to your POM.xml:
<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>
If you’re using Gradle
Likewise, if you’re using Gradle, then use this:
plugins {
id 'java'
}
jar {
manifest {
attributes (
'Main-Class': 'com.mypackage.MyClass'
)
}
}
If you didn’t build an Executable Jar
Then you can run it like this:
java -cp app.jar com.mypackage.MyClass
If all else fails: Use this Maven Single Goal
If all else has failed, then you can always use this Maven goal to get the job done:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.mypackage.MyClass</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
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.