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

46

1392

I have installed an application and when I try to run it (it's an executable jar) nothing happens. When I run it from the command line 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 too 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.

Armenian answered 13/3, 2012 at 18:20 Comment(14)
Look for main methods; you can't rely on class names.Kail
I know, but since I only have .class files I can't really see the methods. Or can I?Armenian
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.Kail
Related: with dependencies: https://mcmap.net/q/22062/-how-can-i-create-an-executable-runnable-jar-with-dependencies-using-mavenStefan
what if I don't have main class as I am running the code using CommandLineJobRunnerMurdock
I had this problem with a library that had worked previously, simply because I was missing execution declaration steps...Hayne
Solution involves adding META-INF to resources directory and few other steps explained here youtube.com/watch?v=wPGSas_f0tsHigley
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?Investment
Hey @Armenian I noticed you haven't selected an answer as the solution. You may have forgotten to so I'm just letting you know.Ollie
The easiest way is to just change the Maven/Gradle settings directlyCanopy
@Ollie 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.Armenian
The plugin config mentioned in the answer here would help- #54867795Decry
When packaging a jar file specify the main class name by option e followed by the class name in jar.exe command lineMalnourished
An alternative to java -cp when using maven: mvn exec:java -Dexec.mainClass="com.example.Main" [-Dexec.args="argument1"] ...Dado
R
1372

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'
        )
    }
}
Rennarennane answered 13/3, 2012 at 18:27 Comment(15)
In Ant its <manifest><attribute name="Main-Class" value="com.mypackage.MyClass"/></manifest> within the <jar> elementDunedin
Thank you. Just wanted to add that you can copy dependencies to the lib folder using this: https://mcmap.net/q/46318/-make-maven-to-copy-dependencies-into-target-lib. 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.Astrogeology
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 !Teutonize
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.Rennarennane
@Teutonize 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>Cocotte
thank you, will edit my answer. Are you sure about this command? it looks weird: a file, a destination... then files again.Rennarennane
*usually named as maven so prefix would be maven <classpathPrefix>maven/</classpathPrefix>Nonattendance
for those dealing with android studio. see @QED answer https://mcmap.net/q/45314/-can-39-t-execute-jar-file-quot-no-main-manifest-attribute-quotBirth
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.Pandanus
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.Buyse
For Maven the explanation above is precise!Distillation
The quotes around "app.jar" is most likely swallowed by the shell used.Trigonal
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.Remark
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?Investment
Thank you for posting specific build examples for different build tools!Langille
M
390

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).

Mamey answered 13/3, 2012 at 18:27 Comment(10)
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?Armenian
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.Mamey
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.Mamey
That's funny since the other system is running win7 and this pc with the problems runs win8.Armenian
Is Java officially supported on Windows 8?Mamey
@Mamey Hello, what if eclipse is using the default package? Do I just put the class name?Accalia
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.Mamey
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.Accompany
@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.Mamey
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.Clambake
S
180

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.

Sawyor answered 26/12, 2014 at 22:44 Comment(13)
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.Derian
In the context of the shade plugin, one has to follow the help at Executable JAR.Drops
This with maven-assembly-plugin worked. maven-jar-plugin did not.Nonanonage
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!Icken
I was really struggling with this issue...This works perfectly. Thanks!Unifilar
This worked together with using mvn clean package assembly:singleCasebound
this should be the answer.Lowermost
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...Elishaelision
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?Knot
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.Knot
@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.Durazzo
Any ideas why the packaging process is incredibly slow even with just one small dependency?Philippines
@MartynasJusevičius this was it for me. If you have a maven-jar-plugin defined it seems those settings will be ignored. possible fix is delete that block or copy in the values from it into the assembly blockTarttan
A
70

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 have just found a really neat way to specify the application's entry point in Eclipse :

  1. Select File > Export..
  2. Select JAR file then Next
  3. Give it a name in the next window then Next
  4. Select Next again
  5. 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

Angellaangelle answered 29/9, 2013 at 0:26 Comment(0)
H
62

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

<build>
  <plugins>
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
  </plugins>
</build>
Hazeghi answered 23/3, 2018 at 9:38 Comment(5)
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>Floorwalker
Sorry but it's not a solution because he is not talking about Spring Boot, it's general problem with jar execution :)Cavesson
Thanks a lot, this solved the problem for spring boot!Coated
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.Goda
I finally solved my issue with the custom repackage-classifier. Please visit docs.spring.io/spring-boot/docs/2.2.0.RELEASE/maven-plugin/…Goda
H
41

