1

I have a simple Hello World application 'App' which has been made in a Maven project. I'm aware that after building the project, the Maven assembly plugin is required in order to execute the JAR which is created when the project is built. I have followed the instructions found at:

http://maven.apache.org/plugins/maven-assembly-plugin/usage.html

But after editing my pom.xml and re-building the project, I still cannot run my JAR file.

Here is my 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.mycompany</groupId>
<artifactId>mini-project-6-ex6</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<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>
<build>
<plugins>
  <plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.5.4</version>
    <configuration>
      <descriptorRefs>
        <descriptorRef>jar-with-dependencies</descriptorRef>
      </descriptorRefs>
      <archive>
        <manifest>
          <mainClass>com.mycompany.mini.project.ex6.App</mainClass>
        </manifest>
      </archive>
    </configuration>
  </plugin>
</plugins>
</build>

Is there a mistake in my POM or additional lines which I have left out which is causing me to not be able to run the resulting JAR file?

EDIT: It seems as though the JAR has no manifest attribute (see comments). I suppose that I could open the jar using 7zip or similar program and manually add a manifest file, but the real question is how to create the JAR using Maven to include a manifest file in the first place.

hjalpmig
  • 702
  • 1
  • 13
  • 39
  • What is the stacktrace when running the jar file?? – ryekayo May 07 '15 at 14:32
  • @ryekayo 'no main manifest attribute, in mini-project-6-ex6-1.0-SNAPSHOT.jar' When executing the .jar from cmd using 'java -jar mini-project-6-ex6-1.0-SNAPSHOT.jar' – hjalpmig May 07 '15 at 14:45
  • Ok so that means, the jar file is not finding the main class. The pom file needs to know what the main class is. – ryekayo May 07 '15 at 14:54
  • http://stackoverflow.com/questions/9689793/cant-execute-jar-file-no-main-manifest-attribute – Andreas Waldahl May 07 '15 at 14:56
  • Does the line com.mycompany.mini.project.ex6.App in the pom.xml not specify the entry point of the application? – hjalpmig May 07 '15 at 15:02
  • I'm confused. Are you asking how to BUILD the jar file (which the assembly plugin might do, but the regular jar plugin is probably enough), or are you asking how to RUN a jar file? (for which you would use the maven exec plugin) – Gimby May 07 '15 at 15:04
  • @Gimby Ive had success before building and running .jar files using cmd, but this is the first time that I have used Maven to create and run .jar files. When I build the project in netbeans it creates a .jar file for me but I am unable to run it. I was under the impression that to be able to run the .jar you must add the maven assembly plugin to pom.xml and specify the attribute which I have done. However, after then rebuilding the .jar I am still unable to execute it through clicking or using cmd. – hjalpmig May 07 '15 at 15:11

1 Answers1

1

You're looking at the wrong JAR. The POM above will create at least two JARs:

mini-project-6-ex6-1.0-SNAPSHOT.jar
mini-project-6-ex6-1.0-SNAPSHOT-jar-with-dependencies.jar

The former is only the code of the module, the latter is the code plus all the dependencies.

If you run mvn install, you will only get the first one since the Assembly Plugin isn't configured to run automatically. You can run it manually with mvn assembly:assembly.

If you want it to run automatically when you mvn install, you need to change the POM:

<plugins>
  <plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.5.4</version>
    <configuration>
      ...
    </configuration>
    <executions>
      <execution>
        <id>make-assembly</id> <!-- this is used for inheritance merges -->
        <phase>package</phase> <!-- bind to the packaging phase -->
        <goals>
          <goal>single</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

See http://maven.apache.org/plugins/maven-assembly-plugin/usage.html#Execution:_Building_an_Assembly

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • When I build the project, it only creates one JAR file, which is: mini-project-6-ex6-1.0-SNAPSHOT.jar – hjalpmig May 07 '15 at 15:27
  • When running from the command line my program now works and print 'Hello World' to the command line, however when ran by using ht mouse pointer and double clicking the .jar file, nothing is displayed, do you know what the issue for this could be? – hjalpmig May 07 '15 at 15:57
  • If double clicking doesn't work, then the default Java on your computer is broken somehow. I need more information to help. – Aaron Digulla May 08 '15 at 08:23