简体   繁体   中英

Can't run jar file on command line with Maven

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.

  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

Try adding the following nodes to the pom.xml file:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM