Reputation: 41
I want to compile my Java-Project with maven but it didn't add the main-class to the manifest file. So i can't run the jar outside of intellij. I've added the main-class to my pom.xml but it don't show up in the manifest file.
I'm using the build function of intellij on a Win10 machine and maven3 (IntelliJ IDEA 2018.3.1/plugins/maven/lib/maven3).
If i run the jar i get the error
$java -jar myJar.jar
kein Hauptmanifestattribut, in myJar.jar
(German for: no main manifest attribute, in myJar.jar, i don't know the exact english translation for that but it means: no main class specified)
I've tried serveral maven plugins (maven-compile, maven-jar, maven-shade) and ways to specify the main-class but non of them added it to the manifest file. The only way i get this to work is when i added the line
Main-Class: Main
by hand.
my pom file (yes, my main-class called Main and yes it works fine when i added it by hand)
...
<groupId>com.myname</groupId>
<artifactId>myProject</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
...
the manifest file
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: dspicuzz
Created-By: Apache Maven 3.3.9
Build-Jdk: 1.8.0_65
If i added the line by hand it runs fine
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: dspicuzz
Created-By: Apache Maven 3.3.9
Build-Jdk: 1.8.0_65
Main-Class: Main
But i can't get maven to generate it for me this way.
Upvotes: 4
Views: 3954
Reputation: 1284
I'm using the build function of intellij on a Win10 machine and maven3 (IntelliJ IDEA 2018.3.1/plugins/maven/lib/maven3).
IntelliJ's build will not regenerate your jar file. Try running the package goal to recreate the jar file.
Verify that your package name matches you directory structure: com.mytest.Main
If there is no package it will also work but make sure the directory structure has no package structure either.
This will work:
pom.xml
<?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.myname</groupId>
<artifactId>runnable</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.myname.Hello</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
</project>
Hello.java
package com.myname;
public class Hello {
public static void main(String[] args) {
System.out.println("Hello");
}
}
run:
C:\runnable>java -jar target\runnable-1.0-SNAPSHOT.jar
Hello
And, this is the generated manifest file by the maven-jar plugin:
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: egallardo
Created-By: Apache Maven 3.3.9
Build-Jdk: 1.8.0_181
Main-Class: com.myname.Hello
Also check that your sources directory is properly marked in intellij (in light blue) This image is for the packageless version.
These are step by step instructions on how to create the project from scratch using IntelliJ:
<build>
and <properties>
sections to your pom.xml filejava -jar C:\code\java\runnable\target\runnable-1.0-SNAPSHOT.jar
Check that the jar file date matches the time when you clicked package. If the above doesn't work, you might have found a bug. Try a different JDK version.
Upvotes: 1