0

The problem I have is that when I build a project and then tries to run the jar file nothing happens. When I run java -jar Tried-1.jar I get this error message: no main manifest attribute, in Tried-1.jar

I have open the jar file as an archive and found that the Main-Class: line in the MANIFEST.MF file doesn't exist. I have checked an older project that I built in Netbeans 12.0 and that one runs and has the Main-Class: line in its Manifest file.

I tried to build the old project in Netbeans 12.2 and I encounter the same problem that the Main-Class: line doesn't exist.

I have tried multiple fixes and none works, deciding what is the main class in project properties doesn't solve the problem, and other solutions to this problem by changing the pom.xml file doesn't help either, just causes more problems.

I have also tried to run the class file within the jar file from the command prompt and that works fine.

I have read the documentation for Maven but I couldn't find anything to resolve my problem.

Update

I was able to fix the problem by chaning my pom.xml file to 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mycompany</groupId>
    <artifactId>Tried</artifactId>
    <version>1</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>15</maven.compiler.source>
        <maven.compiler.target>15</maven.compiler.target>
    </properties>
    <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>3.2.0</version>
        <configuration>
          <archive>
            <manifest>
                <mainClass>com.mycompany.tried.NewMain</mainClass>
            </manifest>
          </archive>
        </configuration>
  </plugin>
</plugins>
</build>
</project>

But I still have the problem that I would need to add this every time I create a project, is there a way to make this automatic at every new project?

Netbeans 12.2 | Maven 3.6.3 | Java JDK 15.0.1

  • 1
    How are you packaging your jar? Is there a tool in Netbeans that does this for you? Previously I've used either a Maven plugin to automate this or have just created the file by hand under META-INF and had it included in the jar – Kevin Hooke Apr 21 '21 at 15:16
  • It does all automatically when I build the project, but I think it's the `maven-jar-plugin:2.4:jar` – Semlan Bakelsen Apr 21 '21 at 15:36
  • what's in your pom.xml file? – Kevin Hooke Apr 21 '21 at 15:39
  • Take a look at [this answer to _“No Main Manifest Attribute” in ----.jar Netbeans_](https://stackoverflow.com/a/21224284/2985643) which shows how you can configure **maven-jar-plugin** in your **pom.xml** to specify `` and ``. But even if that works it doesn't really explain why it is necessary when using 12.2, but not when using 12.0. Or have you also changed your version of **maven-jar-plugin**, or its configuration in **pom.xml**? – skomisa Apr 21 '21 at 20:51
  • I was able to change the pom.xml file to fix the problem but how do I make it so I don't need to do this every time? – Semlan Bakelsen Apr 22 '21 at 09:57
  • @SemlanBakelsen Why do you think you _"need to do this every time"_? Just edit your **pom.xml** once to fix the problem, and that is all you need to do. Why is that a problem? – skomisa Apr 23 '21 at 09:57
  • Does this answer your question? ["No Main Manifest Attribute" in ----.jar Netbeans](https://stackoverflow.com/questions/12266735/no-main-manifest-attribute-in-jar-netbeans) – skomisa Apr 23 '21 at 09:58
  • @skomisa The problem is that when I create a new project the pom.xml file will not include the main-class lines. And then the jars don't run :/ – Semlan Bakelsen Apr 23 '21 at 12:32
  • @SemlanBakelsen OK, understood. A possible approach might be to use project inheritance with your POMs. Have a parent POM, and inherit from that POM in each project. [See the Maven documentation for details](https://maven.apache.org/guides/introduction/introduction-to-the-pom.html#Project_Inheritance). But you would still have to configure project-specific features of each project within its own POM. Also, since that is a distinct problem from the one you originally raised and have now resolved, please create a new question for any new issues arising if necessary. – skomisa Apr 23 '21 at 19:00

0 Answers0