简体   繁体   中英

How to run specific class from war file?

I have a Maven Web Application that contains only a single class in the Source Packages folder. I have obtained the war file from this project which I would like to copy and run on another machine. I would like to be able to run only that specific class using the commandline.

The reason why I still want to use the war file is because that class implements a RESTful service so I don't know if there is another way to go (like making it a jar and end right there). My war file also contains several dependencies and one of them has the provided scope:

    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-web-api</artifactId>
        <version>7.0</version>
        <scope>provided</scope>
    </dependency>

This is an extract from the pom.xml file and is the only dependency that does not appear in the WEB-INF/lib folder. However, I don't know if this is the problem.

My class is named ClientSide and is under the client package. Running the command:

    java -cp warFileName.war client.ClientSide

resulted in this error:

Error: Could not find or load main class client.ClientSide

Adding Main-Class: client.ClientSide to the manifest file did not change anything. I have tried the solutions proposed here: How do I run a class in a WAR from the command line? but with no luck. This error would keep repeating.

The only way I can run that class is by right-clicking on the class inside the Web Application and select Run File from netbeans, which is very annoying.

You can use

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.4</version>
        <configuration>
            <archive>
                <manifest>
                    <addClasspath>true</addClasspath>
                    <mainClass>client.ClientSide.YourMain</mainClass>
                    <classpathPrefix>./lib/</classpathPrefix>
                </manifest>
            </archive>
        </configuration>
    </plugin>

You can put this in your main pom.xml , Here you are telling where is your main class

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