-1

i try to using Eclipse Maven to build jar with dependencies but when i execute

java -jar App.jar

got the error : "no main method found"

My pom.xml looks like this:

<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.maventest</groupId>
    <artifactId>aproject</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>aproject</name>
    <url>http://maven.apache.org</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

    </dependencies>
</project>

it's just a simple java code:

package com.maventest.aproject;

public class App {
    public static void main( String[] args ) {
        System.out.println( "Hello World!" );
    }
}

eclipse version: 4.5.2

Can anyone help me with this? Thanks.

Bilesh Ganguly
  • 3,792
  • 3
  • 36
  • 58
matt.crawfoord
  • 87
  • 1
  • 3
  • 13

1 Answers1

5

You can use maven-jar-plugin for that purpose.

<build>
    <plugins>
         <plugin>
            <!-- Build an executable JAR -->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>${maven-jar-plugin.version}</version>
            <configuration>
                <archive>
                    <manifest>
                        <!-- <addClasspath>true</addClasspath> -->
                        <mainClass>twaren.elk.elkMaven.App</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
      </plugins>
</build>
ma3stro
  • 313
  • 2
  • 11
  • got the error "'build.plugins.plugin.version' for org.apache.maven.plugins:maven-jar-plugin must be a valid version but is '${maven-jar-plugin.version}'." – matt.crawfoord Jun 07 '16 at 06:23
  • You should provide a compatible plugin version in ${maven-jar-plugin.version} i.e. 2.6. Here you can find the versions: [http://mvnrepository.com/artifact/org.apache.maven.plugins/maven-jar-plugin](http://mvnrepository.com/artifact/org.apache.maven.plugins/maven-jar-plugin). – ma3stro Jun 07 '16 at 06:30
  • For Maven plugins you should better consult http://maven.apache.org/plugins/ – khmarbaise Jun 07 '16 at 07:09