This was my original pom.xml file I started with:
<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.calculator</groupId>
<artifactId>GraphingCalculator</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>23</maven.compiler.source>
<maven.compiler.target>23</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.matheclipse</groupId>
<artifactId>matheclipse-core</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
<version>2.0.9</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.21.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.36</version>
</dependency>
<dependency>
<groupId>org.scilab.forge</groupId>
<artifactId>jlatexmath</artifactId>
<version>1.0.7</version>
</dependency>
</dependencies>
</project>
I then compiling the jar using: mvn clean package
.
I then ran the jar using:
java -jar GraphingCalculator-1.0-SNAPSHOT.jar
This gave me the error:
no main manifest attribute, in GraphingCalculator-1.0-SNAPSHOT.jar
I did some research and tried adding this to my pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.calculator.Main</mainClass> <!-- Replace with your actual main class -->
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.4.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.calculator.Main</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.4.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.calculator.Main</mainClass>
</transformer>
</transformers>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Then when I run, I get this error:
WARNING: Runtime environment or build system does not support multi-release JARs. This will impact location-based features. Exception in thread "main" java.lang.ExceptionInInitializerError at com.calculator.ExpressionEvaluator.(ExpressionEvaluator.java:31) at com.calculator.Main.(Main.java:78) at com.calculator.Main.main(Main.java:711) Caused by: java.lang.UnsupportedOperationException: No class provided, and an appropriate one cannot be found. at org.apache.logging.log4j.LogManager.callerClass(LogManager.java:573) at org.apache.logging.log4j.LogManager.getLogger(LogManager.java:598) at org.apache.logging.log4j.LogManager.getLogger(LogManager.java:585) at org.matheclipse.core.eval.ExprEvaluator.(ExprEvaluator.java:136) ... 3 more
How would I fix my pom.xml to allow my jar file to be run? I don't ever use Log4j2 myself in my code, but I use the Symja API to handle math, which I think uses Log4j2. I couldn't figure out how to remove Log4j2 while getting Symja to still work so I just kept it. When I run my code as a java file, it works perfectly fine but when I try to package it into a jar, it breaks, and I can't run it.
Check the Log4j implementation not found in fat JAR question. While your symptoms differ, the root cause of both problems is shading, which, if done incorrectly, will break the libraries you shade.
In your particular case your application does not have the Multi-Release: true
entry in the manifest, which prevents Log4j API from using the Java 9+ version of StackLocator
and causes the appearance of the warning:
WARNING: Runtime environment or build system does not support multi-release JARs. This will impact location-based features.
and the exception that follows. See our FAQ entry about single-JAR applications for a list of other possible problems with shading.
Using the Maven Shade Plugin requires knowing a lot of technical details about the libraries you shade. Fortunately there is an alternative: "JAR-in-JAR" packaging allows you to bundle all your dependencies in a single JAR, without breaking the dependency JARs themselves.
To create a "JAR-in-JAR" archive, you can use for example Spring Boot Maven plugin:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>3.4.5</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>