I had this issue when creating a jar using IntelliJ IDEA. See this discussion.

What solved it for me was to re-create the jar artifact, choosing JAR > From modules with dependencies, but not accepting the default Directory for META-INF/MANIFEST.MF. Change it from -/src/main/java to -/src/main/resources.

Otherwise it was including a manifest file in the jar, but not the one in -/src/main/java that it should have.

Headsman answered 9/8, 2016 at 16:51 Comment(4)
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.Claro
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)Kindle
confirmed that this works, even though i dont have a /resources directorySerrated
broken link, did you mean https://stackoverflow.com/questions/20952713/wrong-manifest-mf-in-intellij-idea-created-jar?Regurgitate
I
36

For maven, this is what solved it (for me, for a Veetle codebase on GitHub):

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-shade-plugin</artifactId>
      <version>2.0</version>
      <executions>
        <execution>
          <phase>package</phase>
          <goals>
            <goal>shade</goal>
          </goals>
          <configuration>
            <transformers>
              <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                <mainClass>org.lazydevs.veetle.api.VeetleAPI</mainClass>
              </transformer>
            </transformers>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>
Impendent answered 1/3, 2013 at 16:6 Comment(3)
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.Karleen
This solution was mentioned in an above comment. See maven.apache.org/plugins/maven-shade-plugin/examples/…Louvenialouver
I get an error Error: A JNI error has occured, please check your instllation and try again. Exception in thread "main" java.lang.SecurityEception: Invalid signature file digestfor Manifest main attributes. . I'm looking for a solutionLouvenialouver
T
34

The Gradle answer is to add a jar/manifest/attributes setting like this:

apply plugin: 'java'

jar {
    manifest {
        attributes 'Main-Class': 'com.package.app.Class'
    }
}
Trident answered 10/5, 2016 at 0:15 Comment(2)
the simplest answer thus far.Birth
For the Gradle build script written on Kotlin see this answer.Jennyjeno
T
31

Try this command to include the jar:

java -cp yourJarName.jar your.package..your.MainClass
Tortoise answered 23/8, 2013 at 7:31 Comment(1)
One way is to include main class in pom.xml and use java -jar command, other one is to use java -cp command.Obituary
V
21

For me, none of the answers really helped - I had the manifest file in correct place, containing the Main-Class and everything. What tripped me over was this:

Warning: The text file from which you are creating the manifest must end with a new line or carriage return. The last line will not be parsed properly if it does not end with a new line or carriage return.

(source). Adding a newline at the end of the manifest fixed it.

Vitrics answered 25/1, 2015 at 9:15 Comment(0)
B
19

The MAVEN problem is that its try to include the first MANIFEST.MF file from first library from dependencies instead of THE OUR OWN MANIFEST.MF WHEN YOU USE ARTIFACTS!.

  1. Rename yourjar.jar to yourjar.zip
  2. Open MANIFEST.MF file from META-INF\MANIFEST.MF
  3. Copy the real MANIFEST.MF that already generate in your project by MAVEN That include somelike that:

    Manifest-Version: 1.0 Main-Class: yourpacket.yourmainclass (for exmaple info.data.MainClass)

  4. Replace the content of MANIFEST.MF from youjar.zip with it.

  5. Rename yourjar.zip to yourjar.jar back.
  6. Now java -jar yourjar.jar work perfectly.

OR!

Simple create you own MANIFEST.MF and:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>3.2.0</version>
    <configuration>
        <archive>
            <manifestFile> Your path like: src/main/resources/META-INF/MANIFEST.MF </manifestFile>
            <index>true</index>
                <manifest>
                    <addClasspath>true</addClasspath>
                </manifest>
        </archive>
    </configuration>
</plugin>

