简体   繁体   中英

Run maven web application from command line

I'm trying to run my maven web application through:

java -jar CliniKeyMaven-1.0-SNAPSHOT.war

First, I faced the error no main manifest attribute in CliniKeyMaven-1.0-SNAPSHOT.war and after searching for a quite long time I reached this in my pom.xml:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.1.1</version>
        <executions>
            <execution>
                <phase>package</phase>
                <goals>
                    <goal>manifest</goal>
                </goals>
            </execution>
        </executions>
    <configuration>
        <failOnMissingWebXml>false</failOnMissingWebXml>
        <attachClasses>true</attachClasses>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <mainClass>com.mycompany.clinikeymaven.App</mainClass>
            </manifest>
        </archive>
    </configuration>
</plugin>

and this is the App.java that contains a main:

package com.mycompany.clinikeymaven;

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

But I'm now facing the error:

Could not find or load main class com.mycompany.clinikeymaven.App

When I run the application from the NetBeans it is working but I need to run it from the command line.. what should I do?

this is a working pom .Pls try this one.

<build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.0.0</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>

                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>com.mycompany.clinikeymaven.App</mainClass>
                        </manifest>
                    </archive>

                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id> 
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
    </build>

You need to user the Maven jar plugin and not the Maven war plugin

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.4</version>
    <configuration>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <mainClass>com.mycompany.clinikeymaven.App</mainClass>
            </manifest>
            <compress>true</compress>
            <index>true</index>
        </archive>
    </configuration>
</plugin>

Also as people said in the comments, with the maven-war-plugin you are creating a .war file that needs an application server to be deployed on in order to run.
For example the generating .war could be deployed on in tomcat server.

Try

java -cp CliniKeyMaven-1.0-SNAPSHOT.war com.mycompany.clinikeymaven.App

Alternately, you can update the content of your manifest file to following:

Main-Class: com.mycompany.clinikeymaven.App

Then you can try running it the following way:

java -jar CliniKeyMaven-1.0-SNAPSHOT.war

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