1

I want to execute jar file as standalone application. When I run the below command I get this error message:

[rcbandit@Laptop target]$ /opt/jdk1.8.0/bin/java -jar DX57DC-1.0.jar
no main manifest attribute, in DX57DC-1.0.jar
[rcbandit@Laptop target]$ 

This is the POM configuration:

<?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.dx57dc</groupId>
    <artifactId>DX57DC</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>

    <name>DX57DC</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <mainClass></mainClass>
    </properties>

    <organization>
        <name>Corporation Name</name>
    </organization>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.8</version>
                <executions>
                    <execution>
                        <id>unpack-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>unpack-dependencies</goal>
                        </goals>
                        <configuration>
                            <excludeScope>system</excludeScope>
                            <excludeGroupIds>junit,org.mockito,org.hamcrest</excludeGroupIds>
                            <outputDirectory>${project.build.directory}/classes</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>com.zenjava</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>2.0</version>
                <configuration>
                    <mainClass>com.dx57dc.main.DX57DC</mainClass>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

I suppose that I'm missing a maven plugin. Can you tell me how I can fix this?

Dmitry
  • 2,943
  • 1
  • 23
  • 26
Peter Penzov
  • 1,126
  • 134
  • 430
  • 808
  • 1
    possible duplicate of [How can I create an executable jar with dependencies using Maven?](http://stackoverflow.com/questions/574594/how-can-i-create-an-executable-jar-with-dependencies-using-maven) – Raedwald Aug 23 '13 at 21:22

4 Answers4

2

Your jar doesn't contain a Manifest that specifies the executable main() function, so java doesn't know which class to execute/start.

When using maven, have a look at the assembly plugin. This enables you to create a jar with the correct Manifest.

Or simply start your programm with: java -cp DX57DC-1.0.jar 'your_main_class_here'

Regards,

Mike

boskop
  • 609
  • 5
  • 23
2

Create your jar as follows:

  • Open Command Prompt or Terminal and locate the directory where all Classes are stored
  • Type following command:

    jar cfev YourJarName.jar EntryClass *

Or, If Your classes are in some package then,

  • Go Outside Your Package Folder and run the following command:

    jar cfev YourJarName.jar YourPackage.EntryClass YourPackage/*

This will create a jar file. Now if Double Clicking does not open the jar then,

  • Locate the Directory in the Terminal or Cmd, where the jar file is kept, and run the following command:

    java -jar YourJarName.jar args

Here Options were:

    -c  create new archive
    -f  specify archive file name
    -e  specify application entry point for stand-alone application
        bundled into an executable jar file
    -v  generate verbose output on standard output

Hope this will Help.

Abhishek Kashyap
  • 3,332
  • 2
  • 18
  • 20
1

try adding something like this to plugins:

    <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.3</version>
            <executions>
                <execution>
                    <goals>
                        <goal>attached</goal>
                    </goals>
                    <phase>package</phase>
                    <configuration>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                        <archive>
                            <manifest>
                                <mainClass>com.package.to.my.Main</mainClass>
                            </manifest>
                        </archive>
                    </configuration>
                </execution>
            </executions>
        </plugin>
Alper Akture
  • 2,445
  • 1
  • 30
  • 44
  • 1
    Which plugin will be more suitable? maven-assembly-plugin or the maven-jar-plugin – Peter Penzov Aug 23 '13 at 21:01
  • We've been using the assembly plugin, I haven't tried the jar plugin, but maybe that's the way to go. – Alper Akture Aug 23 '13 at 21:53
  • With the assembly plugin there are two jar files generated. How I can generate just one? – Peter Penzov Aug 23 '13 at 22:55
  • Should only be one, are you sure you removed the jar plugin? What command are you using to build? – Alper Akture Aug 23 '13 at 22:59
  • I pasted the the POM file at the beginning. – Peter Penzov Aug 23 '13 at 23:01
  • If you only have either the jar or the assembly plugin, I'm not sure how there could be two. But regarding which is more appropriate, assembly plugin is geared for building an uber jar (containing all dependent jars in addition to your classes). Both jar and assembly plugin seem to require the shade plugin beyond if you require more than "basic" support. http://maven.apache.org/plugins/maven-assembly-plugin/ – Alper Akture Aug 27 '13 at 01:52
1

In order for a JAR file to be executable it needs to have a manifest file with Main-Class and Class-Path entries:

       <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <mainClass>my.package.App</mainClass>               
                    </manifest>           
                </archive>
                <executions>
                    <execution>
                        <id>default-package</id>
                        <phase>package</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </configuration>
        </plugin>

This produces a MANIFEST.MF file under META-INF directory in the root of your jar file. Only relevant entries listed:

Class-Path: lib/somejar.jar
Main-Class: my.package.App

The Class-Path states that in the directory where the jar file resides there exists a lib folder with file somejar.jar in it.

The Main-Class states that the file App.class exists in package my.package and its main method will be run.

update

If the lib folder isn't present the execution will fail when first dependent class is to be loaded. To evade this you can pack all the dependencies in your jar with the shade plugin:

       <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.1</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>my.package.App</mainClass>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>

Since all the dependencies are packed together the Class-Path entry is not necessary any more.

Community
  • 1
  • 1
linski
  • 5,046
  • 3
  • 22
  • 35