But if you use maven panel (or maven command line) you can force it to generate own manifest and include it into JAR file.

  1. Add to the you pom.xml's build section this code:

    <plugins>
        <plugin>
    
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>3.2.0</version>
        <executions>
            <execution>
                <phase>package</phase>
                <goals>
                    <goal>single</goal>
                </goals>
            </execution>
        </executions>
    
        <configuration>
            <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
    
            <archive>
    
                <index>true</index>
    
                <manifest>
                    <addClasspath>true</addClasspath>
                    <mainClass> yourpacket.yourmainclass (for exmaple info.data.MainClass)</mainClass>
                </manifest>
                <manifestEntries>
                    <mode>development</mode>
                    <url>${project.url}</url>
                </manifestEntries>
            </archive>
        </configuration>
    </plugin>
    

  2. Open the MAVEN panel (in Intellij) and execute "Install". It will generate the MANIFEST file and compile property the JAR file with all dependencies into the "Target" folder. Also it will be installed to the local maven repository.

Blather answered 31/8, 2019 at 9:41 Comment(0)
S
17

If using Maven, include following in the pom

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.2.RELEASE</version>
</parent>

<properties>
    <java.version>1.8</java.version>
</properties>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
Spouse answered 23/11, 2016 at 21:29 Comment(10)
Who said this was a Spring Boot project?Bidwell
@Bidwell 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.Bangtail
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.Bangtail
@Bangtail 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.Dissension
@Dissension 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.Bangtail
@Bangtail 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.Dissension
@Bangtail 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.Dissension
@Bangtail 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>Dissension
@Bangtail 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.Dissension
sniping aside, does anyone know how to get spring boot to do this if you include the plugin but NOT the parent?Ruffianism
L
12

I had the same issue today. My problem was solved my moving META-INF to the resources folder.

Lynnelynnea answered 23/4, 2017 at 14:46 Comment(2)
This worked for me too. Try it if you are using Jetbrains IntelliJFriedrich
Great, I spent 3 hours to solve this. You got it bro, thanks I was using IntelliJ.🫡Toscanini
P
11

I got same error just now. If u're using gradle, just add next one in ur gradle.build:

apply plugin: 'java'

jar {
    manifest {
        attributes 'Main-Class': 'com.company.project.MainClass'
    }
}

Where com.company.project.MainClass path to ur class with public static void main(String[] args) method.

Puiia answered 1/9, 2017 at 10:39 Comment(1)
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 .jarNasal
N
9

If you are using the command line to assemble .jar it is possible to point to the main without adding Manifest file. Example:

jar cfve app.jar TheNameOfClassWithMainMethod *.class

(param "e" does that: TheNameOfClassWithMainMethod is a name of the class with the method main() and app.jar - name of executable .jar and *.class - just all classes files to assemble)

Numerator answered 20/11, 2016 at 1:0 Comment(0)
C
8

If the jar isn't following the rules, it's not an executable jar.

Comb answered 13/3, 2012 at 18:24 Comment(2)
+1 for the Jar File Specification link (docs.oracle.com/javase/1.4.2/docs/guide/jar/jar.html)Uppity
please be specific when you answer questions, see other answersTiki
H
6

I had the same problem. A lot of the solutions mentioned here didn't give me the whole picture, so I'll try to give you a summary of how to pack jar files from the command line.

  1. If you want to have your .class files in packages, add the package in the beginning of the .java.

    Test.java

    package testpackage;
    
    public class Test
    {
        ...
    }
    
  2. To compile your code with your .class files ending up with the structure given by the package name use:

    javac -d . Test.java
    

    The -d . makes the compiler create the directory structure you want.

  3. When packaging the .jar file, you need to instruct the jar routine on how to pack it. Here we use the option set cvfeP. This is to keep the package structure (option P), specify the entry point so that the manifest file contains meaningful information (option e). Option f lets you specify the file name, option c creates an archive and option v sets the output to verbose. The important things to note here are P and e.

    Then comes the name of the jar we want test.jar.

    Then comes the entry point .

    And then comes -C . <packagename>/ to get the class files from that folder, preserving the folder structure.

    jar cvfeP test.jar testpackage.Test -C . testpackage/
    
  4. Check your .jar file in a zip program. It should have the following structure

    test.jar

    META-INF
    | MANIFEST.MF
    testpackage
    | Test.class
    

    The MANIFEST.MF should contain the following

    Manifest-Version: 1.0
    Created-By: <JDK Version> (Oracle Corporation)
    Main-Class: testpackage.Test
    

    If you edit your manifest by hand be sure to keep the newline at the end otherwise java doesn't recognize it.

  5. Execute your .jar file with

    java -jar test.jar
    
