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.
You might get this error when the Main-Class entry is missing in MANIFEST. MF file. You can put the maven-jar-plugin plugin in pom. xml to fix it.
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.
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' ) } }
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With