I am developing a small Java application using intellij idea. There is a package and a class. The class runs as expected when outside of the jar format.
I can build a jar and deploy all the dependencies to the same directory and the jar works as expected (I do this with the copy to the output path and link via manifest option when configuring a jar build process). However when I try to build a fat jar with all dependencies included in the same jar file, I get one of two errors depending on where the META-INF/MANIFEST.MF
is positioned: no main manifest attribute and Could not find or load main class.
Given that the I could get a working jar file if all the dependencies are in the same directory as said jar file, I assumed that the issue was with the classpath.
TheMETA-INF/MANIFEST.MF
contents is as follows:
Manifest-Version: 1.0
Main-Class: myPackage.myClass
Class-Path: . myClassJarName.jar
When the jar is generated, I see the META-INF/MANIFEST.MF
at the root of the structure with the correct information.
Also in the root of the jar is the myPackage/myClass.class
, corresponding with the manifest file.
I can see all the dependencies in the jar file too.
My projects structure looks like this - note that I have added two manifest lcoations I've tried and the error which comes with each:
.idea
out
artifacts
myPackage_jar
myPackage.jar
src
main
java
myPackage
myClass.java
META-INF <-------------------- *If here, Error: Could not find or load main class err*
MANIFEST.MF
META-INF < ------------------ *If here no main manifest attribute err*
MANIFEST.MF
test
java
myPackage
myClass.java
target
classes
myPackage
myClass
generated-sources
generated-test-sources
test-classes
mypackage.iml
pom.xml
... and its pom file looks like 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>myPackage</groupId>
<artifactId>myPackage</artifactId>
<version>1.0-SNAPSHOT</version>
<name>anonymise</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<repositories>
<repository>
<id>hortonworks</id>
<name>hortonworks</name>
<url>https://repo.hortonworks.com/content/repositories/releases/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-common -->
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.7.3.2.6.2.0-205</version>
</dependency>
<dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hive/hive-exec -->
<groupId>org.apache.hive</groupId>
<artifactId>hive-exec</artifactId>
<version>1.2.1000.2.6.2.69-1</version>
</dependency>
</dependencies>
</project>
The way I configured the jar artifact is:
- Project Structure -> Artifacts
- Click + -> Jar -> From modules with dependencies
- I select the main class as the interface suggests
- Option 'Extract to target Jar'
- I currently have the
META-INF/MANIFEST.MF
Manifest directory set atsrc/main
(the full path) - In the following window, on the left I see all the dependencies of the project. At the bottom of the list, it says
myPackage compile output
After applying and oking, I generate the jar file to see, as I would expect, that inside there is both the META-INF/MANIFEST.MF
and the myPackage/myClass
in the jar root, along with a all the other dependencies. However when I try to run the jar either by:
cd
to the location of the jar and usejava -jar myGeneratedJarFilename.jar
- Use the intellij idea run/debug configuration with the 'application' or the 'run jar application'
The result is the same:
no main manifest attribute, in myGeneratedJarFilename.jar
I've tried using the invalidate caches and restart several times. I've read several sets of documentation regarding Jars, executable Jars and the Java classpath but I can't find an error.
- I am using
Intellij Idea Community 2020.2
, on Arch Linux. - Upon typing
java -version
on the terminal I seeopenjdk version "14.0.2" 2020-07-14
. - THe Class-Path in the manifest file is a bit of a mystery to me. The docs say that it is a relative path but relative to what I'm not sure. Any clarifications on that from a pro would be nice!
- The project structure was made using the org.apache.maven quickstark archetype
Can anyone offer any ideas as to what may be the problem?
Thanks a lot in advance!