1

I have a Netbeans project with Maven that I'm trying to compile into an executable JAR file. I think so far everything works fine inside Netbeans however when I package it was a 20kb file-SNAPSHOT and couldnt get it to run.
Someone pointed me to some MAVEN code to package all dependencies and make it into a "FAT JAR" file. I did that and started getting an error "No Main Manifest Attribute" I copied some more MAVEN snippets to add the main manifest however I still get the same error "No Main Manifest" so I assume I did something wrong. Here's my POM.XML file:

<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>Client_BoxTest</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <dependencies>
    <dependency>
   <groupId>com.fazecast</groupId>
   <artifactId>jSerialComm</artifactId>
   <version>[2.0.0,3.0.0)</version>
</dependency>
    </dependencies>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>14</maven.compiler.source>
        <maven.compiler.target>14</maven.compiler.target>
    </properties>
        <build>
        <finalName>Client-Boxtest</finalName>
        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

            <plugin>
            <!-- Build an executable JAR -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.1.1</version>
                <configuration>
                  <archive>
                    <manifest>
                      <addClasspath>true</addClasspath>
                      <classpathPrefix>lib/</classpathPrefix>
                      <mainClass>my.client_boxtest.BoxTestUI</mainClass>
                    </manifest>
                  </archive>
                </configuration>
            </plugin>
            
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.1.1</version>

                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>

                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
    
            </plugin>
        </plugins>
    </build>
</project>

Now I am not sure about the location of my main class: it's a fairly simple program and my tree in NetBeans looks like this, with only one file:

File tree in Netbeans

dvd.Void
  • 339
  • 1
  • 5
  • 21

2 Answers2

2

Try to add a reference to the main class in your POM as described below, it works for me:

<project>
  [...]
  <build>
    [...]
    <plugins>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>3.3.0</version>
        <configuration>
          [...]
          <archive>
            <manifest>
              <mainClass>org.sample.App</mainClass>
            </manifest>
          </archive>
        </configuration>
        [...]
      </plugin>
      [...]
</project>
1

The maven-shade-plugin is much more powerful for create an Uber jar or fat jar and is the preferred plugin for this task for over ten years. https://maven.apache.org/plugins/maven-shade-plugin/examples/executable-jar.html describes how you should specify the mainClass.

Robert Scholte
  • 11,889
  • 2
  • 35
  • 44