Can't execute jar- file: "no main manifest attribute"

1,968,695

Solution 1

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'
        )
    }
}

Solution 2

That should have been java -jar app.jar instead of java -jar "app".

The -jar option only works if the JAR file is an executable JAR file, which means it must have a manifest file with a Main-Class attribute in it. See Packaging Programs in JAR Files to learn how to create an executable JAR.

If it's not an executable JAR, then you'll need to run the program with something like:

java -cp app.jar com.somepackage.SomeClass

where com.somepackage.SomeClass is the class that contains the main method to run the program. (What that class is depends on the program, it's impossible to tell from the information you've supplied).

Solution 3

Alternatively, you can use maven-assembly-plugin, as shown in the below example:

<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.package.MainClass</mainClass>
        </manifest>
      </archive>
      <descriptorRefs>
        <descriptorRef>jar-with-dependencies</descriptorRef>
      </descriptorRefs>
    </configuration>
  </plugin> 

In this example all the dependency jars as specified in section will be automatically included in your single jar. Note that jar-with-dependencies should be literally put as, not to be replaced with the jar file names you want to include.

Solution 4

That is because Java cannot find the Main attribute in the MANIFEST.MF file. The Main attribute is necessary to tell java which class it should use as the application's entry point. Inside the jar file, the MANIFEST.MF file is located in META-INF folder. Wondering how you could look at what's inside a jar file? Open the jar file with WinRAR.

The main attribute inside the MANIFEST.MF looks like this:

Main-Class: <packagename>.<classname>

You get this "no main manifest attribute" error when this line is missing from the MANIFEST.MF file.

It's really a huge mess to specify this attribute inside the MANIFEST.MF file.

Update: I just found a really neat way to specify the Application's entry point in eclipse. When you say Export,

Select Jar and next 

[ give it a name in the next window ] and next

and next again

and you'll see " Select the class of the application entry point".

Just pick a class and Eclipse will automatically build a cool MANIFEST.MF for you.

enter image description here

Solution 5

I had the same issue. by adding following lines to pom file made it work. The plugin will make sure the build process of your application with all necessary steps.

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
Share:
1,968,695
Ewoud
Author by

Ewoud

Updated on July 08, 2022