Helpful answered 20/1, 2017 at 6:55 Comment(3)
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.Courtship
@Courtship 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.Helpful
Hah, thanks. I was just banging my head on that newline thing.Eberhardt
K
6

I personally think all the answers here are mis-understanding the question. The answer to this lies in the difference of how spring-boot builds the .jar. Everyone knows that Spring Boot sets up a manifest like this, which varies from everyones asssumption that this is a standard .jar launch, which it may or may not be :

Start-Class: com.myco.eventlogging.MyService
Spring-Boot-Classes: BOOT-INF/classes/
Spring-Boot-Lib: BOOT-INF/lib/
Spring-Boot-Version: 1.4.0.RELEASE
Created-By: Apache Maven 3.3.9
Build-Jdk: 1.8.0_131
Main-Class: org.springframework.boot.loader.JarLauncher

Perhaps it needs to executed with org.springframework.boot.loader.JarLauncher on the classpath?

Koren answered 27/10, 2017 at 18:37 Comment(1)
This sounds promising. How exactly do you add JarLauncher to the classpath?Fandango
B
6

I found a new solution to bad manifest generation !

  1. Open the jar file with a zip editor like WinRAR
  2. Click on for META-INF

  3. Add or edit

    • Add:

      • Create a text file called MANIFEST.MF in a folder called META-INF and add the following line:

        • Manifest-Version: 1.0
        • Main-Class: package.ex.com.views.mainClassName
      • Save the file and add it to the zip

    • Edit:

      • Drag the file out modify the MANIFEST.MF to add the previous line
  4. Open cmd and type: java -jar c:/path/JarName.jar

It should work fine now !

Boom answered 9/1, 2018 at 17:29 Comment(1)
This idea is pure genius. Thankyou. Works a treat. now why we have still to do this is beyond me. I wish it was not.Genoese
I
5

I faced the same issue and it's fixed now:) Just follow the below steps and the error could be for anything, but the below steps makes the process smoother. I spend lot of time to find the fix.

1.Try restart the Eclipse (if you are using Eclipse to built JAR file) --> Actually this helped my issue in exporting the JAR file properly.

2.After eclipse restart, try to see if your eclipse is able to recognize the main class/method by your Java project --> right click --> Run as --> Run configurations --> Main --> click Search button to see if your eclipse is able to lookup for your main class in the JAR file. --> This is for the validation that JAR file will have the entry point to the main class.

  1. After this, export your Java Dynamic project as "Runnable JAR" file and not JAR file.

  2. In Java launch configuration, choose your main class.

  3. Once export the jar file, use the below command to execute. java -cp [Your JAR].jar [complete package].MainClass eg: java -cp AppleTCRuleAudit.jar com.apple.tcruleaudit.classes.TCRuleAudit

  4. You might face the unsupported java version error. the fix is to change the java_home in your shell bash profile to match the java version used to compile the project in eclipse.

Hope this helps! Kindly let me know if you still have any issues.

Intermixture answered 26/2, 2016 at 0:2 Comment(0)
O
5

I tried this and it worked for me. mvn clean install package should work.

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
            <execution>
                <goals>
                    <goal>repackage</goal>
                </goals>
            </execution>
            </executions>
        </plugin>
    </plugins>
</build>
Oklahoma answered 4/2, 2020 at 5:26 Comment(0)
G
4

Any executable jar file Should run either by clicking or running using command prompt like java -jar app.jar (use "if path of jar contains space" - i.e. java -jar "C:\folder name\app.jar"). If your executable jar is not running, which means it is not created properly.

For better understanding, extract the jar file (or view using any tool, for windows 7-Zip is nice one) and check the file under /META-INF/MANIFEST.MF. If you find any entry like

Main-Class: your.package.name.ClaaswithMain - then it's fine, otherwise you have to provide it.

Be aware of appending Main-Class entry on MANIFEST.MF file, check where you are saving it!

Gingili answered 3/8, 2012 at 14:4 Comment(0)
A
4

You might not have created the jar file properly:

ex: missing option m in jar creation

The following works:

jar -cvfm MyJar.jar Manifest.txt *.class
Avernus answered 28/8, 2013 at 17:59 Comment(0)
L
4

You Can Simply follow this step Create a jar file using

 jar -cfm jarfile-name manifest-filename Class-file name

While running the jar file simple run like this

 java -cp jarfile-name main-classname
Lab answered 28/9, 2013 at 4:59 Comment(0)
B
4

