无法使用Maven在命令行上运行jar文件

时间:2019-03-21 00:55:39

标签: java maven jar terminal executable

我正在尝试运行由Maven打包的jar文件。运行java -jar target/java-prac-1.0-SNAPSHOT.jar时,得到以下输出: 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>           

我的MANIFEST.MF文件看起来像


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

有趣的是,当我运行ls target/classes时,列出了'META-INF /'文件夹。所以该文件似乎在jar文件中,java出于某种原因找不到它。

2 个答案:

答案 0 :(得分:3)

  1. 您可以将此插件添加到您的pom.xml文件中
        <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. 运行此命令
mvn compile package
  1. 运行您的jar文件
java -jar target/java-prac-1.0-SNAPSHOT.jar

答案 1 :(得分:0)

尝试将以下节点添加到pom.xml文件:

<build>
     <plugins>
         <plugin>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-maven-plugin</artifactId>
         </plugin>
     </plugins>
</build>