Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
+3 votes
2 views
in Java by (11.4k points)

I have installed an application 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 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.

1 Answer

+4 votes
by (32.3k points)

I don’t know why you are executing java -jar "app" and not java -jar app.jar

Anyways, in order to make a jar executable, you need to jar a file called META-INF/MANIFEST.MF

The file should at least carry this one-liner with itself:

Main-Class: com.mypackage.MyClass

Where com.mypackage.MyClass is the class holding the public static void main(String[] args) entry point.

You should also know that there are many ways to get this done either with the CLI, Maven, Ant or Gradle:

For CLI:

jar cmvf META-INF/MANIFEST.MF <new-jar-filename>.jar  <files to include>

For Maven: Simply apply this trick on the plugin definition, not on the full 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>

(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>

For Gradle:

plugins {

    id 'java'

}

jar {

    manifest {

        attributes(

                'Main-Class': 'com.mypackage.MyClass'

        )

    }

}

Related questions

0 votes
1 answer
asked Oct 9, 2019 in Java by Anvi (10.2k points)
0 votes
1 answer
asked Mar 28, 2021 in Java by dante07 (13.1k points)
0 votes
1 answer
0 votes
1 answer
asked Jul 11, 2019 in Java by Ritik (3.5k points)
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...