For my case the problem is <pluginManagement> under <build> makes things cannot work properly.

My original pom.xml:

<build>
  <pluginManagement>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        ...
        ...
        ...
  </pluginManagement>
</build>

After removing <pluginManagement>, the error is gone.

Bicorn answered 26/2, 2021 at 7:28 Comment(1)
Thanks a lot, that did the trick!Wimbush
M
3

For me this error occurred simply because I forgot tell Eclipse that I wanted a runnable jar file and not a simple library jar file. So when you create the jar file in Eclipse make sure that you click the right radio button

Mckenziemckeon answered 27/11, 2015 at 6:38 Comment(0)
M
3

The above answers were only partly helpful for me. java -cp was part of the answer, but I needed more specific info on how to identify the class to run. Here is what worked for me:

Step 1: find the class I need to run

jar tf /path/to/myjar.jar | more

The top lines of the result were:

META-INF/
META-INF/MANIFEST.MF
somepath/
somepath/App.class
META-INF/maven/
...

App.class contained the main class to run. I'm not 100% sure if you can always assume the class you need is the first one, but it was for me. If it isn't, I'd imagine it isn't too hard to use grep to exclude library-related results to pare the class list down to a manageable size.

From there it was easy: I just use that path (minus the ".class" suffix):

java -cp /path/to/myjar.jar somepath/App
Mendelian answered 21/4, 2017 at 21:11 Comment(1)
It isn't always the first class. Hardly ever. Command line should be java -cp ... somepackage.App.Rhombohedral
B
3

(first post - so it may not be clean)

This is my fix for OS X 11.6, Maven-based Netbeans 8.2 program. Up to now my app is 100% Netbeans - no tweaking (just a few shell escapes for the impossible!).

Having tried most all of the answers here and elsewhere to no avail, I returned to the art of "use what works".

The top answer here (olivier-refalo thanx) looked like the right place to start but didn't help.

Looking at other projects which did work, I noticed some minor differences in the manifest lines:

  1. addClasspath, classpathPrefix were absent (deleted them)
  2. mainClass was missing the "com." (used the NB -> Project Properties->Run->Main Class->Browse to specify)

Not sure why (I am only 3 months into java) or how, but can only say this worked.

Here is just the modified manifest block used:

    <manifest>
        <mainClass>mypackage.MyClass</mainClass>
    </manifest>
Birl answered 19/6, 2017 at 19:52 Comment(0)
B
3

most of the solutions did not work for me but my instructor helped me out i would like to share his solution here i used kali linux terminal but should be fine in all debian

javac *.java
nano MANIFEST.MF

in the file type

Main-Class: Main or whatever your main file name is (make sure to add package name if it exists)

jar -cvmf MANIFEST.MF new.jar *.class

now to run the file use

java -jar new.jar

or you can go to propeties of file and check

Allow Execution of file as program

double click on it

it helped me while most of the above answers did not

Balfour answered 18/6, 2019 at 18:23 Comment(0)
D
3

In my case - I work on a multi-module project - I could introduced the issue in the following way:

I added this into the parent pom.xml, which caused the issue. Namely, the skip with value true:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <!--
                besides hindering the packaging, this also skips running the app after build when calling spring-boot:run. You have to enable it in the
                corresponding module by setting skip to false, there.
                -->
                <skip>true</skip>
            </configuration>
        </plugin>
    </plugins>
</build>

I fixed the issue by adding the same configuration to the modules that i wanted to be packaged as a jar, but changed the value of skip to false:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <version>${spring-boot.version}</version>
    <configuration>
        <mainClass>${project.mainClass}</mainClass>
        <layout>ZIP</layout>
        <skip>false</skip>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>repackage</goal>
            </goals>
        </execution>
        <execution>
            <id>build-info</id>
            <goals>
                <goal>build-info</goal>
            </goals>
        </execution>
    </executions>
</plugin>
Detrude answered 12/5, 2021 at 9:50 Comment(0)
S
2

Since you've add MANIFEST.MF, I think you should consider the order of Field in this file. My env is java version "1.8.0_91"

and my MANIFEST.MF as here

// MANIFEST.MF
Manifest-Version: 1.0
Created-By: 1.8.0_91 (Oracle Corporation)
Main-Class: HelloWorldSwing

