Can't run jar file on command line with Maven

Logan Phillips :

I am trying to run a jar file that has been packaged by Maven. When I run java -jar target/java-prac-1.0-SNAPSHOT.jar I get the following output: no main manifest attribute, in target/java-prac-1.0-SNAPSHOT.jar

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>localdomain.localhost.tutorial</groupId>
    <artifactId>java-prac</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies></dependencies>

    <build>
        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <manifest>
                       <mainClass>localdomain.localhost.tutorial.Main</mainClass>

                    </manifest>
                </configuration>
            </plugin>

        </plugins>
</build>
</project>           

My MANIFEST.MF file looks like


Manifest-Version: 1.0                                                                                                            
Main-Class: localdomain.localhost.tutorial.Main

The interesting thing is that when I run ls target/classes, the 'META-INF/' folder is listed. So the file seems to be in the jar file, java just can't find it for some reason.

lonecloud :
  1. you can add this plugins into your pom.xml file
        <plugins>          
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <!--add you main class-->
                            <mainClass>cn.lonecloud.RefDemo</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy</id>
                        <phase>install</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>
                                ${project.build.directory}
                            </outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
  1. run this command
mvn compile package
  1. run your jar file
java -jar target/java-prac-1.0-SNAPSHOT.jar

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=144603&siteId=1