0

I have finished making my Java program with a Swing GUI in NetBeans and have built, cleaned and run it error free. However, now I am looking to save it as a runnable format, I seem to be missing the trick. I have spent hours searching and have tried the following aside from all of the dead ends or years old questions that as since not applicable:

  • Opened the program in Eclipse and exported a runnable .jar file
  • Run "Build and Clean" in NetBeans to generate the "snapshot" .jar file
  • I have tried running both of these .jar files from both command line and elevated command line with varying errors.

I have a feeling it has to do with my pom.xml file, but as I said I cannot find anything or I am missing the point entirely.

This is my pom.xml file:

<?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.OETrackerV0</groupId>
    <artifactId>OETrackerV0</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <dependencies>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>5.0.0</version>
            <type>jar</type>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>5.0.0</version>
        </dependency>
        <dependency>
            <groupId>com.formdev</groupId>
            <artifactId>flatlaf</artifactId>
            <version>1.1</version>
            <type>jar</type>
        </dependency>
    </dependencies>
    <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>frameOne</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>15</maven.compiler.source>
        <maven.compiler.target>15</maven.compiler.target>
    </properties>
</project>

I'm happy to edit and post the rest of the code if necessary, it is also on my GitHub here: https://github.com/ChaseVsGodzilla/oetracker.git

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
ChaseVsGodzilla
  • 102
  • 1
  • 1
  • 5
  • 2
    "I have tried running both of these .jar files from both command line and elevated command line with varying errors." How were you trying to run them, and what errors did you encounter? Those are really important details that need to be in the question. – Jon Skeet Mar 31 '21 at 10:13

1 Answers1

0

The maven-jar-plugin will package your application sources within the target JAR but without packing your dependencies, hence you end-up with an un-executable archive.

You have two options to package your application with its dependencies:

Maven Assembly Plugin

You can use the maven-assembly-plugin assemble:single goal to package your application within an fat (jar with dependencies) JAR:

<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>
                        <mainClass>frameOne</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>3.1.1</version>
            <!-- You need the JAR with dependencies descriptor -->
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
            <!-- You can 'optionally' bind the plugin to the `package` build phase for automatic execution over builds -->
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Maven Shade Plugin

You can use the maven-shade-plugin with custom inclusion and exclusion rule sets for your dependencies / classes:

<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>
                        <mainClass>frameOne</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.2.4</version>
            <executions>
              <execution>
                <phase>package</phase>
                <goals>
                  <goal>shade</goal>
                </goals>
                <configuration>
                  <minimizeJar>true</minimizeJar>
                  <filters>
                    <filter>
                       <artifact>org.apache.poi:poi</artifact>
                       <includes>
                           <include>**</include>
                       </includes>
                    </filter>
                    <filter>
                       <artifact>org.apache.poi:poi-ooxml</artifact>
                       <includes>
                           <include>**</include>
                       </includes>
                    </filter>
                    <filter>
                       <artifact>com.formdev:flatlaf</artifact>
                       <includes>
                           <include>**</include>
                       </includes>
                    </filter>
                  </filters>
                </configuration>
              </execution>
            </executions>
        </plugin>
    </plugins>
</build>
tmarwen
  • 15,750
  • 5
  • 43
  • 62