Comments

  • Ewoud
    Ewoud almost 2 years

    I have installed an application, when I try to run it (it's an executable jar) nothing happens. When I run it from the commandline with:

    java -jar "app.jar"

    I get the following message:

    no main manifest attribute, in "app.jar"

    Normally, if I had created the program myself, I would have added a main class attribute to the manifest file. But in this case, since the file is from an application, i cannot do that. I also tried extracting the jar to see if I could find the main class, but there are to many classes and none of them has the word "main" in it's name. There must be a way to fix this because the program runs fine on other systems.

    • Dave Newton
      Dave Newton about 12 years
      Look for main methods; you can't rely on class names.
    • Ewoud
      Ewoud about 12 years
      I know, but since I only have .class files I can't really see the methods. Or can I?
    • Dave Newton
      Dave Newton about 12 years
      You aren't really typing the quotes, are you? In any case, there are a number of ways to see methods, include using javap. You might want to un-jar it and look to see if there's actually no manifest, though.
    • weston
      weston over 8 years
      Related: with dependencies: stackoverflow.com/a/23986765/360211
    • Kamini
      Kamini almost 7 years
      what if I don't have main class as I am running the code using CommandLineJobRunner
    • MrMesees
      MrMesees about 6 years
      I had this problem with a library that had worked previously, simply because I was missing execution declaration steps...
    • abdul rashid
      abdul rashid almost 4 years
      Solution involves adding META-INF to resources directory and few other steps explained here youtube.com/watch?v=wPGSas_f0ts
    • Dimitri Kopriwa
      Dimitri Kopriwa over 3 years
      I have a task called buildRepackage , I think this is what does the task but I can't make a jar to be runnable with the whole dependency. Do you know how they work together and how I can get this done for a spring boot application?
    • CausingUnderflowsEverywhere
      CausingUnderflowsEverywhere almost 3 years
      Hey @Ewoud I noticed you haven't selected an answer as the solution. You may have forgotten to so I'm just letting you know.
    • AO_
      AO_ over 2 years
      The easiest way is to just change the Maven/Gradle settings directly
    • Ewoud
      Ewoud over 2 years
      @CausingUnderflowsEverywhere I honestly don't remember if or how I solved the issue all those years ago. To just pick an answer now, without knowing if it solved my problem would be unethical in my opinion. I think that there's a lot of useful information here for people that are facing the same or similar problem, and I hope the rating system will help them find the right answer.
    • Caffeine Coder
      Caffeine Coder about 2 years
      The plugin config mentioned in the answer here would help- stackoverflow.com/questions/54867295/…
  • Ewoud
    Ewoud about 12 years
    thanks for your reply, but your solution only works if I know the name of the class that contains the main method. And it was a typo... It was supposed to be "app.jar". But how do you explain why it runs on other systems by just double clicking the file?
  • Jesper
    Jesper about 12 years
    If it is indeed an executable JAR, you can extract the manifest file (it's in the META-INF directory inside the JAR file). It should contain a Main-Class attribute that gives you the name of the main class.
  • Jesper
    Jesper about 12 years
    If it doesn run on one system, then that system maybe has a too old Java version. If the JAR is for example compiled with Java 7, then you can't run it on a system that has Java 6 or older.
  • Ewoud
    Ewoud about 12 years
    That's funny since the other system is running win7 and this pc with the problems runs win8.
  • Jesper
    Jesper about 12 years
    Is Java officially supported on Windows 8?
  • Michael Niemand
    Michael Niemand over 11 years
    In Ant its <manifest><attribute name="Main-Class" value="com.mypackage.MyClass"/></manifest> within the <jar> element
  • Paolo
    Paolo about 11 years
    +1 for the Jar File Specification link (docs.oracle.com/javase/1.4.2/docs/guide/jar/jar.html)
  • Ferran Maylinch
    Ferran Maylinch almost 10 years
    Thank you. Just wanted to add that you can copy dependencies to the lib folder using this: stackoverflow.com/a/996915/1121497. Since the classpath includes that lib folder then you only need to execute the jar with java -jar myproject.jar and it will find the dependencies.
  • Ogen
    Ogen almost 10 years
    @Jesper Hello, what if eclipse is using the default package? Do I just put the class name?
  • Jesper
    Jesper almost 10 years
    Yes, if the class is in the default package, then just specify the class name without a package name. However, it's good practice to always put your code in a package.
  • Wicelo
    Wicelo over 9 years
    HOW TO "jar a file called META-INF/MANIFEST.MF" ? I have a .jar on one hand and a .MF on the other, how do I link them together ? I put the manifest in the same folder as the .jar but it doesn't work I still got the problem !
  • Olivier Refalo
    Olivier Refalo over 9 years
    I think that's a different question - but a jar is actually a zip. Just use your best zip tool to add the file in the right location.
  • dvvrt
    dvvrt over 9 years
    @Wicelo To specify a specific MANIFEST.MF file while creating a jar file use the m flag for jar. eg. jar cmvf META-INF/MANIFEST.MF <new-jar-filename>.jar <files to include>
  • Olivier Refalo
    Olivier Refalo over 9 years
    thank you, will edit my answer. Are you sure about this command? it looks weird: a file, a destination... then files again.
  • Paul
    Paul almost 9 years
    Perfect, this works. It bundles all dependencies in to one jar, thus allowing you to compile/build a project and run it out of the box.
  • Gaurav Khare
    Gaurav Khare about 8 years
    One way is to include main class in pom.xml and use java -jar command, other one is to use java -cp command.
  • Dan Randolph
    Dan Randolph almost 8 years
    Note that it is useful to use 7-zip to open the "app.jar" file to find main class. For example, I downloaded tcpmon-1.1.jar from the Google Code Archive. MainWindow.class is located in com/codegoogle/tcpmon. Thus cmd: "java -cp tcpmon-1.1.jar com.codegoogle.tcpmon.MainWindow" opended tcpmon main window in Windows 10.
  • Jesper
    Jesper almost 8 years
    @DanRandolph A JAR file is indeed a ZIP file, so you can open and inspect JAR files with programs like 7-Zip or any other tool that can read ZIP files.
  • lsrom
    lsrom over 7 years
    This worked for me with IDEA 14.1.6. I also added the build property for pom.xml but it had no effect. But your answer solved it, thank you.
  • user4321
    user4321 about 7 years
    *usually named as maven so prefix would be maven <classpathPrefix>maven/</classpathPrefix>
  • Ratata Tata
    Ratata Tata over 6 years
    I did it to run a jar listed in a jnlp file (xml). I found the main-class xml tag and it's running fine now.
  • koppor
    koppor over 6 years
    In the context of the shade plugin, one has to follow the help at Executable JAR.
  • james.garriss
    james.garriss over 6 years
    Who said this was a Spring Boot project?
  • lasec0203
    lasec0203 over 6 years
    the simplest answer thus far.
  • lasec0203
    lasec0203 over 6 years
    for those dealing with android studio. see @QED answer stackoverflow.com/a/37127374/2445763
  • Raystorm
    Raystorm over 6 years
    I found this worked but I had to execute as mvn package shade:shade just running mvn package didn't trigger the shade plugin to run.
  • kevlarr
    kevlarr over 6 years
    This helped me! gradle tutorials had specified using the top-level mainClassName variable set, but that only helps with gradle run command, not with creating an executable .jar
  • Vishrant
    Vishrant over 6 years
    @james.garriss well I would say, I came to this post searching for no main manifest attribute error but I am working on spring boot application so this answer helped me. I already knew how to create META-INF/MANIFEST.MF file, but not how to make spring-boot automatically handles it.
  • Vishrant
    Vishrant over 6 years
    I don't see any reason for downvoting this answer. If you are not using spring-boot just ignore it. Stackoverflow also helps to build your own issue repository which you face while programming.
  • MarkHu
    MarkHu over 6 years
    This sounds promising. How exactly do you add JarLauncher to the classpath?
  • Bane
    Bane about 6 years
    @Vishrant I don't know about others but I downvoted because this answer does not address the question as it was posed. It may have just happened to answer your question, but your question wasn't really what was asked.
  • Vishrant
    Vishrant about 6 years
    @Bane sure. but the question can be taken in a broad sense and this answer applies to it and can help others in the sense when they will use spring boot.
  • user207421
    user207421 about 6 years
    It isn't always the first class. Hardly ever. Command line should be java -cp ... somepackage.App.
  • user207421
    user207421 about 6 years
    Eh? The order is immaterial. Unclear what you're claiming.
  • Bane
    Bane about 6 years
    @Vishrant I get that, but then why don't we just submit a million answers to this question regarding every possible tech that the user could use, which just happens to involve the original question, just in case? There are a bazillion frameworks out there; should we provide unique answers for every one? Or should we instead seek to answer the specific, actual question and allow people to extrapolate that answer to any specific use-case they are dealing with? At some point, too many answers is like having too many shopping-choices: overwhelmingly ineffective.
  • Bane
    Bane about 6 years
    @Vishrant besides, the responder wasn't even very accurate in their answer... he literally said "if using maven, do this: {add spring boot}"... that's a pretty confusing answer... I think Maven is generic enough that providing an "if using Maven"-answer actually seems appropriate... but it does NOT follow that "if using Maven", then add this spring-boot thing... Boot is awesome, but it's pretty terrible advise to add it randomly to your project in an attempt to get past this one problem and the fact you can add Boot via Maven has literally no relationship to the problem.
  • Bane
    Bane about 6 years
    @Vishrant Finally, the correct complement to "if using maven, then do this..." is this (and is not adding Boot!): <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.0.2</version> <configuration> <archive> <manifest> <mainClass>com.myPackage.MyClass</mainClass> </manifest> </archive> </configuration> </plugin>
  • Bane
    Bane about 6 years
    @Vishrant Regarding the "downvote", it's important to realize that on SO they aren't used to merely indicate "this is spam" but also to clarify answers the voter considers high- vs low-quality. When doing this, the voter actually sacrifices their reputation & takes a slight hit to do so. If they are willing to take that hit, they probably feel justified. I am not a coder just looking to make things work, but rather an architect striving for things to be 'best fit'. It's important to me to arrive-at/promote good decisions, not just ones that happen to work, and I am willing to take that hit.
  • Tendai
    Tendai about 6 years
    worked perfectly, but consider adding the version as of April 2018 <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.0.1.RELEASE</version> </plugin>
  • Reihan_amn
    Reihan_amn about 6 years
    Is there anything wrong here that I am getting down vote?!
  • Shane Kenyon
    Shane Kenyon almost 6 years
    Excellent detail, thanks so much! One Maven n00b note, simply doing a rebuild in your IDE (such as Intellij) won't build the exec JAR. You have to run mvn clean install or equivalent.
  • LAMRIN TAWSRAS
    LAMRIN TAWSRAS almost 6 years
    Sorry but it's not a solution because he is not talking about Spring Boot, it's general problem with jar execution :)
  • Maude
    Maude almost 6 years
    The 4th step of your answer is very important ! My manifest wasn't working because of that newline at the end I did not know I had to put. All answers I visited on this topic (a lot) did not mention this, and it is mandatory for anyone not using maven,ant,gradle, and so on.
  • CodeMonkey
    CodeMonkey over 5 years
    @Maude thank you for the feedback. That's precisely why I added the answer with the newline hint in bold. I looked for days until I found this out by comparing with an auto-generated manifest.
  • Matthias Bö
    Matthias Bö over 5 years
    Thanks for saving my desktop from getting dented by the pure frustration of nothing else working ;) Your link seems broken, but I can confirm that this works perfectly. Tested with IntelliJ IDEA 2018.2.5 (Community Edition)
  • Martynas Jusevičius
    Martynas Jusevičius over 5 years
    This with maven-assembly-plugin worked. maven-jar-plugin did not.
  • Kirill Karmazin
    Kirill Karmazin over 5 years
    Note: put this code in your pom.xml file inside <build><plugins> PUT IT HERE </plugins></build> and then execute maven Package (in IDEA open maven sliding menu on the right, look for project > Lifecycle > package). Then your jar file will be in Target folder . Cheers!
  • Aaron Franke
    Aaron Franke over 5 years
    Note: For Maven, I already had a maven-jar-plugin item in the VSC-generated Maven file, so I just added the <configuration> section to it.
  • julianm
    julianm over 5 years
    I was really struggling with this issue...This works perfectly. Thanks!
  • NickSoft
    NickSoft about 5 years
    This worked for me too. Try it if you are using Jetbrains IntelliJ
  • adrian filipescu
    adrian filipescu over 4 years
    For Maven the explanation above is precise!
  • lxknvlk
    lxknvlk over 4 years
    confirmed that this works, even though i dont have a /resources directory
  • matbrgz
    matbrgz over 4 years
    The quotes around "app.jar" is most likely swallowed by the shell used.
  • sem10
    sem10 over 4 years
    Thanks a lot, this solved the problem for spring boot!
  • JOSE MANUEL RAMIREZ LEON
    JOSE MANUEL RAMIREZ LEON over 4 years
    This worked together with using mvn clean package assembly:single
  • Richard Thomas
    Richard Thomas over 4 years
    Hah, thanks. I was just banging my head on that newline thing.
  • tusar
    tusar over 4 years
    I am working on spring boot 2.2.0.Release. Unfortunately, this solution did not work for me. However, it works when you have referred from a <parent>...</parent> application in the pom.xml. My guess is, if we visit the parent application's pom.xml, we will get a clear idea.
  • tusar
    tusar over 4 years
    I finally solved my issue with the custom repackage-classifier. Please visit docs.spring.io/spring-boot/docs/2.2.0.RELEASE/maven-plugin/…
  • cueedee
    cueedee about 4 years
    Having tried the for-Maven suggestion, I wanted to report that this does not work; The jar created in this way now barfs with an "Error: A JNI error has occurred, please check your installation and try again". The answer by @CodeBrew otoh, resulted in a *-jar-with-dependencies.jar that did the job just nicely.
  • Banee Ishaque K
    Banee Ishaque K almost 4 years
    this should be the answer.
  • Dimitri Kopriwa
    Dimitri Kopriwa over 3 years
    I have a task called buildRepackage , I think this is what does the task but I can't make a jar to be runnable with the whole dependency. Do you know how they work together and how I can get this done for a spring boot application?
  • Ben
    Ben over 3 years
    broken link, did you mean https://stackoverflow.com/questions/20952713/wrong-manifest-‌​mf-in-intellij-idea-‌​created-jar?
  • Ilya Serbis
    Ilya Serbis over 3 years
    For the Gradle build script written on Kotlin see this answer.
  • Rhubarb
    Rhubarb about 3 years
    sniping aside, does anyone know how to get spring boot to do this if you include the plugin but NOT the parent?
  • Lucian Radu
    Lucian Radu over 2 years
    Thanks a lot, that did the trick!
  • vainquit
    vainquit over 2 years
    This is great. I just want to mention that if you find assembly-plugin doesn't run at all, it may be caused by the pluginManagement block. Just delete it...
  • Krisztian Nagy Zsolt
    Krisztian Nagy Zsolt over 2 years
    OK. I added the code into the pom.xml and when i click Clean and Build it builds the project and i find a file named: Dice-1.0-SNAPSHOT-jar-with-dependencies.jar under: C:\Users\Name\Documents\NetBeansProjects\Dice\target but when i try to run it in Command prompt with cmd java -jar Dice-1.0-SNAPSHOT-jar-with-dependencies.jar it tells me an error that Error: JavaFX runtime components are missing, and are required to run this application. What can i do to run my jar file?
  • Krisztian Nagy Zsolt
    Krisztian Nagy Zsolt over 2 years
    Ps. the jar file is 9 mb, and it is a plain Hello World new project (JavaFX Maven) in Netbeans IDE 12.6 with JDK-17.0.1 installed.
  • jewelsea
    jewelsea over 2 years
    @KrisztianNagyZsolt your JavaFX issue is a different issue, which you have asked as a separate question, which is the correct way to ask follow-up questions rather than commenting on existing answers.
  • Malinda
    Malinda over 2 years
    please be specific when you answer questions, see other answers
  • jtyler
    jtyler over 2 years
    Thank you for posting specific build examples for different build tools!