// run
~ java -jar HelloWorldSwing.jar
no main manifest attribute, in HelloWorldSwing.jar

However, this as below run through

Manifest-Version: 1.0
Main-Class: HelloWorldSwing
Created-By: 1.8.0_91 (Oracle Corporation)

//this run swing normally
Solicitous answered 22/3, 2017 at 9:47 Comment(1)
Eh? The order is immaterial. Unclear what you're claiming.Rhombohedral
C
2

Just to make one point clear about

Main-Class: <packagename>.<classname>

If you don't have package you have to ignore that part, like this:

Main-Class: <classname>
Chuckhole answered 22/9, 2017 at 22:37 Comment(1)
Is there anything wrong here that I am getting down vote?!Chuckhole
S
2

I was getting the same error when executing mvn package. This is how I solved the problem.

I was using the maven-multi-module. I was facing this issue because I added below part in parent pom by mistake.

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

Then replacing this to submodules pom.xml from parent pom solved my problem.

Smiga answered 27/11, 2019 at 5:18 Comment(0)
D
2

If your on maven and your pom.xml is something like this

    <?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.3</version>
        <relativePath/> <!-- lookup parent from repository -->
      </parent>
      <groupId>com.example</groupId>
      <artifactId>demo</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <name>demo</name>
      <properties>
        <java.version>11</java.version>
      </properties>
      <dependencies>
        <!-- dependencies -->
      </dependencies>
      <build>
        <pluginManagement>
          <plugins>
            <plugin>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
          </plugins>
        </pluginManagement>
      </build>
    </project>

just comment the pluginManagement that will result you in the following 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.3</version>
        <relativePath/> <!-- lookup parent from repository -->
      </parent>
      <groupId>com.example</groupId>
      <artifactId>demo</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <name>demo</name>
      <properties>
        <java.version>11</java.version>
      </properties>
      <dependencies>
        <!-- dependencies -->
      </dependencies>
      <build>
        <plugins>
          <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
          </plugin>
        </plugins>
      </build>
    </project>

Deity answered 8/3, 2021 at 11:3 Comment(0)
L
2

First I created an artifact using this instruction: https://www.jetbrains.com/help/idea/compiling-applications.html#package_into_jar

Then when this error occurred this short video helped me: https://www.youtube.com/watch?v=AeN1yLSjfLU In short, the essence of the answer is as follows. Even though we have a file MANIFEST.MF lying (for example) on the way to src/main/java/com.example.demo.META-INF/MANIFEST.MF in which there is a string indicating the path to the Main class:

Main-Class: com.example.demo.DemoApplication

When we building an artifact, there is a file MANIFEST.MF in the interior. For some reason he does not have inside himself this string. To remove this error, we need to manually get this MANIFEST.MF from the artifact/archive and add this path from the original MANIFEST.MF to it.

Logger answered 2/5, 2023 at 17:48 Comment(0)
S
1

You might have the same problem as I do. After creating your .jar file, write jar xf app.jar META-INF/MANIFEST.MF. This will create a copy of the file to your current directory so you can read it. If it only says something like:

Manifest-Version: 1.0

Created-By: 1.8.0_51 (Oracle Corporation)

and does not contain the "Main-Class" declaration, then I think you found your problem.

I do not know how to solve it, though. I checked other people with same/similar problems on StackOverflow and couldn't find an answer. However with this information you can perhaps get some better help (given the fact that you have the same problem as I).

Edit: I had tried with a manifest-file but didn't get it to work, but my mistake was to only name one of the classes when creating the jar-file. I wrote *.class instead and it works now.

Although I don't know why there is a need to create a manifest-file. But I guess it's fine as long as it works.

Saguache answered 17/11, 2015 at 22:14 Comment(0)
L
1

I had this problem and i solved it recently by doing this in Netbeans 8 (Refer to the image below):

Netbeans project properties

  1. go to properties of your project.
  2. click on Run.
  3. specify the main class of your project using browse.
  4. build and run the Jar file.
Ledet answered 1/2, 2017 at 5:45 Comment(0)
H
1

Found a great solution which would help in any such situation, given you just need a runnable jar, which you do in most cases. If your application is running in Intellij Idea follow these steps: 1) Go to module settings and then artifacts, and add a jar and define main class 2) Then go to Build in the menu and click "build artifact" and you get the jar.

This worked even when I changed the source folder and used scala instead of java.

Hostage answered 15/3, 2019 at 5:9 Comment(0)
U
1

I have similar questions from my students, but I have added this answer as is a bit different, it covers another HOT topic: How to build a JAR form IntelliJ JavaFX project since IntelliJ in 2023 allows to build a fast JavaFX project.

Some ideas:

  • Use maven
  • Start form IntelliJ
  • Use maven power to make a jar with shaved plugs in
  • Use cmd liken or double-click

Without filling all at the page with HTML / code, I can make a little project with detailed PDF, step by step.

(From IntelliJ JavaFX demo to JAR .pdf) https://github.com/ingconti/JavaFXMinimalClickableDemo

Hope it helps.

Ungula answered 11/4, 2023 at 12:39 Comment(0)
J
0

check your jar file inside MANIFEST.MF Main-Class is available or not

first.java

class first
{
        public static void main (String arg[ ])
        {
           System.out.println("Welcome to the world of Java");
        }
}

Before:

Manifest-Version: 1.0
Created-By: 1.7.0_80 (Oracle Corporation)

sony@sony-VPCEH25EN:~/Documents$ java -jar first.jar
no main manifest attribute, in first.jar

After:

Manifest-Version: 1.0
Created-By: 1.7.0_80 (Oracle Corporation)
Main-Class: first

sony@sony-VPCEH25EN:~/Documents$ java -jar first.jar 
Welcome to the world of Java
Jipijapa answered 4/12, 2016 at 0:52 Comment(0)
B
0

I had a similar issue as you, in below a syntax to create successfully .war File:-

jar {cvf} [jar-file] [manifest-file]

manifest When creating (c) or updating (u) a JAR file, the manifest operand defines the preexisting manifest files with names and values of attributes to be included in MANIFEST.MF in the JAR file. The manifest operand must be specified if the f option is present '[1]'.

In order to create manifest file you need to defined a value for some attributes, you could put asterisk after the (.WAR) file name to avoid creating manifest file:-

jar -cvf foo.war *

To be honest with you I don't know if that is a best practice but it do the work for me :).

Benisch answered 28/12, 2016 at 13:15 Comment(0)
A
0

Create the folder META-INF and the file MANIFEST.MF in that folder with this content:

Manifest-Version: 1.0
Class-Path: .
Main-Class: [YOUR_MAIN_CLASS]

Then compile including that manifest file.

Agathaagathe answered 6/7, 2020 at 22:21 Comment(0)
A
0

Simply add this to your java module's build.gradle. It'll create executable jar. It will include dependent libraries in archive.

jar {
  manifest { 
    attributes "Main-Class": "com.company.application.Main"
  }  

  from {
    configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
  }
}

This will result in [module_name]/build/libs/[module_name].jar file. I tested this with shell.

Affection answered 22/7, 2020 at 12:54 Comment(0)
D
0

If you are using IntelliJ, then JEtBrains has detailed the steps to fix this over here

https://www.jetbrains.com/help/idea/2022.2/convert-a-regular-project-into-a-maven-project.html#develop_with_maven

Create an executable JAR You can conclude the following optional steps to create an executable JAR.

Click the Build Project icon to build project. IntelliJ IDEA generates target folder. Note that IntelliJ IDEA only compiles sources and doesn't create either JAR file or Manifest file.

Create a Manifest file in the resources directory.

Right-click the directory, select New | Directory to create the META-INF subdirectory. Then right-click the subdirectory, select New | File to create the MANIFEST.MF file.

the Manifest file Open the MANIFEST.MF file in the editor and add information about your main class.

Check the following code:

Main-Class: com.company.MyApp Alternatively, we can ask Maven to add this line of code into the MANIFEST.MF file with the following code in pom.xml:

org.apache.maven.plugins maven-jar-plugin com.company.MyApp In your POM specify the Manifest file information, so you can use Maven to generate an executable jar file.

POM with manifest

Dolce answered 27/3, 2023 at 14:38 Comment(0)
T
0

I am using a Quarkus based application. In my case, this did the trick:

./mvnw package -Dquarkus.package.type=uber-jar

Hope it helps!

Thracian answered 16/1 at 12:36 Comment(0)
T
-1

Check your local .m2 direcory for a sub directory of this artifact. If exixts - delete it, and perform Maven update again

Triolet answered 5/7, 2016 at 14:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.